Module:Navigation box

From Wikipedia

Documentation for this module may be created at Module:Navigation box/doc

local z = {
    wikitext = require "Module:Wikitext"
}

function linkedtitle(item)
    local link = item.link
    local text = item.text
    if ( link ~= nil ) then
        if ( text ~= nil ) then text = "|" .. text else text = "" end
        text = "[[" .. link .. text .. "]]"
    else
        text = text or ""
    end
    if ( item.boldface ) then text = "'''" .. text .. "'''" end
    return text
end

function makelist(stars, list)
    local text = {}
    for i,item in ipairs(list) do
        if ( nil ~= item.raw ) then
            table.insert(text, stars .. item.raw)
        elseif ( nil ~= item.link or nil ~= item.text ) then
            table.insert(text, stars .. " " .. linkedtitle(item) .. "\n")
        else
            table.insert(text, makelist(stars .. "*", item))
        end
    end
    return table.concat(text)
end

function maketable(data, params, control)
    local rows = {}
    local headingclass = "navbox-group"
    for i,v in ipairs(data) do
        local heading = linkedtitle(v.title or {})
        local subtable = v.subtable
        local body1
        if ( subtable ) then body1 = "\n" .. maketable(subtable, params, control) .. "\n" else body1 = "" end
        local bodylist = makelist("*", v)
        local body2 
        if ( "" ~= bodylist ) then 
            body2 = mw.text.tag({name="div", contents="\n" .. bodylist, params=params})
            control.odd = not control.odd
        else 
            body2 = "" 
        end
        local bodyclass = "navbox-list "
        if ( control.odd ) then bodyclass = bodyclass .. "navbox-odd" else bodyclass = bodyclass .. "navbox-even" end
        table.insert(rows, "|-\n! class=\"" .. headingclass .. "\"| " .. heading .. "\n| class=\"" .. bodyclass .. "\"|" .. body1 .. body2 .. "\n")
    end
    local class = "nowraplinks navbox-subgroup"
    local style = ""
    return  "{| class=\"" .. class .. "\" " .. style .. "\n" .. table.concat(rows) .. "|}"
end

function z.navbox (frame, data, styles)
    local links
    local modname = data.module
    if ( nil ~= modname ) then
        local viewurl = mw.url.full("Module:" .. modname, "")
        local talkurl = mw.url.full("Module talk:" .. modname, "")
        local editurl = mw.url.full("Module:" .. modname, "action=edit")
        local edittext = "*" .. mw.text.tag({name="span", contents="[" .. editurl .. " edit]\n", params={title="Edit the navigation data"}}) .. "\n"
        local viewtext = "*" .. mw.text.tag({name="span", contents="[" .. viewurl .. "  view]\n", params={title="View the navigation data"}}) .. "\n"
        local talktext = "*" .. mw.text.tag({name="span", contents="[" .. talkurl .. " talk]\n", params={title="Discuss the navigation data"}}) .. "\n"
        local text = "\n" .. edittext .. viewtext .. talktext
        links = mw.text.tag({name="div", contents=text, params={class="plainlinks noprint navbar mini hlist"}}) .. "\n"
    else
        links = ""
    end
    local params = { class="hlist" }
    local control = { odd = true }
    local title = makelist("*", data.title)
    -- For the [[Project:NavFrame]] magic in [[MediaWiki:Common.js]] to work, this must be a "!" not a "|".
    if ( "" ~= title ) then title = "|-\n! class=\"navbox-title\"|" .. mw.text.tag({name="div", contents=links .. "\n" .. title, params=params}) .. "\n" end
    local above = makelist("*", data.above)
    if ( "" ~= above ) then above = "|-\n| class=\"navbox-abovebelow\"|" .. mw.text.tag({name="div", contents="\n" .. above, params=params}) .. "\n" end
    local below = makelist("*", data.below)
    if ( "" ~= below ) then below = "|-\n| class=\"navbox-abovebelow\"|" .. mw.text.tag({name="div", contents="\n" .. below, params=params}) .. "\n" end
    local middle = maketable(data, params, control)
    local innerclass = "nowraplinks hlist collapsible autocollapse navbox-inner"
    local innerstyle = "border-spacing:0;background:transparent;color:inherit;;"
    local inner = "\n{| class=\"" .. innerclass .. "\" style=\"" .. innerstyle .. "\"\n" .. title .. above .. "|-\n| class=\"navbox-group\"|\n" .. middle .. "\n" .. below .. "|}"
    return "\n{| class=\"navbox\"\n|-\n|" .. inner .. "\n|}"
end

return z