More actions
Created page with "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 parame..." |
No edit summary |
||
Line 29: | Line 29: | ||
end | end | ||
-- Build CSS classes | -- Build CSS classes - match the original format | ||
local cssClasses = {"mw-collapsible"} | local cssClasses = {"mw-collapsible", "mw-collapsed", "wikitable"} | ||
-- | -- Override collapsed state if explicitly set to no | ||
if collapsed == " | if collapsed == "no" or collapsed == "false" or collapsed == "0" then | ||
cssClasses = {"mw-collapsible", "wikitable"} | |||
end | end | ||
Line 43: | Line 43: | ||
local classAttr = table.concat(cssClasses, " ") | 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 | -- Build the HTML structure | ||
Line 48: | Line 55: | ||
local table = html:tag('table') | local table = html:tag('table') | ||
: | :attr('class', classAttr) | ||
:attr('style', finalStyle) | |||
-- Add custom ID if provided | -- Add custom ID if provided | ||
Line 55: | Line 63: | ||
end | end | ||
-- Create the header row (th automatically becomes the collapsible toggle) | |||
-- Create the header row | |||
local headerRow = table:tag('tr') | local headerRow = table:tag('tr') | ||
local headerCell = headerRow:tag('th') | local headerCell = headerRow:tag('th') | ||
:wikitext(header) | :wikitext(header) | ||
-- Create the content row | -- Create the content row - single td containing everything | ||
local contentRow = table:tag('tr') | local contentRow = table:tag('tr') | ||
local contentCell = contentRow:tag('td') | local contentCell = contentRow:tag('td') | ||
-- Handle the content - preserve it as-is to avoid parser interference | -- Handle the content - preserve it as-is to avoid parser interference | ||
if content ~= "" then | if content ~= "" then | ||
contentCell:wikitext(content) | |||
contentCell:wikitext( | |||
end | end | ||
Line 91: | Line 91: | ||
local collapsed = args.collapsed or "yes" | local collapsed = args.collapsed or "yes" | ||
-- Build CSS classes | -- Build CSS classes to match original | ||
local cssClasses = {"mw-collapsible"} | local cssClasses = {"mw-collapsible", "mw-collapsed", "wikitable"} | ||
if collapsed == " | if collapsed == "no" or collapsed == "false" or collapsed == "0" then | ||
cssClasses = {"mw-collapsible", "wikitable"} | |||
end | end | ||
Line 103: | Line 103: | ||
local classAttr = table.concat(cssClasses, " ") | 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 | -- Build HTML | ||
local html = mw.html.create() | local html = mw.html.create() | ||
local table = html:tag('table'): | local table = html:tag('table') | ||
:attr('class', classAttr) | |||
:attr('style', finalStyle) | |||
if id ~= "" then | if id ~= "" then | ||
Line 112: | Line 121: | ||
end | end | ||
-- Header row - th becomes collapsible toggle automatically | |||
table:tag('tr'):tag('th'):wikitext(mw.text.trim(header)) | |||
-- Header | |||
table:tag('tr'):tag('th') | |||
-- Content | -- Content row - single td | ||
table:tag('tr'):tag('td') | table:tag('tr'):tag('td'):wikitext(content) | ||
return tostring(html) | return tostring(html) |
Latest revision as of 00:23, 22 July 2025
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