https://tnfshwiki.tfcis.org/index.php?title=%E6%A8%A1%E7%B5%84:Infobox&feed=atom&action=history
模組:Infobox - 修訂歷史
2024-03-28T16:51:11Z
本 Wiki 上此頁面的修訂歷史
MediaWiki 1.39.2
https://tnfshwiki.tfcis.org/index.php?title=%E6%A8%A1%E7%B5%84:Infobox&diff=7437&oldid=prev
Xiplus:從維基百科更新
2019-10-04T12:22:10Z
<p>從維基百科更新</p>
<a href="https://tnfshwiki.tfcis.org/index.php?title=%E6%A8%A1%E7%B5%84:Infobox&diff=7437&oldid=4542">顯示變更</a>
Xiplus
https://tnfshwiki.tfcis.org/index.php?title=%E6%A8%A1%E7%B5%84:Infobox&diff=4542&oldid=prev
Xiplus:新頁面: -- -- This module implements {{Infobox}} -- 中文的此模塊對比英文維基多解析了overimage, overcaption, -- overimagerowclass以及header/labal/data*style參數。 -- local...
2016-04-11T00:01:03Z
<p>新頁面: -- -- This module implements {{Infobox}} -- 中文的此模塊對比英文維基多解析了overimage, overcaption, -- overimagerowclass以及header/labal/data*style參數。 -- local...</p>
<p><b>新頁面</b></p><div>--<br />
-- This module implements {{Infobox}}<br />
-- 中文的此模塊對比英文維基多解析了overimage, overcaption,<br />
-- overimagerowclass以及header/labal/data*style參數。<br />
--<br />
<br />
local p = {}<br />
<br />
local HtmlBuilder = require('Module:HtmlBuilder')<br />
<br />
local args = {}<br />
local origArgs<br />
local root<br />
<br />
function union(t1, t2)<br />
-- Returns the union of the values of two tables, as a sequence.<br />
local vals = {}<br />
for k,v in pairs(t1) do<br />
vals[v] = true<br />
end<br />
for k,v in pairs(t2) do<br />
vals[v] = true<br />
end<br />
local ret = {}<br />
for k,v in pairs(vals) do<br />
table.insert(ret, k)<br />
end<br />
return ret<br />
end<br />
<br />
local function getArgNums(prefix)<br />
-- Returns a table containing the numbers of the arguments that exist<br />
-- for the specified prefix. For example, if the prefix was 'data', and<br />
-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.<br />
local nums = {}<br />
for k,v in pairs(args) do<br />
local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')<br />
if num then table.insert(nums, tonumber(num)) end<br />
end<br />
table.sort(nums)<br />
return nums<br />
end<br />
<br />
local function addRow(rowArgs)<br />
-- Adds a row to the infobox, with either a header cell<br />
-- or a label/data cell combination.<br />
if rowArgs.header then<br />
root<br />
.tag('tr')<br />
.addClass(rowArgs.rowclass)<br />
.tag('th')<br />
.attr('colspan', 2)<br />
.addClass(rowArgs.class)<br />
.css('text-align', 'center')<br />
.cssText(rowArgs.headerstyle)<br />
.wikitext(rowArgs.header)<br />
elseif rowArgs.data then<br />
local row = root.tag('tr')<br />
row.addClass(rowArgs.rowclass)<br />
if rowArgs.label then<br />
row<br />
.tag('th')<br />
.attr('scope', 'row')<br />
.css('text-align', 'left')<br />
.cssText(rowArgs.labelstyle)<br />
.wikitext(rowArgs.label)<br />
.done()<br />
end<br />
<br />
local dataCell = row.tag('td')<br />
if not rowArgs.label then <br />
dataCell<br />
.attr('colspan', 2)<br />
.css('text-align', 'center') <br />
end<br />
dataCell<br />
.addClass(rowArgs.class)<br />
.cssText(rowArgs.datastyle)<br />
.newline()<br />
.wikitext(rowArgs.data)<br />
end<br />
end<br />
<br />
local function renderOverImage()<br />
if not args.overimage then return end<br />
<br />
local row = root.tag('tr')<br />
row.addClass(args.overimagerowclass)<br />
local topImage = row.tag('td')<br />
topImage.attr('colspan', 2)<br />
topImage.addClass(args.class)<br />
topImage.cssText(args.datastyle)<br />
topImage.css('text-align', 'center')<br />
if args.overcaption then<br />
topImage.wikitext(args.overimage .. '<br><span style=\"' .. args.captionstyle .. '\">' .. args.overcaption .. '</span>')<br />
else<br />
topImage.wikitext(args.overimage)<br />
end<br />
end<br />
<br />
local function renderTitle()<br />
if not args.title then return end<br />
<br />
root<br />
.tag('caption')<br />
.addClass(args.titleclass)<br />
.cssText(args.titlestyle)<br />
.wikitext('\'\'\'' .. args.title .. '\'\'\'')<br />
end<br />
<br />
local function renderAboveRow()<br />
if not args.above then return end<br />
<br />
root<br />
.tag('tr')<br />
.tag('th')<br />
.attr('colspan', 2)<br />
.addClass(args.aboveclass)<br />
.css('text-align', 'center')<br />
.css('font-size', '125%')<br />
.css('font-weight', 'bold')<br />
.cssText(args.abovestyle)<br />
.wikitext(args.above)<br />
end<br />
<br />
local function renderBelowRow()<br />
if not args.below then return end<br />
<br />
root<br />
.tag('tr')<br />
.tag('td')<br />
.attr('colspan', '2')<br />
.addClass(args.belowclass)<br />
.css('text-align', 'center')<br />
.cssText(args.belowstyle)<br />
.newline()<br />
.wikitext(args.below)<br />
end<br />
<br />
local function renderSubheaders()<br />
if args.subheader then<br />
args.subheader1 = args.subheader<br />
end<br />
if args.subheaderrowclass then<br />
args.subheaderrowclass1 = args.subheaderrowclass<br />
end<br />
local subheadernums = getArgNums('subheader')<br />
for k,num in ipairs(subheadernums) do<br />
addRow({<br />
data = args['subheader' .. tostring(num)],<br />
datastyle = args.subheaderstyle or args['subheaderstyle' .. tostring(num)],<br />
class = args.subheaderclass,<br />
rowclass = args['subheaderrowclass' .. tostring(num)]<br />
})<br />
end<br />
end<br />
<br />
local function renderImages()<br />
if args.image then<br />
args.image1 = args.image<br />
end<br />
if args.caption then<br />
args.caption1 = args.caption<br />
end<br />
local imagenums = getArgNums('image')<br />
for k,num in ipairs(imagenums) do<br />
local caption = args['caption' .. tostring(num)]<br />
local data = HtmlBuilder.create().wikitext(args['image' .. tostring(num)])<br />
if caption then<br />
data<br />
.tag('br', {selfClosing = true})<br />
.done()<br />
.tag('div')<br />
.cssText(args.captionstyle)<br />
.wikitext(caption)<br />
end<br />
addRow({<br />
data = tostring(data),<br />
datastyle = args.imagestyle,<br />
class = args.imageclass,<br />
rowclass = args['imagerowclass' .. tostring(num)]<br />
})<br />
end<br />
end<br />
<br />
local function renderRows()<br />
-- Gets the union of the header and data argument numbers,<br />
-- and renders them all in order using addRow.<br />
local rownums = union(getArgNums('header'), getArgNums('data'))<br />
table.sort(rownums)<br />
for k,num in ipairs(rownums) do<br />
addRow({<br />
header = args['header' .. tostring(num)],<br />
headerstyle = (args.headerstyle or '') .. ';' .. (args['header' .. tostring(num) .. 'style'] or ''),<br />
label = args['label' .. tostring(num)],<br />
labelstyle = (args.labelstyle or '') .. ';' .. (args['label' .. tostring(num) .. 'style'] or ''),<br />
data = args['data' .. tostring(num)],<br />
datastyle = (args.datastyle or '') .. ';' .. (args['data' .. tostring(num) .. 'style'] or ''),<br />
class = args['class' .. tostring(num)],<br />
rowclass = args['rowclass' .. tostring(num)]<br />
})<br />
end<br />
end<br />
<br />
local function renderNavBar()<br />
if not args.name then return end<br />
<br />
root<br />
.tag('tr')<br />
.tag('td')<br />
.attr('colspan', '2')<br />
.css('text-align', 'right')<br />
.wikitext(mw.getCurrentFrame():expandTemplate({ <br />
title = 'navbar', <br />
args = { args.name, mini = 1 }<br />
}))<br />
end<br />
<br />
local function renderItalicTitle()<br />
local italicTitle = args['italic title'] and mw.ustring.lower(args['italic title'])<br />
if italicTitle == '' or italicTitle == 'force' or italicTitle == 'yes' then<br />
root.wikitext(mw.getCurrentFrame():expandTemplate({title = 'italic title'}))<br />
end<br />
end<br />
<br />
local function renderTrackingCategories()<br />
if args.decat ~= 'yes' then<br />
if #(getArgNums('data')) == 0 and mw.title.getCurrentTitle().namespace == 0 then<br />
root.wikitext('[[Category:使用無數據行信息框模板的條目]]')<br />
end<br />
if args.child == 'yes' and args.title then<br />
root.wikitext('[[Category:使用帶有標題參數的嵌入式信息框模板的條目]]')<br />
end<br />
end<br />
end<br />
<br />
local function _infobox()<br />
-- Specify the overall layout of the infobox, with special settings<br />
-- if the infobox is used as a 'child' inside another infobox.<br />
if args.child ~= 'yes' then<br />
root = HtmlBuilder.create('table')<br />
<br />
root<br />
.addClass('infobox')<br />
.addClass(args.bodyclass)<br />
.attr('cellspacing', 3)<br />
.css('border-spacing', '3px')<br />
<br />
if args.subbox == 'yes' then<br />
root<br />
.css('padding', '0')<br />
.css('border', 'none')<br />
.css('margin', '-3px')<br />
.css('width', 'auto')<br />
.css('min-width', '100%')<br />
.css('font-size', 'small')<br />
.css('clear', 'none')<br />
.css('float', 'none')<br />
.css('background-color', 'transparent')<br />
else<br />
root<br />
.css('width', '22em')<br />
.css('text-align', 'left')<br />
.css('font-size', 'small')<br />
.css('line-height', '1.5em')<br />
end<br />
root<br />
.cssText(args.bodystyle)<br />
<br />
renderTitle()<br />
renderAboveRow()<br />
else<br />
root = HtmlBuilder.create()<br />
<br />
root<br />
.wikitext(args.title)<br />
end<br />
<br />
renderOverImage()<br />
renderSubheaders()<br />
renderImages() <br />
renderRows() <br />
renderBelowRow() <br />
renderNavBar()<br />
renderItalicTitle()<br />
renderTrackingCategories()<br />
<br />
return tostring(root)<br />
end<br />
<br />
local function preprocessSingleArg(argName)<br />
-- If the argument exists and isn't blank, add it to the argument table.<br />
-- Blank arguments are treated as nil to match the behaviour of ParserFunctions.<br />
if origArgs[argName] and origArgs[argName] ~= '' then<br />
args[argName] = origArgs[argName]<br />
end<br />
end<br />
<br />
local function preprocessArgs(prefixTable, step)<br />
-- Assign the parameters with the given prefixes to the args table, in order, in batches<br />
-- of the step size specified. This is to prevent references etc. from appearing in the<br />
-- wrong order. The prefixTable should be an array containing tables, each of which has<br />
-- two possible fields, a "prefix" string and a "depend" table. The function always parses<br />
-- parameters containing the "prefix" string, but only parses parameters in the "depend"<br />
-- table if the prefix parameter is present and non-blank.<br />
if type(prefixTable) ~= 'table' then<br />
error("Non-table value detected for the prefix table", 2)<br />
end<br />
if type(step) ~= 'number' then<br />
error("Invalid step value detected", 2)<br />
end<br />
<br />
-- Get arguments without a number suffix, and check for bad input.<br />
for i,v in ipairs(prefixTable) do<br />
if type(v) ~= 'table' or type(v.prefix) ~= "string" or (v.depend and type(v.depend) ~= 'table') then<br />
error('Invalid input detected to preprocessArgs prefix table', 2)<br />
end<br />
preprocessSingleArg(v.prefix)<br />
-- Only parse the depend parameter if the prefix parameter is present and not blank.<br />
if args[v.prefix] and v.depend then<br />
for j,dependValue in ipairs(v.depend) do<br />
if type(dependValue) ~= 'string' then<br />
error('Invalid "depend" parameter value detected in preprocessArgs')<br />
end<br />
preprocessSingleArg(dependValue)<br />
end<br />
end<br />
end<br />
<br />
-- Get arguments with number suffixes.<br />
local a = 1 -- Counter variable.<br />
local moreArgumentsExist = true<br />
while moreArgumentsExist == true do<br />
moreArgumentsExist = false<br />
for i = a, a + step - 1 do<br />
for j,v in ipairs(prefixTable) do<br />
local prefixArgName = v.prefix .. tostring(i)<br />
if origArgs[prefixArgName] then<br />
moreArgumentsExist = true -- Do another loop if any arguments are found, even blank ones.<br />
preprocessSingleArg(prefixArgName)<br />
end<br />
-- Process the depend table if the prefix argument is present and not blank, or<br />
-- we are processing "prefix1" and "prefix" is present and not blank, and<br />
-- if the depend table is present.<br />
if v.depend and (args[prefixArgName] or (i == 1 and args[v.prefix])) then<br />
for j,dependValue in ipairs(v.depend) do<br />
local dependArgName = dependValue .. tostring(i)<br />
preprocessSingleArg(dependArgName)<br />
end<br />
end<br />
end<br />
end<br />
a = a + step<br />
end<br />
end<br />
<br />
function preprocessSpecificStyle(styleTable, step)<br />
-- Assign the parameters *style to the args table<br />
local a = 1 -- Counter variable.<br />
local moreArgumentsExist = true<br />
while moreArgumentsExist == true do<br />
moreArgumentsExist = false<br />
for i = a,a + step - 1 do<br />
for j,v in ipairs(styleTable) do<br />
local styleArgName = v.arg .. tostring(i) .. 'style'<br />
if origArgs[styleArgName] then<br />
moreArgumentsExist = true -- Do another loop if any arguments are found, even blank ones.<br />
preprocessSingleArg(styleArgName)<br />
end<br />
end<br />
end<br />
a = a + step<br />
end<br />
end<br />
<br />
function p.infobox(frame)<br />
-- If called via #invoke, use the args passed into the invoking template.<br />
-- Otherwise, for testing purposes, assume args are being passed directly in.<br />
if frame == mw.getCurrentFrame() then<br />
origArgs = frame:getParent().args<br />
else<br />
origArgs = frame<br />
end<br />
<br />
-- Parse the data parameters in the same order that the old {{infobox}} did, so that<br />
-- references etc. will display in the expected places. Parameters that depend on<br />
-- another parameter are only processed if that parameter is present, to avoid<br />
-- phantom references appearing in article reference lists.<br />
preprocessSingleArg('child')<br />
preprocessSingleArg('bodyclass')<br />
preprocessSingleArg('subbox')<br />
preprocessSingleArg('bodystyle')<br />
preprocessSingleArg('overimage')<br />
preprocessSingleArg('overcaption')<br />
preprocessSingleArg('overimagerowclass')<br />
preprocessSingleArg('title')<br />
preprocessSingleArg('titleclass')<br />
preprocessSingleArg('titlestyle')<br />
preprocessSingleArg('above')<br />
preprocessSingleArg('aboveclass')<br />
preprocessSingleArg('abovestyle')<br />
preprocessSingleArg('aboverowclass')<br />
preprocessArgs({<br />
{prefix = 'subheader', depend = {'subheaderstyle', 'subheaderrowclass'}}<br />
}, 10)<br />
preprocessSingleArg('subheaderstyle')<br />
preprocessSingleArg('subheaderclass')<br />
preprocessArgs({<br />
{prefix = 'image', depend = {'caption', 'imagerowclass'}}<br />
}, 10)<br />
preprocessSingleArg('captionstyle')<br />
preprocessSingleArg('imagestyle')<br />
preprocessSingleArg('imageclass')<br />
preprocessArgs({<br />
{prefix = 'header'},<br />
{prefix = 'data', depend = {'label'}},<br />
{prefix = 'rowclass'},<br />
{prefix = 'class'}<br />
}, 80)<br />
preprocessSpecificStyle({<br />
{arg = 'header'},<br />
{arg = 'label'},<br />
{arg = 'data'}<br />
}, 80)<br />
preprocessSingleArg('headerstyle')<br />
preprocessSingleArg('labelstyle')<br />
preprocessSingleArg('datastyle')<br />
preprocessSingleArg('below')<br />
preprocessSingleArg('belowclass')<br />
preprocessSingleArg('belowstyle')<br />
preprocessSingleArg('name')<br />
args['italic title'] = origArgs['italic title'] -- different behaviour if blank or absent<br />
preprocessSingleArg('decat')<br />
<br />
return _infobox()<br />
end<br />
<br />
return p</div>
Xiplus