Diferencia entre revisiones de «Módulo:Side box»
De Familia Sanchez Arjona
(create Lua version of Template:Side box) |
m (10 revisiones importadas) |
||
(No se muestran 9 ediciones intermedias de otro usuario) | |||
Línea 1: | Línea 1: | ||
-- This module implements {{side box}}. | -- This module implements {{side box}}. | ||
+ | |||
+ | local yesno = require('Module:Yesno') | ||
local p = {} | local p = {} | ||
Línea 25: | Línea 27: | ||
-- Main table classes | -- Main table classes | ||
data.classes = {} | data.classes = {} | ||
− | if args.metadata | + | if yesno(args.metadata) ~= false then |
table.insert(data.classes, 'metadata') | table.insert(data.classes, 'metadata') | ||
end | end | ||
Línea 34: | Línea 36: | ||
end | end | ||
table.insert(data.classes, args.class) | table.insert(data.classes, args.class) | ||
+ | |||
+ | -- Image | ||
+ | if args.image and args.image ~= 'none' then | ||
+ | data.image = args.image | ||
+ | end | ||
-- Copy over data that doesn't need adjusting | -- Copy over data that doesn't need adjusting | ||
Línea 47: | Línea 54: | ||
-- Body row | -- Body row | ||
'text', | 'text', | ||
− | |||
'imageright', | 'imageright', | ||
Línea 75: | Línea 81: | ||
-- The "above" row | -- The "above" row | ||
if data.above then | if data.above then | ||
− | local aboveCell = root:tag('tr'):tag('td') | + | local aboveCell = root:newline():tag('tr'):tag('td') |
aboveCell | aboveCell | ||
:attr('colspan', data.imageright and 3 or 2) | :attr('colspan', data.imageright and 3 or 2) | ||
Línea 85: | Línea 91: | ||
aboveCell:cssText(data.abovestyle) | aboveCell:cssText(data.abovestyle) | ||
end | end | ||
− | aboveCell:wikitext(data.above) | + | aboveCell |
+ | :newline() | ||
+ | :wikitext(data.above) | ||
end | end | ||
-- The body row | -- The body row | ||
− | local bodyRow = root:tag('tr') | + | local bodyRow = root:newline():tag('tr'):newline() |
if data.image then | if data.image then | ||
bodyRow:tag('td') | bodyRow:tag('td') | ||
Línea 97: | Línea 105: | ||
bodyRow:tag('td'):css('width', '1px') | bodyRow:tag('td'):css('width', '1px') | ||
end | end | ||
− | local textCell = bodyRow:tag('td') | + | local textCell = bodyRow:newline():tag('td') |
textCell:addClass('mbox-text plainlist') | textCell:addClass('mbox-text plainlist') | ||
if data.textstyle then | if data.textstyle then | ||
Línea 104: | Línea 112: | ||
textCell:wikitext(data.text) | textCell:wikitext(data.text) | ||
if data.imageright then | if data.imageright then | ||
− | bodyRow:tag('td') | + | bodyRow:newline():tag('td') |
:addClass('mbox-imageright') | :addClass('mbox-imageright') | ||
:wikitext(data.imageright) | :wikitext(data.imageright) | ||
Línea 111: | Línea 119: | ||
-- The below row | -- The below row | ||
if data.below then | if data.below then | ||
− | local belowCell = root:tag('tr'):tag('td') | + | local belowCell = root:newline():tag('tr'):tag('td') |
belowCell | belowCell | ||
:attr('colspan', data.imageright and 3 or 2) | :attr('colspan', data.imageright and 3 or 2) |
Revisión actual del 14:29 31 may 2016
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.
<source lang="lua"> local mSideBox = require('Module:Side box') </source>
You can then generate a side box using the _main function.
<source lang="lua"> mSideBox._main(args) </source>
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.
-- This module implements {{side box}}. local yesno = require('Module:Yesno') local p = {} 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 function p._main(args) local data = p.makeData(args) return p.renderSidebox(data) end function p.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, 'mbox-small-left') else table.insert(data.classes, 'mbox-small') 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 doesn't need adjusting local argsToCopy = { -- Styles 'style', 'textstyle', -- 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 function p.renderSidebox(data) -- Renders the sidebox HTML. -- Table root local root = mw.html.create('table') for i, class in ipairs(data.classes or {}) do root:addClass(class) end root:css{border = '1px solid #aaa', ['background-color'] = '#f9f9f9'} if data.style then root:cssText(data.style) end -- The "above" row if data.above then local aboveCell = root:newline():tag('tr'):tag('td') aboveCell :attr('colspan', data.imageright and 3 or 2) :addClass('mbox-text') if data.textstyle then aboveCell:cssText(data.textstyle) end if data.abovestyle then aboveCell:cssText(data.abovestyle) end aboveCell :newline() :wikitext(data.above) end -- The body row local bodyRow = root:newline():tag('tr'):newline() if data.image then bodyRow:tag('td') :addClass('mbox-image') :wikitext(data.image) else bodyRow:tag('td'):css('width', '1px') end local textCell = bodyRow:newline():tag('td') textCell:addClass('mbox-text plainlist') if data.textstyle then textCell:cssText(data.textstyle) end textCell:wikitext(data.text) if data.imageright then bodyRow:newline():tag('td') :addClass('mbox-imageright') :wikitext(data.imageright) end -- The below row if data.below then local belowCell = root:newline():tag('tr'):tag('td') belowCell :attr('colspan', data.imageright and 3 or 2) :addClass('mbox-text') if data.textstyle then belowCell:cssText(data.textstyle) end belowCell:wikitext(data.below) end return tostring(root) end return p