Module:Message box: Difference between revisions

no edit summary
(Allow passing attributes per protected edit request by User:Jackmcbarn. Also remove undocumented "hidden" parameter and format the module so it mostly fits within 80 chars.)
No edit summary
Line 1:
-- This is a meta-module for producing message box templates, including
-- {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}.
 
-- Require necessary modules.
local getArgs = require('Module:Arguments').getArgs
Line 7:
local categoryHandler = require('Module:Category handler').main
local yesno = require('Module:Yesno')
 
-- Load the configuration page.
local cfgTables = mw.loadData('Module:Message box/configuration')
 
-- Get a language object for formatDate and ucfirst.
local lang = mw.language.getContentLanguage()
 
-- Set aliases for often-used functions to reduce table lookups.
local format = mw.ustring.format
Line 19:
local tconcat = table.concat
local trim = mw.text.trim
 
--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------
 
local function getTitleObject(page, ...)
if type(page) == 'string' then
Line 34:
end
end
 
local function union(t1, t2)
-- Returns the union of two arrays.
Line 51:
return ret
end
 
local function getArgNums(args, prefix)
local nums = {}
Line 63:
return nums
end
 
--------------------------------------------------------------------------------
-- Box class definition
--------------------------------------------------------------------------------
 
local box = {}
box.__index = box
 
function box.new()
local obj = {}
Line 76:
return obj
end
 
function box.getNamespaceId(ns)
if not ns then return end
Line 90:
end
end
 
function box.getMboxType(nsid)
-- Gets the mbox type from a namespace number.
Line 108:
end
end
 
function box:addCat(ns, cat, sort)
if type(cat) ~= 'string' then return end
Line 134:
end
end
 
function box:addClass(class)
if type(class) ~= 'string' then return end
Line 140:
tinsert(self.classes, class)
end
 
function box:addAttr(attr, val)
if type(attr) ~= 'string' or type(val) ~= 'string' then return end
Line 146:
tinsert(self.attrs, attr)
end
 
function box:setTitle(args)
-- Get the title object and the namespace.
Line 154:
self.nsid = box.getNamespaceId(self.demospace) or self.title.namespace
end
 
function box:getConfig(boxType)
-- Get the box config data from the data page.
Line 175:
return cfg
end
 
function box:removeBlankArgs(cfg, args)
-- Only allow blank arguments for the parameter names listed in
Line 190:
return newArgs
end
 
function box:setBoxParameters(cfg, args)
-- Get type data.
Line 203:
self.typeClass = typeData.class
self.typeImage = typeData.image
 
-- Find if the box has been wrongly substituted.
if cfg.substCheck and args.subst == 'SUBST' then
self.isSubstituted = true
end
 
-- Find whether we are using a small message box.
if cfg.allowSmall and (
Line 219:
self.isSmall = false
end
 
-- Add attributes, classes and styles.
if cfg.allowId then
Line 237:
self.style = args.style
self.attrs = args.attrs
 
-- Set text style.
self.textstyle = args.textstyle
 
-- Find if we are on the template page or not. This functionality is only
-- used if useCollapsibleTextFields is set, or if both cfg.templateCategory
Line 262:
or false
end
 
-- Process data for collapsible text fields. At the moment these are only
-- used in {{ambox}}.
Line 286:
self.issue = tconcat(issues, ' ')
end
 
-- Get the self.talk value.
local talk = args.talk
Line 335:
end
end
 
-- Get other values.
self.fix = args.fix ~= '' and args.fix or nil
Line 349:
self.info = args.info
end
 
-- Set the non-collapsible text field. At the moment this is used by all box
-- types other than ambox, and also by ambox when small=yes.
Line 357:
self.text = args.text
end
 
-- Set the below row.
self.below = cfg.below and args.below
 
-- General image settings.
self.imageCellDiv = not self.isSmall and cfg.imageCellDiv and true or false
Line 367:
self.imageEmptyCellStyle = 'border:none;padding:0px;width:1px'
end
 
-- Left image settings.
local imageLeft = self.isSmall and args.smallimage or args.image
Line 382:
end
end
 
-- Right image settings.
local imageRight = self.isSmall and args.smallimageright or args.imageright
Line 388:
self.imageRight = imageRight
end
 
-- Add mainspace categories. At the moment these are only used in {{ambox}}.
if cfg.allowMainspaceCategories then
Line 433:
end
end
 
-- Add template-namespace categories.
if cfg.templateCategory then
Line 444:
end
end
 
-- Add template error category.
if cfg.templateErrorCategory then
Line 470:
self:addCat('template', templateCat, templateSort)
end
 
-- Categories for all namespaces.
if self.invalidTypeError then
Line 479:
self:addCat('all', 'Pages with incorrectly substituted templates')
end
 
-- Convert category tables to strings and pass them through
-- [[Module:Category handler]].
Line 491:
}
end
 
function box:export()
local root = htmlBuilder.create()
 
-- Add the subst check error.
if self.isSubstituted and self.name then
Line 505:
))
end
 
-- Create the box table.
local boxTable = root.tag('table')
Line 521:
.attr(attr, val)
end
 
-- Add the left-hand image.
local row = boxTable.tag('tr')
Line 544:
.cssText(self.imageEmptyCellStyle)
end
 
-- Add the text.
local textCell = row.tag('td').addClass('mbox-text')
Line 577:
.wikitext(self.text)
end
 
-- Add the right-hand image.
if self.imageRight then
Line 589:
.wikitext(self.imageRight)
end
 
-- Add the below row.
if self.below then
Line 599:
.wikitext(self.below)
end
 
-- Add error message for invalid type parameters.
if self.invalidTypeError then
Line 610:
))
end
 
-- Add categories.
root
.wikitext(self.categories)
 
return tostring(root)
end
 
local function main(boxType, args)
local outputBox = box.new()
Line 626:
return outputBox:export()
end
 
local function makeWrapper(boxType)
return function (frame)
Line 633:
end
end
 
local p = {
main = main,
mbox = makeWrapper('mbox')
}
 
for boxType in pairs(cfgTables) do
p[boxType] = makeWrapper(boxType)
end
 
return p
Anonymous user