Jump to content

Module:TemplateStructure: Difference between revisions

No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 1: Line 1:
local p = {}
local p = {}
-- Helper function to convert the mw.html object to a string,
-- checking for available methods for different MediaWiki versions.
local function convertHtml(obj)
    if obj.toString then
        return obj:toString()
    elseif obj.html then
        return obj:html()
    elseif obj.all then
        return obj:all()
    else
        return tostring(obj)
    end
end


--[[
--[[
Line 43: Line 57:
     end
     end


     -- Return the full HTML string for the table.
     -- Return the full HTML string for the table using our conversion helper.
     return tableEl:toString()
     return convertHtml(tableEl)
end
end


return p
return p

Revision as of 04:43, 16 February 2025

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

local p = {}

-- Helper function to convert the mw.html object to a string,
-- checking for available methods for different MediaWiki versions.
local function convertHtml(obj)
    if obj.toString then
        return obj:toString()
    elseif obj.html then
        return obj:html()
    elseif obj.all then
        return obj:all()
    else
        return tostring(obj)
    end
end

--[[
    p.render(args, config)
    
    Renders a table using a modular, block-based approach via the HTML builder API.
    This version generates an explicit <table> element with its rows inserted as unescaped wikitext,
    so that MediaWiki’s parser won’t inject unwanted <p> or <br> elements.
    
    Parameters:
      args   - Table of template parameters (passed to each block function).
      config - Table containing configuration options:
          tableClass: (string) CSS class for the table.
                      Default: "template-table"
          tableAttrs: (table) A table of additional attributes for the table tag.
                      Default: { cellpadding = "2" }
          blocks:     (array) An ordered list of functions (blocks) that generate table rows.
                      Each block function should accept (args, config) and return a string.
    
    Returns:
      A string containing the complete HTML markup for the table.
]]
function p.render(args, config)
    config = config or {}
    local tableClass = config.tableClass or "template-table"
    local tableAttrs = config.tableAttrs or { cellpadding = "2" }
    local blocks = config.blocks or {}

    -- Create a table element with the given attributes.
    local tableEl = mw.html.create("table", tableAttrs)
    tableEl:attr("class", tableClass)
    
    -- Process each block function and insert its output as unescaped wikitext.
    for i, block in ipairs(blocks) do
        if type(block) == "function" then
            local blockOutput = block(args, config)
            if blockOutput and blockOutput ~= "" then
                tableEl:wikitext(blockOutput)
            end
        else
            mw.log("Warning: Block #" .. i .. " is not a function and will be skipped.")
        end
    end

    -- Return the full HTML string for the table using our conversion helper.
    return convertHtml(tableEl)
end

return p