Difference between revisions of "Module:Message box"
(start work on a module for displaying Template:Mbox-family message boxes) |
(more abstraction for box structure and better small=yes support) |
||
Line 4: | Line 4: | ||
local p = {} | local p = {} | ||
+ | |||
+ | local function generateBoxStructure() | ||
+ | local root = htmlBuilder.create() -- Includes error messages and categories added after the box. | ||
+ | local box = root.tag('table') | ||
+ | local row = box.tag('tr') | ||
+ | return root, box, row | ||
+ | end | ||
function p.build(data, args) | function p.build(data, args) | ||
− | -- Process config data | + | -- Process config data. |
local isSmall = args.small == 'yes' or args.small == true | local isSmall = args.small == 'yes' or args.small == true | ||
local typeData = data.types[args.type] | local typeData = data.types[args.type] | ||
local invalidType = args.type and not typeData and true or false | local invalidType = args.type and not typeData and true or false | ||
typeData = typeData or data.types[data.default] | typeData = typeData or data.types[data.default] | ||
+ | |||
+ | local image, imageRight, text, imageSize | ||
+ | if isSmall then | ||
+ | image = args.smallimage or args.image | ||
+ | imageRight = args.smallimageright or args.imageright | ||
+ | text = args.smalltext or args.text | ||
+ | imageSize = data.imageSizeSmall or data.imageSize | ||
+ | else | ||
+ | image = args.image | ||
+ | imageRight = args.imageright | ||
+ | text = args.text | ||
+ | imageSize = data.imageSizeLarge or data.imageSize | ||
+ | end | ||
+ | |||
+ | -- Get the box structure. | ||
+ | local root, box, row = generateBoxStructure() | ||
-- Build the box. | -- Build the box. | ||
− | |||
− | |||
box | box | ||
.attr('id', args.id) | .attr('id', args.id) | ||
Line 28: | Line 49: | ||
-- Add the left-hand image. | -- Add the left-hand image. | ||
− | + | if image ~= 'none' then | |
− | if | ||
row.tag('td') | row.tag('td') | ||
.addClass('mbox-image') | .addClass('mbox-image') | ||
− | .wikitext( | + | .wikitext(image or mw.ustring.format('[[File:%s|%s|link=|alt=]]', typeData.image, imageSize)) |
− | |||
− | |||
− | |||
− | |||
elseif data.imageEmptyCell then | elseif data.imageEmptyCell then | ||
row.tag('td') | row.tag('td') | ||
Line 46: | Line 62: | ||
.addClass('mbox-text') | .addClass('mbox-text') | ||
.cssText(args.textstyle) | .cssText(args.textstyle) | ||
− | .wikitext( | + | .wikitext(text) |
-- Add the right-hand image. | -- Add the right-hand image. | ||
− | if | + | if imageRight then |
row.tag('td') | row.tag('td') | ||
.addClass('mbox-imageright') | .addClass('mbox-imageright') | ||
− | .wikitext( | + | .wikitext(imageRight) |
end | end | ||
Revision as of 15:27, 19 September 2013
Lua error in package.lua at line 80: module 'Module:HtmlBuilder' not found. Lua error in package.lua at line 80: module 'Module:HtmlBuilder' not found. Lua error in package.lua at line 80: module 'Module:HtmlBuilder' not found.
- REDIRECT Template:Intricate template
This is a meta-module that implements the message box templates {{mbox}}, {{ambox}}, {{cmbox}}, {{fmbox}}, {{imbox}}, {{ombox}}, and {{tmbox}}. It is intended to be used from Lua modules, and should not be used directly from wiki pages. If you want to use this module's functionality from a wiki page, please use the individual message box templates instead.
Usage
To use this module from another Lua module, first you need to load it.
<source lang="lua"> local messageBox = require('Module:Message box') </source>
To create a message box, use the main
function. It takes two parameters: the first is the box type (as a string), and the second is a table containing the message box parameters.
<source lang="lua"> local box = messageBox.main( boxType, {
param1 = param1, param2 = param2, -- More parameters...
}) </source>
There are seven available box types:
Box type | Template | Purpose |
---|---|---|
mbox |
{{mbox}} | For message boxes to be used in multiple namespaces |
ambox |
{{ambox}} | For article message boxes |
cmbox |
{{cmbox}} | For category message boxes |
fmbox |
{{fmbox}} | For interface message boxes |
imbox |
{{imbox}} | For file namespace message boxes |
tmbox |
{{tmbox}} | For talk page message boxes |
ombox |
{{ombox}} | For message boxes in other namespaces |
See the template page of each box type for the available parameters.
Usage from #invoke
As well as the main
function, this module has separate functions for each box type. They are accessed using the code {{#invoke:Message box|mbox|...}}
, {{#invoke:Message box|ambox|...}}
, etc. These will work when called from other modules, but they access code used to process arguments passed from #invoke, and so calling them will be less efficient than calling main
.
Technical details
The module uses the same basic code for each of the templates listed above; the differences between each of them are configured using the data at Module:Message box/configuration. Here are the various configuration options and what they mean:
types
- a table containing data used by the type parameter of the message box. The table keys are the values that can be passed to the type parameter, and the table values are tables containing the class and the image used by that type.default
- the type to use if no value was passed to the type parameter, or if an invalid value was specified.showInvalidTypeError
- whether to show an error if the value passed to the type parameter was invalid.allowBlankParams
- usually blank values are stripped from parameters passed to the module. However, whitespace is preserved for the parameters included in the allowBlankParams table.allowSmall
- whether a small version of the message box can be produced with "small=yes".smallParam
- a custom name for the small parameter. For example, if set to "left" you can produce a small message box using "small=left".smallClass
- the class to use for small message boxes.substCheck
- whether to perform a subst check or not.classes
- an array of classes to use with the message box.imageEmptyCell
- whether to use an empty<td>...</td>
cell if there is no image set. This is used to preserve spacing for message boxes with a width of less than 100% of the screen.imageEmptyCellStyle
- whether empty image cells should be styled.imageCheckBlank
- whether "image=blank" results in no image being displayed.imageSmallSize
- usually, images used in small message boxes are set to 30x30px. This sets a custom size.imageCellDiv
- whether to enclose the image in a div enforcing a maximum image size.useCollapsibleTextFields
- whether to use text fields that can be collapsed, i.e. "issue", "fix", "talk", etc. Currently only used in ambox.imageRightNone
- whether imageright=none results in no image being displayed on the right-hand side of the message box.sectionDefault
- the default name for the "section" parameter. Depends onuseCollapsibleTextFields
.allowMainspaceCategories
- allow categorisation in the main namespace.templateCategory
- the name of a category to be placed on the template page.templateCategoryRequireName
- whether thename
parameter is required to display the template category.templateErrorCategory
- the name of the error category to be used on the template page.templateErrorParamsToCheck
- an array of parameter names to check. If any are absent, thetemplateErrorCategory
is applied to the template page.
-- This is a meta-module for producing message box templates, including {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}. local htmlBuilder = require('Module:HtmlBuilder') local p = {} local function generateBoxStructure() local root = htmlBuilder.create() -- Includes error messages and categories added after the box. local box = root.tag('table') local row = box.tag('tr') return root, box, row end function p.build(data, args) -- Process config data. local isSmall = args.small == 'yes' or args.small == true local typeData = data.types[args.type] local invalidType = args.type and not typeData and true or false typeData = typeData or data.types[data.default] local image, imageRight, text, imageSize if isSmall then image = args.smallimage or args.image imageRight = args.smallimageright or args.imageright text = args.smalltext or args.text imageSize = data.imageSizeSmall or data.imageSize else image = args.image imageRight = args.imageright text = args.text imageSize = data.imageSizeLarge or data.imageSize end -- Get the box structure. local root, box, row = generateBoxStructure() -- Build the box. box .attr('id', args.id) for i, class in ipairs(data.classes) do box .addClass(class) end box .addClass(typeData.class) .addClass(args.class) .cssText(args.style) .attr('role', 'presentation') -- Add the left-hand image. if image ~= 'none' then row.tag('td') .addClass('mbox-image') .wikitext(image or mw.ustring.format('[[File:%s|%s|link=|alt=]]', typeData.image, imageSize)) elseif data.imageEmptyCell then row.tag('td') .addClass('mbox-empty-cell') end -- Add the text. row.tag('td') .addClass('mbox-text') .cssText(args.textstyle) .wikitext(text) -- Add the right-hand image. if imageRight then row.tag('td') .addClass('mbox-imageright') .wikitext(imageRight) end -- Add error messages and categories. if invalidType then local title = mw.title.getCurrentTitle() local catsort = (title.namespace == 0 and 'Main:' or '') .. title.prefixedText root.tag('div') .css('text-align', 'center') .wikitext(mw.ustring.format('This message box is using an invalid "type=%s" parameter and needs fixing.', args.type or '')) .done() .wikitext(mw.ustring.format('[[Category:Wikipedia message box parameter needs fixing|%s]]', catsort)) end return tostring(root) end function p._fmbox(args) local data = {} data.types = { warning = { class = 'fmbox-warning', image = 'Cmbox deletion.png' }, editnotice = { class = 'fmbox-editnotice', image = 'Imbox notice.png' }, system = { class = 'fmbox-system', image = 'Imbox notice.png' } } data.default = 'system' data.classes = { 'plainlinks', 'fmbox' } data.imageSize = '40x40px' data.imageEmptyCell = false return p.build(data, args) end function p._ombox(args) local data = {} data.types = { speedy = { class = 'ombox-speedy', image = 'Imbox speedy deletion.png' }, delete = { class = 'ombox-delete', image = 'Imbox deletion.png' }, content = { class = 'ombox-content', image = 'Imbox content.png' }, style = { class = 'ombox-style', image = 'Edit-clear.svg' }, move = { class = 'ombox-move', image = 'Imbox move.png' }, protection = { class = 'ombox-protection', image = 'Imbox protection.png' }, notice = { class = 'ombox-notice', image = 'Imbox notice.png' } } data.default = 'notice' data.classes = {'plainlinks', 'ombox'} data.imageSizeLarge = '40x40px' data.imageSizeSmall = '30x30px' data.imageEmptyCell = true return p.build(data, args) end local function makeWrapper(func) return function (frame) -- If called via #invoke, use the args passed into the invoking -- template, or the args passed to #invoke if any exist. Otherwise -- assume args are being passed directly in from the debug console -- or from another Lua module. local origArgs if frame == mw.getCurrentFrame() then origArgs = frame:getParent().args for k, v in pairs(frame.args) do origArgs = frame.args break end else origArgs = frame end -- Trim whitespace and remove blank arguments. local args = {} for k, v in pairs(origArgs) do if type(v) == 'string' then v = mw.text.trim(v) end if v ~= '' then args[k] = v end end return func(args) end end p.fmbox = makeWrapper(p._fmbox) p.ombox = makeWrapper(p._ombox) return p