https://wikijoo.ir/index.php?title=%D9%BE%D9%88%D8%AF%D9%85%D8%A7%D9%86:Infobox&feed=atom&action=historyپودمان:Infobox - تاریخچهٔ نسخهها2024-03-29T05:42:58Zتاریخچهٔ نسخهها برای این صفحه در ویکیMediaWiki 1.41.0https://wikijoo.ir/index.php?title=%D9%BE%D9%88%D8%AF%D9%85%D8%A7%D9%86:Infobox&diff=1067121&oldid=prevDaneshGostar: ۱ نسخه واردشده2019-07-24T05:23:50Z<p>۱ نسخه واردشده</p>
<p><b>صفحهٔ تازه</b></p><div>--<br />
-- This module implements {{Infobox}}<br />
--<br />
<br />
local p = {}<br />
<br />
local args = {}<br />
local origArgs<br />
local root<br />
<br />
local 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 />
:cssText(rowArgs.rowstyle)<br />
:attr('id', rowArgs.rowid)<br />
:tag('th')<br />
:attr('colspan', 2)<br />
:attr('id', rowArgs.headerid)<br />
:addClass(rowArgs.class)<br />
:addClass(args.headerclass)<br />
:css('text-align', 'center')<br />
:cssText(args.headerstyle)<br />
:wikitext(rowArgs.header)<br />
elseif rowArgs.data then<br />
local row = root:tag('tr')<br />
row:addClass(rowArgs.rowclass)<br />
row:cssText(rowArgs.rowstyle)<br />
row:attr('id', rowArgs.rowid)<br />
if rowArgs.label then<br />
row<br />
:tag('th')<br />
:attr('scope', 'row')<br />
:attr('id', rowArgs.labelid)<br />
:css('text-align', 'right')<br />
:cssText(args.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 />
:attr('id', rowArgs.dataid)<br />
:addClass(rowArgs.class)<br />
:cssText(rowArgs.datastyle)<br />
:newline()<br />
:wikitext(rowArgs.data)<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 = mw.html.create():wikitext(args['image' .. tostring(num)])<br />
if caption then<br />
data<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 />
label = args['label' .. tostring(num)],<br />
data = args['data' .. tostring(num)],<br />
datastyle = args.datastyle,<br />
class = args['class' .. tostring(num)],<br />
rowclass = args['rowclass' .. tostring(num)],<br />
rowstyle = args['rowstyle' .. tostring(num)],<br />
dataid = args['dataid' .. tostring(num)],<br />
labelid = args['labelid' .. tostring(num)],<br />
headerid = args['headerid' .. tostring(num)],<br />
rowid = args['rowid' .. 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', 'left')<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:Articles which use infobox templates with no data rows]]')<br />
end<br />
if args.child == 'yes' and args.title then<br />
root:wikitext('[[Category:Articles which use embedded infobox templates with the title parameter]]')<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 = mw.html.create('table')<br />
<br />
root<br />
:addClass('infobox')<br />
:addClass(args.bodyclass)<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', '100%')<br />
:css('clear', 'none')<br />
:css('float', 'none')<br />
:css('background-color', 'transparent')<br />
else<br />
root<br />
:css('width', '22em')<br />
end<br />
root<br />
:cssText(args.bodystyle)<br />
<br />
renderTitle()<br />
renderAboveRow()<br />
else<br />
root = mw.html.create()<br />
<br />
root<br />
:wikitext(args.title)<br />
end<br />
<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 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('title')<br />
preprocessSingleArg('titleclass')<br />
preprocessSingleArg('titlestyle')<br />
preprocessSingleArg('above')<br />
preprocessSingleArg('aboveclass')<br />
preprocessSingleArg('abovestyle')<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 = 'rowstyle'},<br />
{prefix = 'class'},<br />
{prefix = 'dataid'},<br />
{prefix = 'labelid'},<br />
{prefix = 'headerid'},<br />
{prefix = 'rowid'}<br />
}, 50)<br />
preprocessSingleArg('headerclass')<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>DaneshGostar