模块:Message box:修订间差异
imported>Quest for Truth 无编辑摘要 |
imported>Shizhao ep |
||
| (未显示8个用户的34个中间版本) | |||
| 第2行: | 第2行: | ||
-- Require necessary modules. | -- Require necessary modules. | ||
local | local getArgs = require('Module:Arguments').getArgs | ||
local categoryHandler = require('Module:Category handler').main | local categoryHandler = require('Module:Category handler').main | ||
local yesno = require('Module:Yesno') | local yesno = require('Module:Yesno') | ||
| 第18行: | 第18行: | ||
local trim = mw.text.trim | local trim = mw.text.trim | ||
-------------------------------------------------------------------------------- | |||
-- Helper functions | |||
-------------------------------------------------------------------------------- | |||
local function getTitleObject(page, ...) | local function getTitleObject(page, ...) | ||
| 第58行: | 第60行: | ||
table.sort(nums) | table.sort(nums) | ||
return nums | return nums | ||
end | |||
local function insertStyle(cfg) | |||
-- Create the templatestyles tag. | |||
local frame = mw.getCurrentFrame() | |||
return frame:extensionTag{ | |||
name = 'templatestyles', | |||
args = { src = cfgTables['@global'].templatestyles }, | |||
} .. frame:extensionTag{ | |||
name = 'templatestyles', | |||
args = { src = cfg.templatestyles }, | |||
} | |||
end | |||
-------------------------------------------------------------------------------- | |||
-- Box class definition | |||
-------------------------------------------------------------------------------- | |||
local box = {} | |||
box.__index = box | |||
function box.new() | |||
local obj = {} | |||
setmetatable(obj, box) | |||
return obj | |||
end | end | ||
| 第105行: | 第134行: | ||
nsVals[i] = format('"%s"', val) | nsVals[i] = format('"%s"', val) | ||
end | end | ||
error(' | error('無效的ns參數傳送到box:addCat;有效的數值為' .. mw.text.listToText(nsVals, '、', '或')) | ||
end | end | ||
self[tname] = self[tname] or {} | self[tname] = self[tname] or {} | ||
| 第141行: | 第170行: | ||
end | end | ||
tinsert(boxTypes, '"mbox"') | tinsert(boxTypes, '"mbox"') | ||
error(format(' | error(format('無效的訊息框類型「%s」;有效的類型為%s', tostring(boxType), mw.text.listToText(boxTypes)), 2) | ||
end | end | ||
self.cfg = cfg | |||
return cfg | return cfg | ||
end | end | ||
function box:removeBlankArgs( | function box:removeBlankArgs(args) | ||
-- Only allow blank arguments for the parameter names listed in cfg.allowBlankParams. | -- Only allow blank arguments for the parameter names listed in cfg.allowBlankParams. | ||
local newArgs = {} | local newArgs = {} | ||
| 第154行: | 第184行: | ||
end | end | ||
end | end | ||
for i, param in ipairs(cfg.allowBlankParams or {}) do | for i, param in ipairs(self.cfg.allowBlankParams or {}) do | ||
newArgs[param] = args[param] | newArgs[param] = args[param] | ||
end | end | ||
| 第160行: | 第190行: | ||
end | end | ||
function box:setBoxParameters( | function box:setBoxParameters(args) | ||
local cfg = self.cfg | |||
-- Get type data. | -- Get type data. | ||
self.type = args.type | self.type = args.type | ||
| 第188行: | 第220行: | ||
if cfg.allowId then | if cfg.allowId then | ||
self.id = args.id | self.id = args.id | ||
self.name = args.name | |||
if self.name then | |||
self:addClass('box-' .. string.gsub(self.name,' ','_')) | |||
end | |||
end | end | ||
self:addClass(cfg.usePlainlinksParam and yesno(args.plainlinks or true) and 'plainlinks') | self:addClass(cfg.usePlainlinksParam and yesno(args.plainlinks or true) and 'plainlinks') | ||
| 第195行: | 第231行: | ||
if self.isSmall then | if self.isSmall then | ||
self:addClass(cfg.smallClass or 'mbox-small') | self:addClass(cfg.smallClass or 'mbox-small') | ||
end | |||
if yesno(args.hidden) then | |||
self:addClass('infobox editsection') | |||
end | end | ||
self:addClass(self.typeClass) | self:addClass(self.typeClass) | ||
| 第224行: | 第263行: | ||
local sect | local sect | ||
if args.sect == '' then | if args.sect == '' then | ||
sect = ' | sect = '此' .. (cfg.sectionDefault or '頁面') | ||
elseif type(args.sect) == 'string' then | elseif type(args.sect) == 'string' then | ||
sect = ' | sect = '此' .. args.sect | ||
end | end | ||
local issue = args.issue | local issue = args.issue | ||
| 第236行: | 第275行: | ||
tinsert(issues, issue) | tinsert(issues, issue) | ||
tinsert(issues, text) | tinsert(issues, text) | ||
self.issue = tconcat(issues | self.issue = tconcat(issues) | ||
end | end | ||
| 第261行: | 第300行: | ||
local talkText = '相關討論可見於' | local talkText = '相關討論可見於' | ||
if talkArgIsTalkPage then | if talkArgIsTalkPage then | ||
talkText = format('%s[[%s|%s]] | talkText = format('%s[[%s|%s]]。', talkText, talk, talkTitle.prefixedText) | ||
else | else | ||
talkText = format('%s[[%s#%s|討論頁]] | talkText = format('%s[[%s#%s|討論頁]]。', talkText, talkTitle.prefixedText, talk) | ||
end | end | ||
self.talk = talkText | self.talk = talkText | ||
| 第273行: | 第312行: | ||
if args.date and args.date ~= '' then | if args.date and args.date ~= '' then | ||
date = args.date | date = args.date | ||
elseif args. | elseif args.time == '' and self.isTemplatePage then | ||
date = lang:formatDate('Y年n月j日') | date = lang:formatDate('Y年n月j日') | ||
elseif args.time and args.time ~= '' then | elseif args.time and args.time ~= '' then | ||
date = lang:formatDate('Y年n月j日', time) | date = lang:formatDate('Y年n月j日', args.time) | ||
end | end | ||
if date then | if date then | ||
self.date = format("<small> | local ok, tempdate = pcall(lang.formatDate, lang, 'Y年n月j日', date) -- 正規化日期 | ||
if ok then | |||
date = tempdate | |||
end | |||
end | |||
if date then | |||
self.date = string.format(" <small class='date-container'>''(<span class='date'>%s</span>)''</small>", date) | |||
end | end | ||
if args.fix and args.fix ~= '' then | if args.fix and args.fix ~= '' then | ||
| 第343行: | 第388行: | ||
-- The following is roughly equivalent to the old {{Ambox/category}}. | -- The following is roughly equivalent to the old {{Ambox/category}}. | ||
local date | local date | ||
local sortDay | |||
local dayName = { | |||
[1] = '㏠', | |||
[2] = '㏡', | |||
[3] = '㏢', | |||
[4] = '㏣', | |||
[5] = '㏤', | |||
[6] = '㏥', | |||
[7] = '㏦', | |||
[8] = '㏧', | |||
[9] = '㏨', | |||
[10] = '㏩', | |||
[11] = '㏪', | |||
[12] = '㏫', | |||
[13] = '㏬', | |||
[14] = '㏭', | |||
[15] = '㏮', | |||
[16] = '㏯', | |||
[17] = '㏰', | |||
[18] = '㏱', | |||
[19] = '㏲', | |||
[20] = '㏳', | |||
[21] = '㏴', | |||
[22] = '㏵', | |||
[23] = '㏶', | |||
[24] = '㏷', | |||
[25] = '㏸', | |||
[26] = '㏹', | |||
[27] = '㏺', | |||
[28] = '㏻', | |||
[29] = '㏼', | |||
[30] = '㏽', | |||
[31] = '㏾' | |||
} | |||
if args.date and args.date ~= '' then | if args.date and args.date ~= '' then | ||
date = args.date | date = args.date | ||
local ok, tempdate = pcall(lang.formatDate, lang, 'Y年n月', date) -- 正規化日期 | |||
if ok then | |||
date = tempdate | |||
end | |||
elseif args.time and args.time ~= '' then | elseif args.time and args.time ~= '' then | ||
date = lang:formatDate('Y年n月', time) | date = lang:formatDate('Y年n月', args.time) | ||
sortDay = lang:formatDate('j', args.time) | |||
sortDay = tonumber(sortDay) | |||
sortDay = dayName[sortDay] | |||
end | end | ||
date = type(date) == 'string' and date | date = type(date) == 'string' and date | ||
| 第357行: | 第443行: | ||
if mainCat and date and date ~= '' then | if mainCat and date and date ~= '' then | ||
local catTitle = format('%s%s%s', preposition, date, mainCat) | local catTitle = format('%s%s%s', preposition, date, mainCat) | ||
self:addCat('main', catTitle) | if sortDay then | ||
self:addCat('main', catTitle, sortDay) | |||
else | |||
self:addCat('main', catTitle) | |||
end | |||
catTitle = getTitleObject('Category:' .. catTitle) | catTitle = getTitleObject('Category:' .. catTitle) | ||
if not catTitle or not catTitle.exists then | if not catTitle or not catTitle.exists then | ||
| 第411行: | 第501行: | ||
if self.invalidTypeError then | if self.invalidTypeError then | ||
local allSort = (self.nsid == 0 and 'Main:' or '') .. self.title.prefixedText | local allSort = (self.nsid == 0 and 'Main:' or '') .. self.title.prefixedText | ||
self:addCat('all', ' | self:addCat('all', '需要修复的信息框', allSort) | ||
end | end | ||
if self.isSubstituted then | if self.isSubstituted then | ||
self:addCat('all', ' | self:addCat('all', '錯誤使用替換引用的頁面') | ||
end | end | ||
| 第429行: | 第519行: | ||
function box:export() | function box:export() | ||
local root = | local root = mw.html.create() | ||
-- Add the subst check error. | -- Add the subst check error. | ||
if self.isSubstituted and self.name then | if self.isSubstituted and self.name then | ||
root | root | ||
:tag('b') | |||
:addClass('error') | |||
:wikitext(format( | |||
' | '模板<code>%s[[Template:%s|%s]]%s</code>被錯誤地替代。', | ||
mw.text.nowiki('{{'), self.name, self.name, mw.text.nowiki('}}') | mw.text.nowiki('{{'), self.name, self.name, mw.text.nowiki('}}') | ||
)) | )) | ||
end | end | ||
root:wikitext(insertStyle(self.cfg)) | |||
-- Create the box table. | -- Create the box table. | ||
local boxTable = root | local boxTable = root:tag('table') | ||
boxTable | boxTable | ||
:attr('id', self.id) | |||
for i, class in ipairs(self.classes or {}) do | for i, class in ipairs(self.classes or {}) do | ||
boxTable | boxTable | ||
:addClass(class) | |||
end | end | ||
boxTable | boxTable | ||
:cssText(self.style) | |||
:attr('role', 'presentation') | |||
-- Add the left-hand image. | -- Add the left-hand image. | ||
local row = boxTable | local row = boxTable:tag('tr') | ||
if self.imageLeft then | if self.imageLeft then | ||
local imageLeftCell = row | local imageLeftCell = row:tag('td'):addClass('mbox-image') | ||
if self.imageCellDiv then | if self.imageCellDiv then | ||
-- If we are using a div, redefine imageLeftCell so that the image is inside it. | -- If we are using a div, redefine imageLeftCell so that the image is inside it. | ||
| 第463行: | 第555行: | ||
-- images in a div are wider than that, they may overlap with the text or cause | -- images in a div are wider than that, they may overlap with the text or cause | ||
-- other display problems. | -- other display problems. | ||
imageLeftCell = imageLeftCell | imageLeftCell = imageLeftCell:tag('div'):css('width', '52px') | ||
end | end | ||
imageLeftCell | imageLeftCell | ||
:wikitext(self.imageLeft) | |||
elseif self.imageEmptyCell then | elseif self.imageEmptyCell then | ||
-- Some message boxes define an empty cell if no image is specified, and some don't. | -- 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: | -- 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 text cell to have 100% width." | -- "No image. Cell with some width or padding necessary for text cell to have 100% width." | ||
row | row:tag('td') | ||
:addClass('mbox-empty-cell') | |||
:cssText(self.imageEmptyCellStyle) | |||
end | end | ||
-- Add the text. | -- Add the text. | ||
local textCell = row | local textCell = row:tag('td'):addClass('mbox-text') | ||
if self.useCollapsibleTextFields then | if self.useCollapsibleTextFields then | ||
-- The message box uses advanced text parameters that allow things to be collapsible. At the | -- The message box uses advanced text parameters that allow things to be collapsible. At the | ||
-- moment, only ambox uses this. | -- moment, only ambox uses this. | ||
textCell | textCell | ||
:cssText(self.textstyle) | |||
local textCellSpan = textCell | local textCellSpan = textCell:tag('div') | ||
textCellSpan | textCellSpan | ||
:addClass('mbox-text-span') | |||
:wikitext(self.issue) | |||
if not self.isSmall then | if not self.isSmall then | ||
textCellSpan | textCellSpan | ||
:tag('span') | |||
:addClass('hide-when-compact') | |||
:wikitext(self.talk and self.talk) | |||
end | end | ||
textCellSpan | textCellSpan | ||
:wikitext(self.date and self.date) | |||
if not self.isSmall and self.fix ~= '' then | if not self.isSmall and self.fix ~= '' then | ||
textCellSpan | textCellSpan | ||
:tag('span') | |||
:addClass('hide-when-compact') | |||
:wikitext(self.fix and self.fix) | |||
end | end | ||
if not self.isSmall then | if not self.isSmall then | ||
textCellSpan | textCellSpan | ||
:tag('span') | |||
:addClass('hide-when-compact') | |||
:wikitext(self.info and self.info) | |||
end | end | ||
else | else | ||
-- Default text formatting - anything goes. | -- Default text formatting - anything goes. | ||
textCell | textCell | ||
:cssText(self.textstyle) | |||
:wikitext(self.text) | |||
end | end | ||
-- Add the right-hand image. | -- Add the right-hand image. | ||
if self.imageRight then | if self.imageRight then | ||
local imageRightCell = row | local imageRightCell = row:tag('td'):addClass('mbox-imageright') | ||
if self.imageCellDiv then | if self.imageCellDiv then | ||
imageRightCell = imageRightCell | imageRightCell = imageRightCell:tag('div'):css('width', '52px') -- If we are using a div, redefine imageRightCell so that the image is inside it. | ||
end | end | ||
imageRightCell | imageRightCell | ||
:wikitext(self.imageRight) | |||
end | end | ||
-- Add the below row. | -- Add the below row. | ||
if self.below then | if self.below then | ||
boxTable | boxTable:tag('tr') | ||
:tag('td') | |||
:attr('colspan', self.imageRight and '3' or '2') | |||
:addClass('mbox-text') | |||
:cssText(self.textstyle) | |||
:wikitext(self.below) | |||
end | end | ||
| 第537行: | 第629行: | ||
if self.invalidTypeError then | if self.invalidTypeError then | ||
root | root | ||
:tag('div') | |||
:css('text-align', 'center') | |||
:wikitext(format('此訊息框使用無效的「type=%s」參數,需要修復。', self.type or '')) | |||
end | end | ||
-- Add categories. | -- Add categories. | ||
root | root | ||
:wikitext(self.categories) | |||
return tostring(root) | return tostring(root) | ||
| 第550行: | 第642行: | ||
local function main(boxType, args) | local function main(boxType, args) | ||
box:setTitle(args) | local outputBox = box.new() | ||
outputBox:setTitle(args) | |||
args = | outputBox:getConfig(boxType) | ||
args = outputBox:removeBlankArgs(args) | |||
return | outputBox:setBoxParameters(args) | ||
return outputBox:export() | |||
end | end | ||
local function makeWrapper(boxType) | local function makeWrapper(boxType) | ||
return function (frame) | return function (frame) | ||
local args = getArgs(frame, {trim = false, removeBlanks = false}) | |||
return main(boxType, args) | return main(boxType, args) | ||
end | end | ||
end | |||
local function insertStyleWikitext(frame) | |||
local args = getArgs(frame, {trim = false, removeBlanks = false}) | |||
local boxType = args[1] or 'mbox' | |||
-- // ---- // -- | |||
local outputBox = box.new() | |||
outputBox:setTitle(args) | |||
local cfg = outputBox:getConfig(boxType) | |||
return insertStyle(cfg) | |||
end | end | ||
local p = { | local p = { | ||
main = main, | main = main, | ||
mbox = makeWrapper('mbox') | mbox = makeWrapper('mbox'), | ||
insertStyle = insertStyleWikitext | |||
} | } | ||
for boxType in pairs(cfgTables) do | for boxType in pairs(cfgTables) do | ||
p[boxType] = makeWrapper(boxType) | if boxType:sub(1, 1) ~= '@' then | ||
p[boxType] = makeWrapper(boxType) | |||
end | |||
end | end | ||
return p | return p | ||