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

-- <nowiki>
--------------------------------------------------------------------------------
-- A module for building minimalistic, table-based navboxes.
--------------------------------------------------------------------------------
local p = {}
local getArgs = require("Dev:Arguments").getArgs

function p.main(frame)
    local title = frame:getParent():getTitle()

    -- It's faster to use frame:preprocess() here than the mw.title library,
    -- because the latter raises the expensive parser function count by 2.
    --
    -- Use the fullurl magic word for the discuss link to avoid polluting
    -- Special:WantedPages.
    --
    -- Add spaces inside of each link to increase the clickable area.
    local view = "[[" .. title .. "|V ]]"
    local discuss = "[{{fullurl:{{TALKPAGENAME:" .. title .. "}}}} &#32;D ]"
    local edit = "[{{fullurl:" .. title .. "|action=edit}} &#32;E]"

    -- Build the navbox container and top section.
    local args = getArgs(frame)
    local navbox = mw.html.create("table")
        :addClass("mw-collapsible")
        :addClass("mw-collapsed")
        :addClass("navbox")
        :attr("data-expandtext", "More")
        :attr("data-collapsetext", "Less")
        :tag("tr")
            :tag("td")
                :addClass("navbox__vde")
                :tag("span")
                    :attr("title", "View this template")
                    :wikitext(view)
                    :done()
                :wikitext("&middot;")
                :tag("span")
                    :attr("title", "Discuss this template")
                    :wikitext(discuss)
                    :done()
                :wikitext("&middot;")
                :tag("span")
                    :attr("title", "Edit this template")
                    :wikitext(edit)
                    :done()
                :done()
            :tag("th")
                :addClass("navbox__title")
                :wikitext(args.title or "{{{title}}}")
                :done()
            :tag("td")
                :addClass("navbox__toggle")
                :done()
            :done()

    -- Build the navbox contents.
    -- if there's no data for the next row, finish the current one and break
    -- guarantees that at least one row will be built
    for i = 1, 100 do
        local isLastRow = not args["header" .. i + 1] and
                          not args["data" .. i + 1]

        navbox:tag("tr")
            :tag("th")
                :addClass("navbox__header")
                :wikitext(args["header" .. i] or "{{{header" .. i .. "}}}")
                :done()
            :tag("td")
                :addClass("navbox__data")
                :addClass(isLastRow and "navbox__data--last-row" or nil)
                :addClass("inline-list")
                :attr("colspan", "2")
                :wikitext(args["data" .. i] and
                          "\n" .. args["data" .. i] .. "\n" or
                          "{{{data" .. i .. "}}}")
                :done()
            :done()

        if isLastRow then break end
    end

    return frame:preprocess(tostring(navbox))
end

return p
-- </nowiki>
-- [[Category:Lua Modules]]
Community content is available under CC-BY-SA unless otherwise noted.