Module:Message box: Difference between revisions

spread comment over two lines, as modules do not wrap lines
(Undo revision 896901 by 86.135.251.105 (talk))
Enwikipedia>AGK
(spread comment over two lines, as modules do not wrap lines)
Line 1:
-- This is a meta-module for producing message box templates, including {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}.
-- {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}.
 
-- Require necessary modules.
local getArgs = require('Module:Arguments').getArgs
Line 6 ⟶ 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 18 ⟶ 19:
local tconcat = table.concat
local trim = mw.text.trim
 
--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------
 
local function getTitleObject(page, ...)
if type(page) == 'string' then
Line 33 ⟶ 34:
end
end
 
local function union(t1, t2)
-- Returns the union of two arrays.
Line 50 ⟶ 51:
return ret
end
 
local function getArgNums(args, prefix)
local nums = {}
Line 62 ⟶ 63:
return nums
end
 
--------------------------------------------------------------------------------
-- Box class definition
--------------------------------------------------------------------------------
 
local box = {}
box.__index = box
 
function box.new()
local obj = {}
Line 75 ⟶ 76:
return obj
end
 
function box.getNamespaceId(ns)
if not ns then return end
Line 89 ⟶ 90:
end
end
 
function box.getMboxType(nsid)
-- Gets the mbox type from a namespace number.
Line 107 ⟶ 108:
end
end
 
function box:addCat(ns, cat, sort)
if type(cat) ~= 'string' then return end
Line 130 ⟶ 131:
end
end
 
function box:addClass(class)
if type(class) ~= 'string' then return end
Line 136 ⟶ 137:
tinsert(self.classes, class)
end
 
function box:setTitle(args)
-- Get the title object and the namespace.
Line 144 ⟶ 145:
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 161 ⟶ 162:
return cfg
end
 
function box:removeBlankArgs(cfg, args)
-- Only allow blank arguments for the parameter names listed in cfg.allowBlankParams.
Line 175 ⟶ 176:
return newArgs
end
 
function box:setBoxParameters(cfg, args)
-- Get type data.
Line 184 ⟶ 185:
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 200 ⟶ 201:
self.isSmall = false
end
 
-- Add attributes, classes and styles.
if cfg.allowId then
Line 218 ⟶ 219:
self:addClass(args.class)
self.style = args.style
 
-- 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 and cfg.templateCategoryRequireName are set.
Line 234 ⟶ 235:
self.isTemplatePage = self.templateTitle and mw.title.equals(self.title, self.templateTitle) or false
end
 
-- Process data for collapsible text fields. At the moment these are only used in {{ambox}}.
if self.useCollapsibleTextFields then
Line 257 ⟶ 258:
self.issue = tconcat(issues, ' ')
end
 
-- Get the self.talk value.
local talk = args.talk
Line 287 ⟶ 288:
end
end
 
-- Get other values.
self.fix = args.fix ~= '' and args.fix or nil
Line 301 ⟶ 302:
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 309 ⟶ 310:
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 319 ⟶ 320:
self.imageEmptyCellStyle = 'border:none;padding:0px;width:1px'
end
 
-- Left image settings.
local imageLeft = self.isSmall and args.smallimage or args.image
Line 331 ⟶ 332:
end
end
 
-- Right image settings.
local imageRight = self.isSmall and args.smallimageright or args.imageright
Line 337 ⟶ 338:
self.imageRight = imageRight
end
 
-- Add mainspace categories. At the moment these are only used in {{ambox}}.
if cfg.allowMainspaceCategories then
Line 378 ⟶ 379:
end
end
 
-- Add template-namespace categories.
if cfg.templateCategory then
Line 389 ⟶ 390:
end
end
 
-- Add template error category.
if cfg.templateErrorCategory then
Line 415 ⟶ 416:
self:addCat('template', templateCat, templateSort)
end
 
-- Categories for all namespaces.
if self.invalidTypeError then
Line 424 ⟶ 425:
self:addCat('all', 'Pages with incorrectly substituted templates')
end
 
-- Convert category tables to strings and pass them through [[Module:Category handler]].
self.categories = categoryHandler{
Line 435 ⟶ 436:
}
end
 
function box:export()
local root = htmlBuilder.create()
 
-- Add the subst check error.
if self.isSubstituted and self.name then
Line 449 ⟶ 450:
))
end
 
-- Create the box table.
local boxTable = root.tag('table')
Line 461 ⟶ 462:
.cssText(self.style)
.attr('role', 'presentation')
 
-- Add the left-hand image.
local row = boxTable.tag('tr')
Line 483 ⟶ 484:
.cssText(self.imageEmptyCellStyle)
end
 
-- Add the text.
local textCell = row.tag('td').addClass('mbox-text')
Line 516 ⟶ 517:
.wikitext(self.text)
end
 
-- Add the right-hand image.
if self.imageRight then
Line 526 ⟶ 527:
.wikitext(self.imageRight)
end
 
-- Add the below row.
if self.below then
Line 536 ⟶ 537:
.wikitext(self.below)
end
 
-- Add error message for invalid type parameters.
if self.invalidTypeError then
Line 544 ⟶ 545:
.wikitext(format('This message box is using an invalid "type=%s" parameter and needs fixing.', self.type or ''))
end
 
-- Add categories.
root
.wikitext(self.categories)
 
return tostring(root)
end
 
local function main(boxType, args)
local outputBox = box.new()
Line 560 ⟶ 561:
return outputBox:export()
end
 
local function makeWrapper(boxType)
return function (frame)
Line 567 ⟶ 568:
end
end
 
local p = {
main = main,
mbox = makeWrapper('mbox')
}
 
for boxType in pairs(cfgTables) do
p[boxType] = makeWrapper(boxType)
end
 
return p
Anonymous user