Documentation for this module may be created at Module:Hidden/doc
local p = {}
function p.main(frame)
-- Get arguments from the template
local args = frame:getParent().args
-- First parameter is the header
local header = args[1] or "Hidden content"
-- Everything from parameter 2 onwards is content
local contentParts = {}
local i = 2
while args[i] do
table.insert(contentParts, args[i])
i = i + 1
end
local content = table.concat(contentParts, "|")
-- Named parameters for additional options
local id = args.id or ""
local class = args.class or ""
local style = args.style or ""
local collapsed = args.collapsed or "yes"
-- Sanitize and prepare the header
header = mw.text.trim(header)
if header == "" then
header = "Hidden content"
end
-- Build CSS classes - match the original format
local cssClasses = {"mw-collapsible", "mw-collapsed", "wikitable"}
-- Override collapsed state if explicitly set to no
if collapsed == "no" or collapsed == "false" or collapsed == "0" then
cssClasses = {"mw-collapsible", "wikitable"}
end
-- Add custom class if provided
if class ~= "" then
table.insert(cssClasses, mw.text.trim(class))
end
local classAttr = table.concat(cssClasses, " ")
-- Build the default style to match original
local defaultStyle = "width: 100%; table-layout: fixed; display: table"
local finalStyle = defaultStyle
if style ~= "" then
finalStyle = defaultStyle .. "; " .. style
end
-- Build the HTML structure
local html = mw.html.create()
local table = html:tag('table')
:attr('class', classAttr)
:attr('style', finalStyle)
-- Add custom ID if provided
if id ~= "" then
table:attr('id', mw.text.trim(id))
end
-- Create the header row (th automatically becomes the collapsible toggle)
local headerRow = table:tag('tr')
local headerCell = headerRow:tag('th')
:wikitext(header)
-- Create the content row - single td containing everything
local contentRow = table:tag('tr')
local contentCell = contentRow:tag('td')
-- Handle the content - preserve it as-is to avoid parser interference
if content ~= "" then
contentCell:wikitext(content)
end
return tostring(html)
end
-- Alternative function for direct invocation with parameters
function p.create(frame)
local args = frame.args
local header = args.header or args[1] or "Hidden content"
local content = args.content or args[2] or ""
local id = args.id or ""
local class = args.class or ""
local style = args.style or ""
local collapsed = args.collapsed or "yes"
-- Build CSS classes to match original
local cssClasses = {"mw-collapsible", "mw-collapsed", "wikitable"}
if collapsed == "no" or collapsed == "false" or collapsed == "0" then
cssClasses = {"mw-collapsible", "wikitable"}
end
if class ~= "" then
table.insert(cssClasses, mw.text.trim(class))
end
local classAttr = table.concat(cssClasses, " ")
-- Default style matching original
local defaultStyle = "width: 100%; table-layout: fixed; display: table"
local finalStyle = defaultStyle
if style ~= "" then
finalStyle = defaultStyle .. "; " .. style
end
-- Build HTML
local html = mw.html.create()
local table = html:tag('table')
:attr('class', classAttr)
:attr('style', finalStyle)
if id ~= "" then
table:attr('id', mw.text.trim(id))
end
-- Header row - th becomes collapsible toggle automatically
table:tag('tr'):tag('th'):wikitext(mw.text.trim(header))
-- Content row - single td
table:tag('tr'):tag('td'):wikitext(content)
return tostring(html)
end
return p