模块:Side box:修订间差异
小 已保护“模块:Side box”:高风险模板([编辑=仅管理员](无限期)[移动=仅管理员](无限期)) |
-mbox, templatestyled, table -> div |
||
| 第1行: | 第1行: | ||
local yesno = require('Module:Yesno') | local yesno = require('Module:Yesno') | ||
local p = {} | local p = {} | ||
local function makeData(args) | |||
function | |||
local data = {} | local data = {} | ||
| 第31行: | 第11行: | ||
end | end | ||
if args.position and args.position:lower() == 'left' then | if args.position and args.position:lower() == 'left' then | ||
table.insert(data.classes, ' | table.insert(data.classes, 'side-box-left') | ||
else | else | ||
table.insert(data.classes, ' | table.insert(data.classes, 'side-box-right') | ||
end | end | ||
if args.collapsible then | |||
table.insert(data.classes, 'mw-collapsible') | |||
if args.collapsible == "collapsed" then | |||
table.insert(data.classes, 'mw-collapsed') | |||
end | |||
data.collapsible = true | |||
end | |||
table.insert(data.classes, args.class) | table.insert(data.classes, args.class) | ||
| 第42行: | 第31行: | ||
end | end | ||
-- Copy over data that | -- Copy over data that does not need adjusting | ||
local argsToCopy = { | local argsToCopy = { | ||
-- aria qualities | |||
'role', | |||
'labelledby', | |||
-- Classes | |||
'textclass', | |||
-- Styles | -- Styles | ||
'style', | 'style', | ||
'textstyle', | 'textstyle', | ||
'templatestyles', | |||
-- Above row | -- Above row | ||
| 第66行: | 第64行: | ||
end | end | ||
function | local function renderSidebox(data) | ||
-- Renders the sidebox HTML. | -- Renders the sidebox HTML. | ||
-- Table root | -- Table root | ||
local root = mw.html.create(' | local root = mw.html.create('div') | ||
root:attr('role', data.role) | |||
:attr('aria-labelledby', data.labelledby) | |||
: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) | ||
| 第81行: | 第81行: | ||
-- The "above" row | -- The "above" row | ||
if data.above then | if data.above then | ||
local | local above = root:newline():tag('div') | ||
above:addClass('side-box-abovebelow') | |||
: | :newline() | ||
: | :wikitext(data.above) | ||
if data.textstyle then | if data.textstyle then | ||
above:cssText(data.textstyle) | |||
end | end | ||
if data.abovestyle then | if data.abovestyle then | ||
above:cssText(data.abovestyle) | |||
end | end | ||
end | end | ||
-- The body row | -- The body row | ||
local | local body = root:newline():tag('div') | ||
body:addClass('side-box-flex') | |||
:addClass(data.collapsible and 'mw-collapsible-content') | |||
:newline() | |||
if data.image then | if data.image then | ||
body:tag('div') | |||
:addClass(' | :addClass('side-box-image') | ||
:wikitext(data.image) | :wikitext(data.image) | ||
end | end | ||
local | local text = body:newline():tag('div') | ||
text:addClass('side-box-text') | |||
:addClass(data.textclass or 'plainlist') | |||
if data.textstyle then | if data.textstyle then | ||
text:cssText(data.textstyle) | |||
end | end | ||
text:wikitext(data.text) | |||
if data.imageright then | if data.imageright then | ||
body:newline():tag('div') | |||
:addClass(' | :addClass('side-box-imageright') | ||
:wikitext(data.imageright) | :wikitext(data.imageright) | ||
end | end | ||
| 第119行: | 第118行: | ||
-- The below row | -- The below row | ||
if data.below then | if data.below then | ||
local | local below = root:newline():tag('div') | ||
below | |||
: | :addClass('side-box-abovebelow') | ||
: | :wikitext(data.below) | ||
if data.textstyle then | if data.textstyle then | ||
below:cssText(data.textstyle) | |||
end | end | ||
end | end | ||
return tostring(root) | root:newline() | ||
local frame = mw.getCurrentFrame() | |||
local templatestyles = '' | |||
if data.templatestyles then | |||
templatestyles = frame:extensionTag{ | |||
name = 'templatestyles', args = { src = data.templatestyles } | |||
} | |||
end | |||
return frame:extensionTag{ | |||
name = 'templatestyles', args = { src = 'Module:Side box/styles.css' } | |||
} .. templatestyles .. tostring(root) | |||
end | |||
function p._main(args) | |||
local data = makeData(args) | |||
return renderSidebox(data) | |||
end | |||
function p.main(frame) | |||
local origArgs = frame:getParent().args | |||
local args = {} | |||
for k, v in pairs(origArgs) do | |||
v = v:match('%s*(.-)%s*$') | |||
if v ~= '' then | |||
args[k] = v | |||
end | |||
end | |||
return p._main(args) | |||
end | end | ||
return p | return p | ||
2022年7月8日 (五) 01:00的版本
<templatestyles src="Module:Message box/ombox.css"></templatestyles>
| Protected | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
<templatestyles src="Module:Message box/ombox.css"></templatestyles>
| 文件:Lua-logo-nolabel.svg | 此-{zh-hans:模块; zh-hant:模組;}-使用Lua语言: |
模块:TNT第159行Lua错误:Missing JsonConfig extension; Cannot load https://commons.wikimedia.org/wiki/Data:I18n/Uses TemplateStyles.tab This module implements the {{side box}} template.
Usage from wikitext
This module cannot be used directly from wikitext. It can only be used through the {{side box}} template. Please see the template page for documentation.
Usage from Lua modules
To use this module from other Lua modules, first load the module.
<syntaxhighlight lang="lua"> local mSideBox = require('Module:Side box') </syntaxhighlight>
You can then generate a side box using the _main function.
<syntaxhighlight lang="lua"> mSideBox._main(args) </syntaxhighlight>
The args variable should be a table containing the arguments to pass to the module. To see the different arguments that can be specified and how they affect the module output, please refer to the {{side box}} template documentation.
local yesno = require('Module:Yesno')
local p = {}
local function makeData(args)
local data = {}
-- Main table classes
data.classes = {}
if yesno(args.metadata) ~= false then
table.insert(data.classes, 'metadata')
end
if args.position and args.position:lower() == 'left' then
table.insert(data.classes, 'side-box-left')
else
table.insert(data.classes, 'side-box-right')
end
if args.collapsible then
table.insert(data.classes, 'mw-collapsible')
if args.collapsible == "collapsed" then
table.insert(data.classes, 'mw-collapsed')
end
data.collapsible = true
end
table.insert(data.classes, args.class)
-- Image
if args.image and args.image ~= 'none' then
data.image = args.image
end
-- Copy over data that does not need adjusting
local argsToCopy = {
-- aria qualities
'role',
'labelledby',
-- Classes
'textclass',
-- Styles
'style',
'textstyle',
'templatestyles',
-- Above row
'above',
'abovestyle',
-- Body row
'text',
'imageright',
-- Below row
'below',
}
for i, key in ipairs(argsToCopy) do
data[key] = args[key]
end
return data
end
local function renderSidebox(data)
-- Renders the sidebox HTML.
-- Table root
local root = mw.html.create('div')
root:attr('role', data.role)
:attr('aria-labelledby', data.labelledby)
:addClass('side-box')
for i, class in ipairs(data.classes or {}) do
root:addClass(class)
end
if data.style then
root:cssText(data.style)
end
-- The "above" row
if data.above then
local above = root:newline():tag('div')
above:addClass('side-box-abovebelow')
:newline()
:wikitext(data.above)
if data.textstyle then
above:cssText(data.textstyle)
end
if data.abovestyle then
above:cssText(data.abovestyle)
end
end
-- The body row
local body = root:newline():tag('div')
body:addClass('side-box-flex')
:addClass(data.collapsible and 'mw-collapsible-content')
:newline()
if data.image then
body:tag('div')
:addClass('side-box-image')
:wikitext(data.image)
end
local text = body:newline():tag('div')
text:addClass('side-box-text')
:addClass(data.textclass or 'plainlist')
if data.textstyle then
text:cssText(data.textstyle)
end
text:wikitext(data.text)
if data.imageright then
body:newline():tag('div')
:addClass('side-box-imageright')
:wikitext(data.imageright)
end
-- The below row
if data.below then
local below = root:newline():tag('div')
below
:addClass('side-box-abovebelow')
:wikitext(data.below)
if data.textstyle then
below:cssText(data.textstyle)
end
end
root:newline()
local frame = mw.getCurrentFrame()
local templatestyles = ''
if data.templatestyles then
templatestyles = frame:extensionTag{
name = 'templatestyles', args = { src = data.templatestyles }
}
end
return frame:extensionTag{
name = 'templatestyles', args = { src = 'Module:Side box/styles.css' }
} .. templatestyles .. tostring(root)
end
function p._main(args)
local data = makeData(args)
return renderSidebox(data)
end
function p.main(frame)
local origArgs = frame:getParent().args
local args = {}
for k, v in pairs(origArgs) do
v = v:match('%s*(.-)%s*$')
if v ~= '' then
args[k] = v
end
end
return p._main(args)
end
return p