10,178
edits
Changes
m
local box = {}---------------------------------------------------------------------------------- Helper functions--------------------------------------------------------------------------------
-- Returns the union of two arrays. local vals = {} for i, v in ipairs(t1) do vals[v] = true end for i, v in ipairs(t2) do vals[v] = true end local ret = {} for k in pairs(vals) do tinsert table.insert(ret, k) end table.sort(ret) return ret
local nums = {} for k, v in pairs(args) do local num = mw.ustring.match(tostring(k), '^' .. prefix .. '([1-9]%d*)$') if num then tinsert table.insert(nums, tonumber(num)) end end table.sort(nums) return nums
function box:setTitle(args) -- Get the title object Add attributes, classes and the namespacestyles. self.pageTitle id = getTitleObject(args.page ~id self.name = '' and args.page)name if self.title = name then self:addClass('box-' .pageTitle or mw.titlestring.getCurrentTitlegsub(self.name,' ','_')) self.demospace = end if yesno(args.demospace plainlinks) ~= false then self:addClass('plainlinks' and args) end for _, class in ipairs(cfg.demospace classes or nil{}) do self:addClass(class) end if self.nsid = boxisSmall then self:addClass(cfg.getNamespaceIdsmallClass or 'mbox-small') end self:addClass(self.demospacetypeClass) self:addClass(args.class) or self.titlestyle = args.namespacestyleend self.attrs = args.attrs
function box:getConfig(boxType) -- Get the box config data from the data pageSet text style. if boxType == 'mbox' then boxType = box.getMboxType( self.nsid) end local cfgTables = mw.loadData('Module:Message box/configuration') local cfg = cfgTables[boxType] if not cfg then local boxTypes = {} for k, v in pairs(dataTables) do tinsert(boxTypes, format('"%s"', k)) end tinsert(boxTypes, '"mbox"') error(format('invalid message box type "%s"; valid types are %s', tostring(boxType), mw.text.listToText(boxTypes)), 2) end return cfgend function box:removeBlankArgs(cfg, args) -- Only allow blank arguments for the parameter names listed in cfg.allowBlankParams. local newArgs textstyle = {} for k, v in pairs(args) do if v ~= '' then newArgs[k] = v end end for i, param in ipairs(cfg.allowBlankParams or {}) do newArgs[param] = args[param] end return newArgsendtextstyle
function box -- 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. self.useCollapsibleTextFields = cfg.useCollapsibleTextFields if self.useCollapsibleTextFields or cfg.templateCategory and cfg.templateCategoryRequireName then if self.name then local templateName = mw.ustring.match( self.name, '^[tT][eE][mM][pP][lL][aA][tT][eE][%s_]*:setBoxParameters[%s_]*(cfg.*)$' ) or self.name templateName = 'Template:' .. templateName self.templateTitle = getTitleObject(templateName) end self.isTemplatePage = self.templateTitle and mw.title.equals(self.title, argsself.templateTitle) end -- Process data for collapsible text fields. At the moment these are only -- used in {{ambox}}. if self.useCollapsibleTextFields then -- Get type datathe self.issue value. if self.isSmall and args.smalltext then self.type issue = args.typesmalltext else local typeData sect if args.sect = = '' then sect = 'This ' .. (cfg.types[selfsectionDefault or 'page') elseif type(args.type]sect) == 'string' then self sect = 'This ' .. args.invalidTypeError sect end local issue = cfgargs.showInvalidTypeError issue issue = type(issue) == 'string' and selfissue ~= '' and issue or nil local text = args.text text = type (text) == 'string' and not typeData and true text or falsenil typeData local issues = typeData or cfg{} table.types[cfginsert(issues, sect) table.default]insert(issues, issue) self table.typeClass = typeData.classinsert(issues, text) self.typeImage issue = typeDatatable.imageconcat(issues, ' ') end
-- Find Get the self.talk value. local talk = args.talk -- Show talk links on the template page or template subpages if the box has been wrongly substitutedtalk -- parameter is blank. if cfgtalk == '' and self.substCheck templateTitle and args( mw.title.equals(self.templateTitle, self.title) or self.title:isSubpageOf(self.templateTitle) ) then talk = '#' elseif talk == '' then talk = nil end if talk then -- If the talk value is a talk page, make a link to that page. Else -- assume that it's a section heading, and make a link to the talk -- page of the current page with that section heading. local talkTitle = getTitleObject(talk) local talkArgIsTalkPage = true if not talkTitle or not talkTitle.subst isTalkPage then talkArgIsTalkPage = false talkTitle =getTitleObject( self.title.text, mw.site.namespaces[self.title.namespace].talk.id ) end if talkTitle and talkTitle.exists then local talkText = 'SUBSTRelevant discussion may be found on' if talkArgIsTalkPage then talkText = string.format( '%s [[%s|%s]].', talkText, talk, talkTitle.prefixedText ) else talkText = string.format( '%s the [[%s#%s|talk page]].', talkText, talkTitle.prefixedText, talk ) end self.isSubstituted talk = truetalkText end end
-- Find whether we are using a small message boxGet other values. if cfg self.fix = args.allowSmall fix ~= '' and (args.fix or nil local date cfg if args.smallParam date and args.small date ~='' then date = cfgargs.smallParamdate or not cfg elseif args.smallParam date == '' and yesnoself.isTemplatePage then date = lang:formatDate(args'F Y') end if date then self.date = string.format(" <smallclass='date-container'>''(<span class='date'>%s</span>) ''</small>", date) then end self.isSmall info = trueargs.info else if yesno(args.removalnotice) then self.isSmall removalNotice = falsecfg.removalNotice end end
-- Add attributesSet the non-collapsible text field. At the moment this is used by all box -- types other than ambox, classes and styles. self.id also by ambox when small= argsyes.id self:addClass(cfg.usePlainlinksParam and yesno(args.plainlinks or true) and 'plainlinks') for _, class in ipairs(cfg.classes or {}) do self:addClass(class) end if self.isSmall then self:addClass(cfg.smallClass text = args.smalltext or 'mbox-small') end self:addClass(selfargs.typeClass)text self:addClass(args.class) else self.style text = args.styletext end
-- Set text stylethe below row. self.textstyle below = cfg.below and args.textstylebelow
-- Process data for collapsible text fields. At the moment these are only used in {{ambox}}General image settings. self.useCollapsibleTextFields imageCellDiv = not self.isSmall and cfg.useCollapsibleTextFields if self.useCollapsibleTextFields thenimageCellDiv self.name imageEmptyCell = argscfg.nameimageEmptyCell local nameTitle = getTitleObject(self if cfg.name)imageEmptyCellStyle then self.isTemplatePage = nameTitle and self.title.prefixedText =imageEmptyCellStyle = ('Templateborder:none;padding:0px;width:1px' .. nameTitle.text) and true or false end
-- Get the self.issue valueLeft image settings. if local imageLeft = self.isSmall and args.smalltext then self.issue = smallimage or args.smalltext else local sectimage if argscfg.sect =imageCheckBlank and imageLeft ~= 'blank' then sect and imageLeft ~= 'This none' .. ( or not cfg.sectionDefault or imageCheckBlank and imageLeft ~= 'pagenone') elseif type(args.sect) == 'string' then sect self.imageLeft = 'This ' .. args.sectimageLeft end if not imageLeft then local issue imageSize = argsself.issueisSmall issue = type and (issuecfg.imageSmallSize or '30x30px') == or 'string40x40px' and issue or nil local text self.imageLeft = argsstring.text text = typeformat(text) '[[File:%s|%s|link=|alt= ]]'string' and text or nil local issues = {} tinsert(issues, sect) tinsert(issues, issue) tinsert(issues, text) self.issue = tconcat(issues, typeImage or ' Imbox notice.png', imageSize) end end
-- Get the self.talk valueRight image settings. local talk imageRight = argsself.talk if talk == '' isSmall and selfargs.isTemplatePage then talk = '#' end if talk then -- See if the talk link exists and is for a talk smallimageright or a content namespaceargs.imageright local talkTitle = getTitleObject(talk) if not talkTitle or not talkTitle.isTalkPage then -- If we couldn't process the talk page link, get the talk page of the current page. local success success, talkTitle = pcall(self.title.talkPageTitle, selfcfg.title) if not success then talkTitle = nil end end if talkTitle imageRightNone and talkTitle.exists then local talkText imageRight = 'Relevant discussion may be found on' if talkTitle.isTalkPage then talkText = format('%s [[%s|%s]].none', talkText, talk, talkTitle.prefixedText)then else talkText = format('%s the [[%s#%s|talk page]].', talkText, talkTitle.prefixedText, talk) end self.talk imageRight = talkTextimageRight end end
-- Get other values.function MessageBox:setMainspaceCategories() self.fix = args.fix local date if args.date and args.date ~= '' then date = argsself.date elseif args.date == '' and self.isTemplatePage then date = lang:formatDate('F Y') end if date then self.date local cfg = format(" <small>''(%s)''</small>", date) end self.info = args.info endcfg
-- 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. if selfnot cfg.isSmall allowMainspaceCategories then self.text = args.smalltext or args.text else self.text = args.text return nil end
-- Set the below row local nums = {} for _, prefix in ipairs{'cat', 'category', 'all'} do args[prefix .. '1'] = args[prefix] self.below nums = cfg.below and union(nums, getArgNums(args.below, prefix)) end
-- General image settingsThe following is roughly equivalent to the old {{Ambox/category}}. self local date = args.date date = type(date) == 'string' and date local preposition = 'from' for _, num in ipairs(nums) do local mainCat = args['cat' .. tostring(num)] or args['category' ..imageCellDiv tostring(num)] local allCat = not selfargs['all' ..isSmall tostring(num)] mainCat = type(mainCat) == 'string' and cfgmainCat allCat = type(allCat) == 'string' and allCat if mainCat and date and date ~= '' then local catTitle = string.imageCellDiv and true or falseformat('%s %s %s', mainCat, preposition, date) self:addCat(0, catTitle) catTitle = getTitleObject('Category:' .imageEmptyCell = cfg.imageEmptyCellcatTitle) if cfgnot catTitle or not catTitle.imageEmptyCellStyle exists then self.imageEmptyCellStyle :addCat(0, 'Articles with invalid date parameter in template') end elseif mainCat and (not date or date == 'border') then self:none;paddingaddCat(0, mainCat) end if allCat then self:0px;width:1px'addCat(0, allCat) end end end
-- Left image settings.function MessageBox:setTemplateCategories() local imageLeft args = self.isSmall and args.smallimage or args.image if local cfg.imageCheckBlank and imageLeft ~= 'blank' and imageLeft ~= 'none' or not cfg.imageCheckBlank and imageLeft ~= 'none' then self.imageLeft = imageLeft if not imageLeft then local imageSize = self.isSmall and (cfg.imageSmallSize or '30x30px') or '40x40px' self.imageLeft = format('[[File:%s|%s|link=|alt=]]', self.typeImage or 'Imbox notice.png', imageSize) end end
-- Right image settingsAdd template categories. local imageRight = self if cfg.isSmall and argstemplateCategory then if cfg.smallimageright or argstemplateCategoryRequireName then if self.imagerightisTemplatePage then if not self:addCat(10, cfg.imageRightNone and imageRight == 'none'templateCategory) end elseif not self.title.isSubpage then self:addCat(10, cfg.imageRight = imageRighttemplateCategory) end end
-- Add mainspace template error categories. At the moment these are only used in {{ambox}}. if cfg.allowMainspaceCategories templateErrorCategory then if args.cat then args.cat1 local templateErrorCategory = argscfg.cattemplateErrorCategory end local templateCat, templateSort if not self.name and not self.catNums = getArgNums(args, 'cat') if argstitle.category isSubpage then args.category1 templateCat = args.categorytemplateErrorCategory end elseif self.categoryNums = getArgNums(args, 'category')isTemplatePage then self.categoryParamNums local paramsToCheck = union(selfcfg.catNums, self.categoryNums) -- The following is roughly equivalent to the old {templateErrorParamsToCheck or {Ambox/category}}. local date = args.date date = type(date) == 'string' and date local preposition count = 'from'0 for _i, num param in ipairs(self.categoryParamNumsparamsToCheck) do local mainCat = if not args['cat' .. tostring(num)] or args['category' .. tostring(num)param]then local allCat count = args['all' .. tostring(num)]count + 1 mainCat = type(mainCat) == 'string' and mainCat end allCat = type(allCat) == 'string' and allCat end if mainCat and date count > 0 then local catTitle templateCat = format('%s %s %s', mainCat, preposition, date)templateErrorCategory self:addCat templateSort = tostring('main', catTitlecount) catTitle = getTitleObject('Category:' .. catTitle) end if not catTitle or not catTitleself.categoryNums and #self.exists categoryNums > 0 then self:addCat('main', templateCat = templateErrorCategory templateSort = 'Articles with invalid date parameter in templateC') end elseif mainCat and not date then end self:addCat('main'10, mainCat) end if allCat then self:addCat('main'templateCat, allCattemplateSort) end end end
function MessageBox:setAllNamespaceCategories() -- Add template-namespace Set categoriesfor all namespaces. if self.isTemplatePage invalidTypeError then local allSort = type(self.name) == 'string' and self.title.prefixedText namespace == (0 and 'TemplateMain:' .. self.nameor '') if cfg.templateCategory then if self.name then if self.isTemplatePage then self:addCat('template', cfg.templateCategory) end elseif not self.title.isSubpage thenprefixedText self:addCat('templateall', cfg.templateCategory) end end -- Add template error category. if cfg.templateErrorCategory then local templateErrorCategory = cfg.templateErrorCategory local templateCat, templateSort if not self.name and not self.title.isSubpage then templateCat = templateErrorCategory elseif type(self.name) == 'string' and self.title.prefixedText == ('Template:Wikipedia message box parameter needs fixing' .. self.name) then local paramsToCheck = cfg.templateErrorParamsToCheck or {} local count = 0 for i, param in ipairs(paramsToCheckallSort) do if not args[param] then count = count + 1 end end if count > 0 then templateCat = templateErrorCategory templateSort = tostring(count) end if self.categoryNums and #self.categoryNums > 0 isSubstituted then templateCat = templateErrorCategory templateSort = 'C' end end self:addCat('templateall', templateCat, templateSort'Pages with incorrectly substituted templates') endend
-- Categories for all namespaces.function MessageBox:setCategories() if self.invalidTypeError then local allSort = (nsid title.namespace == 0 and 'Mainthen self:' or ''setMainspaceCategories() .. elseif self.title.prefixedTextnamespace == 10 then self:addCatsetTemplateCategories('all', 'Wikipedia message box parameter needs fixing', allSort) end if self.isSubstituted then self:addCatsetAllNamespaceCategories('all', 'Pages with incorrectly substituted templates') end
function MessageBox:renderCategories() if not self.hasCategories then -- No categories added, no need to pass them to Category handler so, -- if it was invoked, it would return the empty string. -- So we shortcut and return the empty string. return "" end -- Convert category tables to strings and pass them through -- [[Module:Category handler]]. self return require('Module:Category handler').categories = categoryHandler_main{ main = tconcattable.concat(self.mainCats categories[0] or {}), template = tconcattable.concat(self.templateCats categories[10] or {}), all = tconcattable.concat(self.allCats categories.all or {}), nocat = self.args.nocat, demospace = self.demospace, page = self.pageTitle and selfargs.pageTitle.prefixedText or nilpage }
-- Add the subst check errorleft-hand image. local row = boxTable:tag('tr') if self.isSubstituted and self.name imageLeft then root . local imageLeftCell = row:tag('btd') .:addClass('errormbox-image') if self.wikitext(format(imageCellDiv then -- If we are using a div, redefine imageLeftCell so that the image 'Template <code>%s[[Template -- is inside it. Divs use style="width:%s|%s]]%s</code> has been incorrectly substituted52px;", which limits the -- image width to 52px.'If any images in a div are wider than that, mw. -- they may overlap with the textor cause other display problems.nowiki imageLeftCell = imageLeftCell:tag('{{div'):css('width', '52px') end imageLeftCell:wikitext(self.name, imageLeft or nil) elseif self.nameimageEmptyCell then -- Some message boxes define an empty cell if no image is specified, mwand -- some don't.The old template code in templates where empty cells are -- specified gives the following hint: "No image. Cell with some width -- or padding necessary for textcell to have 100% width.nowiki" row:tag('}}td') :addClass('mbox-empty-cell') :cssText(self.imageEmptyCellStyle or nil) end
-- Create Add the text. local textCell = row:tag('td'):addClass('mbox-text') if self.useCollapsibleTextFields then -- The message box tableuses advanced text parameters that allow things to be -- collapsible. At the moment, only ambox uses this. textCell:cssText(self.textstyle or nil) local boxTable textCellDiv = roottextCell:tag('div') textCellDiv :addClass('mbox-text-span') :wikitext(self.issue or nil) if (self.talk or self.fix) and not self.isSmall then textCellDiv:tag('tablespan') boxTable :addClass('hide-when-compact') :wikitext(self.attrtalk and ('id', .. self.idtalk) or nil) for i, class in ipairs :wikitext(self.fix and (' ' .. self.classes fix) or {}nil) do boxTable end textCellDiv:wikitext(self.addClassdate and (class' ' .. self.date) or nil) end if self.info and not self.isSmall then textCellDiv :tag('span') boxTable :addClass('hide-when-compact') :wikitext(self.cssTextinfo and (' ' .. self.styleinfo)or nil) end if self.attrremovalNotice then textCellDiv:tag('rolesmall', ) :addClass('hide-when-compact') :tag('presentationi') :wikitext(string.format(" (%s)", self.removalNotice)) end else -- Default text formatting - anything goes. textCell :cssText(self.textstyle or nil) :wikitext(self.text or nil) end
-- Add the leftright-hand image. local row = boxTable.tag('tr') if self.imageLeft imageRight then local imageLeftCell imageRightCell = row.:tag('td').:addClass('mbox-imageimageright') if self.imageCellDiv then -- If we are using a div, redefine imageLeftCell imageRightCell so that the image -- is inside it. -- Divs use style imageRightCell ="widthimageRightCell: 52px;", which limits the image width to 52px. If any -- images in a div are wider than that, they may overlap with the text or cause -- other display problems. imageLeftCell = imageLeftCell.tag('div').:css('width', '52px') end imageLeftCell imageRightCell . :wikitext(self.imageLeft) elseif self.imageEmptyCell then -- Some message boxes define an empty cell if no image is specified, and some don't. -- The old template code in templates where empty cells are specified gives the following hint: -- "No image. Cell with some width imageRight or padding necessary for text cell to have 100% width." row.tag('td') .addClass('mbox-empty-cell') .cssText(self.imageEmptyCellStylenil) end
-- Add the text. local textCell = below row.tag('td').addClass('mbox-text') if self.useCollapsibleTextFields below then -- The message box uses advanced text parameters that allow things to be collapsible. At the -- moment, only ambox uses this. textCell .cssText(self.textstyle) local textCellSpan = textCell. boxTable:tag('span') textCellSpan .addClass(tr'mbox-text-span') .wikitext(self.issue) if not self.isSmall then textCellSpan . :tag('spantd') .addClass :attr('hide-when-compactcolspan') .wikitext(, self.talk imageRight and ' 3' .. self.talk) .wikitext(self.fix and ' ' .. self.fix) end textCellSpan .wikitext(self.date and or ' 2' .. self.date) if not self.isSmall then textCellSpan .tag('span') . :addClass('hidembox-when-compacttext') .wikitext(self.info and ' ' .. self.info) end else -- Default text formatting - anything goes. textCell . :cssText(self.textstyleor nil) . :wikitext(self.textbelow or nil) end
-- Add the right-hand imageerror message for invalid type parameters. if self.imageRight invalidTypeError then local imageRightCell = row. root:tag('tddiv').addClass :css('mboxtext-imagerightalign', 'center') if self :wikitext(string.imageCellDiv thenformat( imageRightCell 'This message box is using an invalid "type= imageRightCell%s" parameter and needs fixing.tag('div'), self.css(type or 'width', '52px' ) -- If we are using a div, redefine imageRightCell so that the image is inside it. end imageRightCell .wikitext(self.imageRight) end
-- Add the below rowcategories. if root:wikitext(self.below then boxTable.tag('tr') .tag:renderCategories('td') .attr('colspan', self.imageRight and '3' or '2'nil) .addClass('mbox-text') .cssText(self.textstyle) .wikitext return tostring(self.belowroot) end
-- Add error message for invalid type parameters.------------------------------------------------------------------------------ if self.invalidTypeError then-- Exports root .tag('div') .addClass('error') .css('text-align', 'center') .wikitext(format('This message box is using an invalid type parameter (<code>type=%s</code>) and needs fixing.', self.type or '')) end-------------------------------------------------------------------------------
-- Add categories. root .wikitext(self.categories)local p, mt = {}, {}
return tostringfunction p._exportClasses(root) -- For testing. return { MessageBox = MessageBox }
local function makeBoxp.main(boxType, args, cfgTables) local box:setTitle= MessageBox.new(boxType, args, cfgTables or mw.loadData(CONFIG_MODULE) local cfg = box:getConfig(boxType) args = box:removeBlankArgssetParameters(cfg, args) box:setBoxParameterssetCategories(cfg, args) return box:export()
local function makeWrappermt.__index(boxTypet, k) 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.not getArgs then local origArgs if frame = getArgs = mw.getCurrentFramerequire() then origArgs = frame'Module:getParent(Arguments').argsgetArgs for k, v in pairs(frame.args) do origArgs = frame.args break end else origArgs = frame end -- Trim whitespace return t. local args = {} for main(k, v in pairsgetArgs(origArgs) do if type(v) frame, {trim =false, removeBlanks = 'string' then v = trim(vfalse}) end args[k] = v end return makeBox(boxType, args) end
1 revision imported
-- This is a meta-module for producing message box templates, including -- {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}.
-- Require Load necessary modules.local htmlBuilder = require('Module:HtmlBuilderNo globals')local categoryHandler = require('Module:Category handler').maingetArgs
local yesno = require('Module:Yesno')
local lang = mw.language.getContentLanguage()
-- Set aliases for often-used functions to reduce table lookups.Define constantslocal format CONFIG_MODULE = mw.ustring.format'Module:Message box/configuration'local tinsert DEMOSPACES = table.insertlocal tconcat {talk = 'tmbox', image = 'imbox', file = 'imbox', category = 'cmbox', article = table.concatlocal trim 'ambox', main = mw.text.trim'ambox'}
local function getTitleObject(page) if type(page...) == 'string' then -- Get the title object, passing the function through pcall -- in case we are over the expensive function count limit. local success, title = pcall(mw.title.new, page...) if success then return title end end
end
local function union(t1, t2)
end
local function getArgNums(args, prefix)
end
---------------------------------------------------------------------------------- Box class definition-------------------------------------------------------------------------------- local MessageBox = {}MessageBox.__index = MessageBox function boxMessageBox.new(boxType, args, cfg) args = args or {} local obj = {} -- Set the title object and the namespace. obj.title = getTitleObject(args.page) or mw.title.getNamespaceIdgetCurrentTitle(ns) -- Set the config for our box type. obj.cfg = cfg[boxType] if not ns obj.cfg then return end if type( local ns) =obj.title.namespace -- boxType is "mbox" or invalid input if args.demospace and args.demospace ~= 'string' then ns -- implement demospace parameter of mbox local demospace = lang:ucfirst(mw.ustringstring.lower(ns)args.demospace) if ns =DEMOSPACES[demospace] then -- use template from DEMOSPACES obj.cfg = cfg[DEMOSPACES[demospace]] elseif string.find( demospace, 'Maintalk' ) then -- demo as a talk page obj.cfg = cfg.tmbox else -- default to ombox obj.cfg = cfg.ombox end elseif ns == 0then end obj.cfg = cfg.ambox -- main namespace elseif ns == 6 then obj.cfg = cfg.imbox -- file namespace elseif ns == 14 then obj.cfg = cfg.cmbox -- category namespace end else local nsTable = mw.site.namespaces[ns] if nsTable and nsTable.isTalk then obj.cfg = cfg.tmbox -- any talk namespace else obj.cfg = cfg.ombox -- other namespaces or invalid input end end end -- Set the arguments, and remove all blank arguments except for the ones -- listed in cfg.allowBlankParams. do local newArgs = {} for k, v in pairs(args) do if v ~= '' then return nsTable newArgs[k] = v end end for i, param in ipairs(obj.cfg.allowBlankParams or {}) do newArgs[param] = args[param] end obj.idargs = newArgs end -- Define internal data structure. obj.categories = {} obj.classes = {} -- For lazy loading of [[Module:Category handler]]. obj.hasCategories = false return setmetatable(obj, MessageBox)
end
function box.getMboxTypeMessageBox:addCat(nsidns, cat, sort) -- Gets the mbox type from a namespace number. if nsid == 0 not cat then return 'ambox' -- main namespacenil end elseif nsid == 6 if sort then return cat = string.format('imbox[[Category:%s|%s]]' -- file namespace, cat, sort) else elseif nsid cat == 14 then return string.format('cmbox[[Category:%s]]' -- category namespace, cat) else end local nsTable self.hasCategories = mwtrue self.sitecategories[ns] = self.namespacescategories[nsidns]or {} if nsTable and nsTable table.insert(self.isTalk then return 'tmbox' -- any talk namespace else return 'ombox' -- other namespaces or invalid input end endcategories[ns], cat)
end
function boxMessageBox:addCataddClass(ns, cat, sort) if type(catclass) ~= 'string' then return end local nsVals = {'main', 'template', 'all'} local tname for i, val in ipairs(nsVals) do if ns == val then tname = ns .. 'Cats' end end if not tname class then for i, val in ipairs(nsVals) do return nil nsVals[i] = format('"%s"', val) end error('invalid ns parameter passed to box:addCat; valid values are ' . table. mw.text.listToTextinsert(nsVals, nil, ' or ')) end self[tname] = self[tname] or {} if type(sort) == 'string' then tinsert(self[tname], format('[[Category:%s|%s]]', cat.classes, sortclass)) else tinsert(self[tname], format('[[Category:%s]]', cat)) end
end
function boxMessageBox:addClasssetParameters(class) if local args = self.args local cfg = self.cfg -- Get type data. self.type = args.type local typeData = cfg.types[self.type] self.invalidTypeError = cfg.showInvalidTypeError and self.type( and not typeData typeData = typeData or cfg.types[cfg.default] self.typeClass = typeData.class) ~ self.typeImage = typeData.image -- Find if the box has been wrongly substituted. self.isSubstituted = cfg.substCheck and args.subst == 'stringSUBST' then return end -- Find whether we are using a small message box. self.classes isSmall = cfg.allowSmall and ( cfg.smallParam and args.small = self= cfg.classes smallParam or {} tinsertnot cfg.smallParam and yesno(selfargs.classes, classsmall)end )
end
function boxMessageBox:export() local root = htmlBuildermw.html.create() -- Add the subst check error. if self.isSubstituted and self.name then root:tag('b') :addClass('error') :wikitext(string.format( 'Template <code>%s[[Template:%s|%s]]%s</code> has been incorrectly substituted.', mw.text.nowiki('{{'), self.name, self.name, mw.text.nowiki('}}') )) end -- Create the box table. local boxTable = root:tag('table') boxTable:attr('id', self.id or nil) for i, class in ipairs(self.classes or {}) do boxTable:addClass(class or nil) end boxTable :cssText(self.style or nil) :attr('role', 'presentation') if self.attrs then boxTable:attr(self.attrs) end
end
end
end
return { box = box, makeBox = makeBox, mbox = makeWrapper('mbox'), ambox = makeWrappersetmetatable('ambox')p, cmbox = makeWrapper('cmbox'), fmbox = makeWrapper('fmbox'), imbox = makeWrapper('imbox'), ombox = makeWrapper('ombox'), tmbox = makeWrapper('tmbox'mt)}