Changes

Jump to: navigation, search

Module:Message box

1,429 bytes added, 1 year ago
m
1 revision imported: mass import/update of Wikipedia stuff
-- This is a meta-module for producing message box templates, including
-- {{mbox}}, {{ambox}}, {{imbox}}, {{tmbox}}, {{ombox}}, {{cmbox}} and {{fmbox}}.
 
-- Load necessary modules.
require('Module:No globals')
local getArgs
local yesno = require('Module:Yesno')
 
-- Get a language object for formatDate and ucfirst.
local lang = mw.language.getContentLanguage()
 
-- Define constants
local CONFIG_MODULE = 'Module:Message box/configuration'
local DEMOSPACES = {talk = 'tmbox', image = 'imbox', file = 'imbox', category = 'cmbox', article = 'ambox', main = 'ambox'}
--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------
 
local function getTitleObject(...)
-- Get the title object, passing the function through pcall
end
end
 
local function union(t1, t2)
-- Returns the union of two arrays.
return ret
end
 
local function getArgNums(args, prefix)
local nums = {}
return nums
end
 
--------------------------------------------------------------------------------
-- Box class definition
--------------------------------------------------------------------------------
 
local MessageBox = {}
MessageBox.__index = MessageBox
 
function MessageBox.new(boxType, args, cfg)
args = args or {}
local obj = {}
 
-- Set the title object and the namespace.
obj.title = getTitleObject(args.page) or mw.title.getCurrentTitle()
 
-- Set the config for our box type.
obj.cfg = cfg[boxType]
local ns = obj.title.namespace
-- boxType is "mbox" or invalid input
if args.demospace and args.demospace ~= '' then -- implement demospace parameter of mbox local demospace = string.lower(args.demospace) if DEMOSPACES[demospace] then -- use template from DEMOSPACES obj.cfg = cfg[DEMOSPACES[demospace]] elseif string.find( demospace, 'talk' ) then -- demo as a talk page obj.cfg = cfg.tmbox else -- default to ombox obj.cfg = cfg.ombox end elseif ns == 0 then
obj.cfg = cfg.ambox -- main namespace
elseif ns == 6 then
end
end
 
-- Set the arguments, and remove all blank arguments except for the ones
-- listed in cfg.allowBlankParams.
obj.args = newArgs
end
 
-- Define internal data structure.
obj.categories = {}
-- For lazy loading of [[Module:Category handler]].
obj.hasCategories = false
 
return setmetatable(obj, MessageBox)
end
 
function MessageBox:addCat(ns, cat, sort)
if not cat then
table.insert(self.categories[ns], cat)
end
 
function MessageBox:addClass(class)
if not class then
table.insert(self.classes, class)
end
 
function MessageBox:setParameters()
local args = self.args
local cfg = self.cfg
 
-- Get type data.
self.type = args.type
self.typeClass = typeData.class
self.typeImage = typeData.image
 
-- Find if the box has been wrongly substituted.
self.isSubstituted = cfg.substCheck and args.subst == 'SUBST'
 
-- Find whether we are using a small message box.
self.isSmall = cfg.allowSmall and (
or not cfg.smallParam and yesno(args.small)
)
 
-- Add attributes, classes and styles.
self.id = args.id
self.name = args.name
if self.name then
self:addClass('box-' .. string.gsub(self.name,' ','_'))
end
if yesno(args.plainlinks) ~= false then
self:addClass('plainlinks')
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
and cfg.templateCategoryRequireName
then
self.name = args.name
if self.name then
local templateName = mw.ustring.match(
and mw.title.equals(self.title, self.templateTitle)
end
 
-- Process data for collapsible text fields. At the moment these are only
-- used in {{ambox}}.
self.issue = table.concat(issues, ' ')
end
 
-- Get the self.talk value.
local talk = args.talk
end
if talkTitle and talkTitle.exists then
local talkText if self.isSmall then local talkLink = talkArgIsTalkPage and talk or (talkTitle.prefixedText .. '#' .. talk) talkText = string.format('([[%s|talk]])', talkLink) else talkText = 'Relevant 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 end
self.talk = talkText
end
end
 
-- Get other values.
self.fix = args.fix ~= '' and args.fix or nil
end
if date then
self.date = string.format(" <smallspan class='date-container'><i>(<span class='date'(>%s</span>)''</smalli></span>", date)
end
self.info = args.info
end
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.
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
self.imageEmptyCell = cfg.imageEmptyCell
if cfg.imageEmptyCellStyle then
self.imageEmptyCellStyle = 'border:none;padding:0px;width:1px'
end
 
-- Left image settings.
local imageLeft = self.isSmall and args.smallimage or args.image
end
end
 
-- Right image settings.
local imageRight = self.isSmall and args.smallimageright or args.imageright
self.imageRight = imageRight
end
-- set templatestyles
self.base_templatestyles = cfg.templatestyles
self.templatestyles = args.templatestyles
end
 
function MessageBox:setMainspaceCategories()
local args = self.args
local cfg = self.cfg
 
if not cfg.allowMainspaceCategories then
return nil
end
 
local nums = {}
for _, prefix in ipairs{'cat', 'category', 'all'} do
nums = union(nums, getArgNums(args, prefix))
end
 
-- The following is roughly equivalent to the old {{Ambox/category}}.
local date = args.date
end
end
 
function MessageBox:setTemplateCategories()
local args = self.args
local cfg = self.cfg
 
-- Add template categories.
if cfg.templateCategory then
end
end
 
-- Add template error categories.
if cfg.templateErrorCategory then
end
end
 
function MessageBox:setAllNamespaceCategories()
-- Set categories for all namespaces.
end
end
 
function MessageBox:setCategories()
if self.title.namespace == 0 then
self:setAllNamespaceCategories()
end
 
function MessageBox:renderCategories()
if not self.hasCategories then
}
end
 
function MessageBox:export()
local root = mw.html.create()
 
-- Add the subst check error.
if self.isSubstituted and self.name then
))
end
local frame = mw.getCurrentFrame() root:wikitext(frame:extensionTag{ name = 'templatestyles', args = { src = self.base_templatestyles }, }) -- Add support for a single custom templatestyles sheet. Undocumented as -- need should be limited and many templates using mbox are substed; we -- don't want to spread templatestyles sheets around to arbitrary places if self.templatestyles then root:wikitext(frame:extensionTag{ name = 'templatestyles', args = { src = self.templatestyles }, }) end
-- Create the box table.
local boxTable = root:tag('table')
:cssText(self.style or nil)
:attr('role', 'presentation')
 
if self.attrs then
boxTable:attr(self.attrs)
end
 
-- Add the left-hand image.
local row = boxTable:tag('tr')
-- 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'):cssaddClass('width', '52pxmbox-image-div')
end
imageLeftCell:wikitext(self.imageLeft or nil)
row:tag('td')
:addClass('mbox-empty-cell')
:cssText(self.imageEmptyCellStyle or nil)
end
 
-- Add the text.
local textCell = row:tag('td'):addClass('mbox-text')
:addClass('mbox-text-span')
:wikitext(self.issue or nil)
if (self.talk or self.fix) and not self.isSmall then
textCellDiv:tag('span')
:addClass('hide-when-compact')
end
if self.removalNotice then
textCellDiv:tag('smallspan')
:addClass('hide-when-compact')
:tag('i')
:wikitext(self.text or nil)
end
 
-- Add the right-hand image.
if self.imageRight then
-- If we are using a div, redefine imageRightCell so that the image
-- is inside it.
imageRightCell = imageRightCell:tag('div'):cssaddClass('width', '52pxmbox-image-div')
end
imageRightCell
:wikitext(self.imageRight or nil)
end
 
-- Add the below row.
if self.below then
:wikitext(self.below or nil)
end
 
-- Add error message for invalid type parameters.
if self.invalidTypeError then
root:tag('div')
:cssaddClass('textmbox-align', 'centerinvalid-type')
:wikitext(string.format(
'This message box is using an invalid "type=%s" parameter and needs fixing.',
))
end
 
-- Add categories.
root:wikitext(self:renderCategories() or nil)
 
return tostring(root)
end
 
--------------------------------------------------------------------------------
-- Exports
--------------------------------------------------------------------------------
 
local p, mt = {}, {}
 
function p._exportClasses()
-- For testing.
}
end
 
function p.main(boxType, args, cfgTables)
local box = MessageBox.new(boxType, args, cfgTables or mw.loadData(CONFIG_MODULE))
return box:export()
end
 
function mt.__index(t, k)
return function (frame)
end
end
 
return setmetatable(p, mt)

Navigation menu