Diff: Module:Side box
Comparing revision #1 (2022-12-13 04:33:19) with revision #2 (2023-02-02 05:25:41).
| Old | New |
|---|---|
local yesno = require('Module:Yesno') |
local yesno = require('Module:Yesno') |
local p = {} |
local p = {} |
local function makeData(args) |
local function makeData(args) |
local data = {} |
local data = {} |
-- Main table classes |
-- Main table classes |
data.classes = {} |
data.classes = {} |
if yesno(args.metadata) ~= false then |
if yesno(args.metadata) ~= false then |
table.insert(data.classes, 'metadata') |
table.insert(data.classes, 'metadata') |
end |
end |
if args.position and args.position:lower() == 'left' then |
if args.position and args.position:lower() == 'left' then |
table.insert(data.classes, 'side-box-left') |
table.insert(data.classes, 'side-box-left') |
else |
else |
table.insert(data.classes, 'side-box-right') |
table.insert(data.classes, 'side-box-right') |
end |
end |
if args.collapsible then |
if args.collapsible then |
table.insert(data.classes, 'mw-collapsible') |
table.insert(data.classes, 'mw-collapsible') |
if args.collapsible == "collapsed" then |
if args.collapsible == "collapsed" then |
table.insert(data.classes, 'mw-collapsed') |
table.insert(data.classes, 'mw-collapsed') |
end |
end |
data.collapsible = true |
data.collapsible = true |
end |
end |
table.insert(data.classes, args.class) |
table.insert(data.classes, args.class) |
-- Image |
-- Image |
if args.image and args.image ~= 'none' then |
if args.image and args.image ~= 'none' then |
data.image = args.image |
data.image = args.image |
end |
end |
-- we have to check to see if a downstream use has plainlist like |
-- we have to check to see if a downstream use has plainlist like |
-- Template:Sister_project. also it's the default. wikitext is :( |
-- Template:Sister_project. also it's the default. wikitext is :( |
if args.textclass == 'plainlist' or not args.textclass then |
if args.textclass == 'plainlist' or not args.textclass then |
data.textclass = 'plainlist' |
data.textclass = 'plainlist' |
data.plainlist_templatestyles = 'Plainlist/styles.css' |
data.plainlist_templatestyles = 'Plainlist/styles.css' |
else |
else |
data.textclass = args.textclass |
data.textclass = args.textclass |
end |
end |
-- Copy over data that does not need adjusting |
-- Copy over data that does not need adjusting |
local argsToCopy = { |
local argsToCopy = { |
-- aria qualities |
-- aria qualities |
'role', |
'role', |
'labelledby', |
'labelledby', |
-- Styles |
-- Styles |
'style', |
'style', |
'textstyle', |
'textstyle', |
'templatestyles', |
'templatestyles', |
-- Above row |
-- Above row |
'above', |
'above', |
'abovestyle', |
'abovestyle', |
-- Body row |
-- Body row |
'text', |
'text', |
'imageright', |
'imageright', |
-- Below row |
-- Below row |
'below', |
'below', |
} |
} |
for i, key in ipairs(argsToCopy) do |
for i, key in ipairs(argsToCopy) do |
data[key] = args[key] |
data[key] = args[key] |
end |
end |
return data |
return data |
end |
end |
local function renderSidebox(data) |
local function renderSidebox(data) |
-- Renders the sidebox HTML. |
-- Renders the sidebox HTML. |
-- Table root |
-- Table root |
local root = mw.html.create('div') |
local root = mw.html.create('div') |
root:attr('role', data.role) |
root:attr('role', data.role) |
:attr('aria-labelledby', data.labelledby) |
:attr('aria-labelledby', data.labelledby) |
:addClass('side-box') |
:addClass('side-box') |
for i, class in ipairs(data.classes or {}) do |
for i, class in ipairs(data.classes or {}) do |
root:addClass(class) |
root:addClass(class) |
end |
end |
if data.style then |
if data.style then |
root:cssText(data.style) |
root:cssText(data.style) |
end |
end |
local frame = mw.getCurrentFrame() |
local frame = mw.getCurrentFrame() |
if data.plainlist_templatestyles then |
if data.plainlist_templatestyles then |
root:wikitext(frame:extensionTag{ |
root:wikitext(frame:extensionTag{ |
name = 'templatestyles', args = { src = data.plainlist_templatestyles } |
name = 'templatestyles', args = { src = data.plainlist_templatestyles } |
}) |
}) |
end |
end |
-- The "above" row |
-- The "above" row |
if data.above then |
if data.above then |
local above = root:newline():tag('div') |
local above = root:newline():tag('div') |
above:addClass('side-box-abovebelow') |
above:addClass('side-box-abovebelow') |
:newline() |
:newline() |
:wikitext(data.above) |
:wikitext(data.above) |
if data.textstyle then |
if data.textstyle then |
above:cssText(data.textstyle) |
above:cssText(data.textstyle) |
end |
end |
if data.abovestyle then |
if data.abovestyle then |
above:cssText(data.abovestyle) |
above:cssText(data.abovestyle) |
end |
end |
end |
end |
-- The body row |
-- The body row |
local body = root:newline():tag('div') |
local body = root:newline():tag('div') |
body:addClass('side-box-flex') |
body:addClass('side-box-flex') |
:addClass(data.collapsible and 'mw-collapsible-content') |
:addClass(data.collapsible and 'mw-collapsible-content') |
:newline() |
:newline() |
if data.image then |
if data.image then |
body:tag('div') |
body:tag('div') |
:addClass('side-box-image') |
:addClass('side-box-image') |
:wikitext(data.image) |
:wikitext(data.image) |
end |
end |
local text = body:newline():tag('div') |
local text = body:newline():tag('div') |
text:addClass('side-box-text') |
text:addClass('side-box-text') |
:addClass(data.textclass) |
:addClass(data.textclass) |
if data.textstyle then |
if data.textstyle then |
text:cssText(data.textstyle) |
text:cssText(data.textstyle) |
end |
end |
text:wikitext(data.text) |
text:wikitext(data.text) |
if data.imageright then |
if data.imageright then |
body:newline():tag('div') |
body:newline():tag('div') |
:addClass('side-box-imageright') |
:addClass('side-box-imageright') |
:wikitext(data.imageright) |
:wikitext(data.imageright) |
end |
end |
-- The below row |
-- The below row |
if data.below then |
if data.below then |
local below = root:newline():tag('div') |
local below = root:newline():tag('div') |
below |
below |
:addClass('side-box-abovebelow') |
:addClass('side-box-abovebelow') |
:wikitext(data.below) |
:wikitext(data.below) |
if data.textstyle then |
if data.textstyle then |
below:cssText(data.textstyle) |
below:cssText(data.textstyle) |
end |
end |
end |
end |
root:newline() |
root:newline() |
local templatestyles = '' |
local templatestyles = '' |
if data.templatestyles then |
if data.templatestyles then |
templatestyles = frame:extensionTag{ |
templatestyles = frame:extensionTag{ |
name = 'templatestyles', args = { src = data.templatestyles } |
name = 'templatestyles', args = { src = data.templatestyles } |
} |
} |
end |
end |
return frame:extensionTag{ |
return frame:extensionTag{ |
name = 'templatestyles', args = { src = 'Module:Side box/styles.css' } |
name = 'templatestyles', args = { src = 'Module:Side box/styles.css' } |
} .. templatestyles .. tostring(root) |
} .. templatestyles .. tostring(root) |
end |
end |
function p._main(args) |
function p._main(args) |
local data = makeData(args) |
local data = makeData(args) |
return renderSidebox(data) |
return renderSidebox(data) |
end |
end |
function p.main(frame) |
function p.main(frame) |
local origArgs = frame:getParent().args |
local origArgs = frame:getParent().args |
local args = {} |
local args = {} |
for k, v in pairs(origArgs) do |
for k, v in pairs(origArgs) do |
v = v:match('%s*(.-)%s*$') |
v = v:match('%s*(.-)%s*$') |
if v ~= '' then |
if v ~= '' then |
args[k] = v |
args[k] = v |
end |
end |
end |
end |
return p._main(args) |
return p._main(args) |
end |
end |
return p |
return p |