Module:Hatnote list

From Uncyclopedia, the content-free encyclopedia
Jump to navigation Jump to search

local p = {}

-- Check if text is already a wikilink
local function isWikilink(text)
    return text:match("^%s*%[%[.+%]%]%s*$") ~= nil
end

function p.render(frame)
    -- Get parent template (source of the numeric link arguments)
    local parent = frame:getParent()
    if not parent then
        return "<strong class='error'>Error: No parent frame (preview mode)</strong>"
    end

    -- Collect link arguments from the template
    local links = {}
    local i = 1
    while parent.args[i] do
        local v = mw.text.trim(parent.args[i])
        if v ~= "" then
            if isWikilink(v) then
                table.insert(links, v)
            else
                table.insert(links, "[[" .. v .. "]]")
            end
        end
        i = i + 1
    end
    
    if #links == 0 then return "" end

    -- Format list with commas and 'and'
    local listText
    if #links == 1 then
        listText = links[1]
    else
        listText = table.concat(links, ", ", 1, #links - 1) .. " and " .. links[#links]
    end

    -- Optional pretext from the #invoke call
    local pretext = frame.args["pretext"]
    if not pretext or mw.text.trim(pretext) == "" then
        pretext = "Further information:"
    end

    -- Wrap in Hatnote template
    return frame:expandTemplate{
        title = "Hatnote",
        args = { pretext .. " " .. listText }
    }
end

return p