Jump to content

Module:Navbox

From Logic World Wiki
Revision as of 00:56, 8 September 2025 by Felipe (talk | contribs) (trim list items)

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

local p = {}

local args, groups

local function parseGroups()
    local groups = {}
    local currentGroup = nil

    local i = 1
    while args[i] ~= nil do
        local arg = args[i]
        i = i + 1

        if arg:sub(1, 2) == "+ " then
            table.insert(currentGroup.items, mw.text.trim(arg:sub(3)))
        else
            currentGroup = { title = arg, items = {} }
            table.insert(groups, currentGroup)
        end
    end

    return groups
end

local function renderTitleRow(tbl)
    tbl:tag('tr')
        :tag('th')
            :attr('class', 'navbox-title')
            :attr('scope', 'col')
            :attr('colspan', '3')
            :tag('div')
                :wikitext(args.title)
end

local function renderAboveRow(tbl)
    if args.above and args.above ~= "" then
        tbl:tag('tr')
            :tag('td')
                :attr('class', 'navbox-abovebelow')
                :attr('colspan', '3')
                :tag('div')
                    :wikitext(args.above)
    end
end

local function renderBelowRow(tbl)
    if args.below and args.below ~= "" then
        tbl:tag('tr')
            :tag('td')
                :attr('class', 'navbox-abovebelow')
                :attr('colspan', '3')
                :tag('div')
                    :wikitext(args.below)
    end
end

local function renderListRow(tbl, group, isOdd)
    local listClass = isOdd and "navbox-list1 navbox-list navbox-odd" or "navbox-list1 navbox-list navbox-even"

    local row = tbl:tag('tr')
    row:tag('th')
        :attr('scope', 'row')
        :attr('class', 'navbox-group')
        :wikitext(group.title)

    local listContent = table.concat(group.items, " ")
    
    mw.log(listContent)

    row:tag('td')
        :attr('class', listClass)
        :attr('style', 'width:100%; padding:0px; margin:0px')
        :tag('div')
            :wikitext(listContent)
end

p.navbox = function(frame)
    args = frame:getParent().args

    groups = parseGroups()

    local navbox = mw.html.create('div')
        :addClass('navbox')

    local tbl = navbox:tag('table')
		:addClass('nowraplinks')
		:addClass("navbox-inner")
		:addClass(args.bodyclass)

    renderTitleRow(tbl)
    renderAboveRow(tbl)

    for i, group in ipairs(groups) do
        renderListRow(tbl, group, i % 2 == 1)
    end

    renderBelowRow(tbl)

    return tostring(navbox)
end

return p