Modul:Infobox: Diferență între versiuni

De la MediaWiki
Sari la navigare Sari la căutare
https://en.wikipedia.org/wiki/$1>Izno
merge hlist here
 
Fără descriere a modificării
 
(Nu s-a afișat o versiune intermediară efectuată de un alt utilizator)
Linia 1: Linia 1:
--
-- This module implements {{Infobox}}
--
local p = {}
local p = {}
local getArgs = require('Modul:Arguments').getArgs
local StringUtils = require('Modul:StringUtils')
local TableTools = require('Modul:TableTools')
local args = {}
local args = {}
local origArgs = {}
local root
local empty_row_categories = {}
local category_in_empty_row_pattern = '%[%[%s*[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]%s*:[^]]*]]'
local has_rows = false
local lists = {
plainlist_t = {
patterns = {
'^plainlist$',
'%splainlist$',
'^plainlist%s',
'%splainlist%s'
},
found = false,
styles = 'Plainlist/styles.css'
},
hlist_t = {
patterns = {
'^hlist$',
'%shlist$',
'^hlist%s',
'%shlist%s'
},
found = false,
styles = 'Hlist/styles.css'
}
}


local function has_list_class(args_to_check)
p._infobox = function(origArgs)
for _, list in pairs(lists) do
if not list.found then
for _, arg in pairs(args_to_check) do
for _, pattern in ipairs(list.patterns) do
if mw.ustring.find(arg or '', pattern) then
list.found = true
break
end
end
if list.found then break end
end
end
end
end
 
local function fixChildBoxes(sval, tt)
local function notempty( s ) return s and s:match( '%S' ) end
if notempty(sval) then
local marker = '<span class=special_infobox_marker>'
local s = sval
-- start moving templatestyles and categories inside of table rows
local slast = ''
while slast ~= s do
slast = s
s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>%s*)(%[%[%s*[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]%s*:[^]]*%]%])', '%2%1')
s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>%s*)(\127[^\127]*UNIQ%-%-templatestyles%-%x+%-QINU[^\127]*\127)', '%2%1')
end
-- end moving templatestyles and categories inside of table rows
s = mw.ustring.gsub(s, '(<%s*[Tt][Rr])', marker .. '%1')
s = mw.ustring.gsub(s, '(</[Tt][Rr]%s*>)', '%1' .. marker)
if s:match(marker) then
s = mw.ustring.gsub(s, marker .. '%s*' .. marker, '')
s = mw.ustring.gsub(s, '([\r\n]|-[^\r\n]*[\r\n])%s*' .. marker, '%1')
s = mw.ustring.gsub(s, marker .. '%s*([\r\n]|-)', '%1')
s = mw.ustring.gsub(s, '(</[Cc][Aa][Pp][Tt][Ii][Oo][Nn]%s*>%s*)' .. marker, '%1')
s = mw.ustring.gsub(s, '(<%s*[Tt][Aa][Bb][Ll][Ee][^<>]*>%s*)' .. marker, '%1')
s = mw.ustring.gsub(s, '^(%{|[^\r\n]*[\r\n]%s*)' .. marker, '%1')
s = mw.ustring.gsub(s, '([\r\n]%{|[^\r\n]*[\r\n]%s*)' .. marker, '%1')
s = mw.ustring.gsub(s, marker .. '(%s*</[Tt][Aa][Bb][Ll][Ee]%s*>)', '%1')
s = mw.ustring.gsub(s, marker .. '(%s*\n|%})', '%1')
end
if s:match(marker) then
local subcells = mw.text.split(s, marker)
s = ''
for k = 1, #subcells do
if k == 1 then
s = s .. subcells[k] .. '</' .. tt .. '></tr>'
elseif k == #subcells then
local rowstyle = ' style="display:none"'
if notempty(subcells[k]) then rowstyle = '' end
s = s .. '<tr' .. rowstyle ..'><' .. tt .. ' colspan=2>\n' ..
subcells[k]
elseif notempty(subcells[k]) then
if (k % 2) == 0 then
s = s .. subcells[k]
else
s = s .. '<tr><' .. tt .. ' colspan=2>\n' ..
subcells[k] .. '</' .. tt .. '></tr>'
end
end
end
end
-- the next two lines add a newline at the end of lists for the PHP parser
-- [[Special:Diff/849054481]]
-- remove when [[:phab:T191516]] is fixed or OBE
s = mw.ustring.gsub(s, '([\r\n][%*#;:][^\r\n]*)$', '%1\n')
s = mw.ustring.gsub(s, '^([%*#;:][^\r\n]*)$', '%1\n')
s = mw.ustring.gsub(s, '^([%*#;:])', '\n%1')
s = mw.ustring.gsub(s, '^(%{%|)', '\n%1')
return s
else
return sval
end
end
 
-- Cleans empty tables
local function cleanInfobox()
root = tostring(root)
if has_rows == false then
root = mw.ustring.gsub(root, '<table[^<>]*>%s*</table>', '')
end
end


-- Returns the union of the values of two tables, as a sequence.
local child = origArgs["child"] or origArgs["embed"] or "no"
local function union(t1, t2)
local bodyclass = origArgs["bodyclass"] or "infocaseta"
 
local antet = origArgs["antet"] or "default"
local vals = {}
local aboveclass = origArgs["aboveclass"] or antet
for k, v in pairs(t1) do
local abovestyle = origArgs["abovestyle"] or ""
vals[v] = true
local culoare_cadru = origArgs["culoare cadru"] or "F5F5DC"
end
if mw.ustring.match(culoare_cadru, '^%x%x%x$') or mw.ustring.match(culoare_cadru, '^%x%x%x%x%x%x$') then culoare_cadru = StringUtils._prependIfMissing({culoare_cadru, '#'}) end
for k, v in pairs(t2) do
local culoare_text = origArgs["culoare text"] or "000000"
vals[v] = true
if mw.ustring.match(culoare_text, '^%x%x%x$') or mw.ustring.match(culoare_text, '^%x%x%x%x%x%x$') then culoare_text = StringUtils._prependIfMissing({culoare_text, '#'}) end
end
local titlestyle = origArgs["titlestyle"] or ""
local ret = {}
local title = origArgs["title"] or origArgs["titlu"] or ""
for k, v in pairs(vals) do
title = StringUtils._capitalize({title})
table.insert(ret, k)
local above = origArgs["above"] or ""
end
local parentColSpanArg = origArgs['parent_colspan']
return ret
local parentColSpan
end
if parentColSpanArg and mw.ustring.gsub(parentColSpanArg, '%d+', '', 1) == '' then
 
parentColSpan = tonumber(parentColSpanArg)
-- Returns a table containing the numbers of the arguments that exist
-- for the specified prefix. For example, if the prefix was 'data', and
-- 'data1', 'data2', and 'data5' exist, it would return {1, 2, 5}.
local function getArgNums(prefix)
local nums = {}
for k, v in pairs(args) do
local num = tostring(k):match('^' .. prefix .. '([1-9]%d*)$')
if num then table.insert(nums, tonumber(num)) end
end
table.sort(nums)
return nums
end
 
-- Adds a row to the infobox, with either a header cell
-- or a label/data cell combination.
local function addRow(rowArgs)
if rowArgs.header and rowArgs.header ~= '_BLANK_' then
has_rows = true
has_list_class({ rowArgs.rowclass, rowArgs.class, args.headerclass })
root
:tag('tr')
:addClass(rowArgs.rowclass)
:cssText(rowArgs.rowstyle)
:tag('th')
:attr('colspan', '2')
:addClass('infobox-header')
:addClass(rowArgs.class)
:addClass(args.headerclass)
-- @deprecated next; target .infobox-<name> .infobox-header
:cssText(args.headerstyle)
:cssText(rowArgs.rowcellstyle)
:wikitext(fixChildBoxes(rowArgs.header, 'th'))
if rowArgs.data then
root:wikitext(
'[[Category:Pages using infobox templates with ignored data cells]]'
)
end
elseif rowArgs.data and rowArgs.data:gsub(category_in_empty_row_pattern, ''):match('^%S') then
has_rows = true
has_list_class({ rowArgs.rowclass, rowArgs.class })
local row = root:tag('tr')
row:addClass(rowArgs.rowclass)
row:cssText(rowArgs.rowstyle)
if rowArgs.label then
row
:tag('th')
:attr('scope', 'row')
:addClass('infobox-label')
-- @deprecated next; target .infobox-<name> .infobox-label
:cssText(args.labelstyle)
:cssText(rowArgs.rowcellstyle)
:wikitext(rowArgs.label)
:done()
end
 
local dataCell = row:tag('td')
dataCell
:attr('colspan', not rowArgs.label and '2' or nil)
:addClass(not rowArgs.label and 'infobox-full-data' or 'infobox-data')
:addClass(rowArgs.class)
-- @deprecated next; target .infobox-<name> .infobox(-full)-data
:cssText(rowArgs.datastyle)
:cssText(rowArgs.rowcellstyle)
:wikitext(fixChildBoxes(rowArgs.data, 'td'))
else
else
table.insert(empty_row_categories, rowArgs.data or '')
parentColSpan = 2
end
end
end
local function renderTitle()
if not args.title then return end
has_rows = true
has_list_class({args.titleclass})
root
local wikidataEnabled = origArgs["wikidata"] or ""
:tag('caption')
local out = ""
:addClass('infobox-title')
:addClass(args.titleclass)
-- @deprecated next; target .infobox-<name> .infobox-title
:cssText(args.titlestyle)
:wikitext(args.title)
end
if child ~= "yes" then
 
out = out .. "<table class=\"" .. bodyclass .. "\">"
local function renderAboveRow()
-- caption
if not args.above then return end
out = out .. tostring(mw.html.create('tr')
 
:tag('td')
has_rows = true
:attr('colspan', '2')
has_list_class({ args.aboveclass })
:addClass('antet ' .. aboveclass)
:css('background-color', culoare_cadru)
root
:css('color', culoare_text)
:tag('tr')
:cssText(titlestyle)
:tag('th')
:wikitext(title))
-- header
if above ~= "" then
local aboveTr = mw.html.create('tr'):tag('td')
:attr('colspan', '2')
:attr('colspan', '2')
:addClass('infobox-above')
:addClass(aboveclass)
:addClass(args.aboveclass)
:css('text-align', 'center')
-- @deprecated next; target .infobox-<name> .infobox-above
:css('font-size', '125%')
:cssText(args.abovestyle)
:css('font-weight', 'bold')
:wikitext(fixChildBoxes(args.above,'th'))
:cssText(abovestyle)
end
:wikitext(above):allDone()
 
out = out .. tostring(aboveTr)
local function renderBelowRow()
end
if not args.below then return end
 
has_rows = true
has_list_class({ args.belowclass })
root
:tag('tr')
:tag('td')
:attr('colspan', '2')
:addClass('infobox-below')
:addClass(args.belowclass)
-- @deprecated next; target .infobox-<name> .infobox-below
:cssText(args.belowstyle)
:wikitext(fixChildBoxes(args.below,'td'))
end
 
local function addSubheaderRow(subheaderArgs)
if subheaderArgs.data and
subheaderArgs.data:gsub(category_in_empty_row_pattern, ''):match('^%S') then
has_rows = true
has_list_class({ subheaderArgs.rowclass, subheaderArgs.class })
local row = root:tag('tr')
row:addClass(subheaderArgs.rowclass)
 
local dataCell = row:tag('td')
dataCell
:attr('colspan', '2')
:addClass('infobox-subheader')
:addClass(subheaderArgs.class)
:cssText(subheaderArgs.datastyle)
:cssText(subheaderArgs.rowcellstyle)
:wikitext(fixChildBoxes(subheaderArgs.data, 'td'))
else
else
table.insert(empty_row_categories, subheaderArgs.data or '')
if title ~= "" then
local newTr = mw.html.create('tr'):tag('td')
:attr('colspan', tostring(parentColSpan))
:css('background-color', culoare_cadru)
:css('color', culoare_text)
:css('font-size', '125%')
:css('margin-bottom', '2')
:css('text-align', 'center')
:css('line-height', '1.2em')
:wikitext(title):allDone()
out = out .. "\'\'\'" .. tostring(newTr) .. "\'\'\'"
end
end
end
end


local function renderSubheaders()
--subheaders
if args.subheader then
local subheaders = {}
args.subheader1 = args.subheader
subheaders[1] = origArgs["subheader"] or origArgs["subheader1"] or ""
local subhIndex = 2
while (origArgs["subheader" .. tostring(subhIndex)] or "") ~= "" do
subheaders[subhIndex] = origArgs["subheader" .. tostring(subhIndex)] or ""
subhIndex = subhIndex + 1
end
end
if args.subheaderrowclass then
local subheaderstyle = origArgs["subheaderstyle"] or ""
args.subheaderrowclass1 = args.subheaderrowclass
local subheaderclass = origArgs["subheaderclass"] or ""
end
for subHeaderIdx = 1,#subheaders do
local subheadernums = getArgNums('subheader')
if subheaders[subHeaderIdx] ~= "" then
for k, num in ipairs(subheadernums) do
local subhTr = mw.html.create('tr'):tag('td'):attr('colspan', tostring(parentColSpan))
addSubheaderRow({
:addClass(subheaderclass)
data = args['subheader' .. tostring(num)],
:css('text-align', 'center')
-- @deprecated next; target .infobox-<name> .infobox-subheader
:cssText(subheaderstyle)
datastyle = args.subheaderstyle,
:wikitext(subheaders[subHeaderIdx]):allDone()
rowcellstyle = args['subheaderstyle' .. tostring(num)],
out = out .. tostring(subhTr)
class = args.subheaderclass,
rowclass = args['subheaderrowclass' .. tostring(num)]
})
end
end
 
local function addImageRow(imageArgs)
 
if imageArgs.data and
imageArgs.data:gsub(category_in_empty_row_pattern, ''):match('^%S') then
 
has_rows = true
has_list_class({ imageArgs.rowclass, imageArgs.class })
local row = root:tag('tr')
row:addClass(imageArgs.rowclass)
 
local dataCell = row:tag('td')
dataCell
:attr('colspan', '2')
:addClass('infobox-image')
:addClass(imageArgs.class)
:cssText(imageArgs.datastyle)
:wikitext(fixChildBoxes(imageArgs.data, 'td'))
else
table.insert(empty_row_categories, imageArgs.data or '')
end
end
 
local function renderImages()
if args.image then
args.image1 = args.image
end
if args.caption then
args.caption1 = args.caption
end
local imagenums = getArgNums('image')
for k, num in ipairs(imagenums) do
local caption = args['caption' .. tostring(num)]
local data = mw.html.create():wikitext(args['image' .. tostring(num)])
if caption then
data
:tag('div')
:addClass('infobox-caption')
-- @deprecated next; target .infobox-<name> .infobox-caption
:cssText(args.captionstyle)
:wikitext(caption)
end
end
addImageRow({
data = tostring(data),
-- @deprecated next; target .infobox-<name> .infobox-image
datastyle = args.imagestyle,
class = args.imageclass,
rowclass = args['imagerowclass' .. tostring(num)]
})
end
end
end
-- When autoheaders are turned on, preprocesses the rows
local function preprocessRows()
if not args.autoheaders then return end
local rownums = union(getArgNums('header'), getArgNums('data'))
--images
table.sort(rownums)
local imageIndices = TableTools.affixNums(origArgs, 'image')
local lastheader
local images = {}
for k, num in ipairs(rownums) do
local captions = {}
if args['header' .. tostring(num)] then
if origArgs['image'] then
if lastheader then
table.insert(images, origArgs['image'] or '')
args['header' .. tostring(lastheader)] = nil
table.insert(captions, origArgs['caption'] or '')
end
lastheader = num
elseif args['data' .. tostring(num)] and
args['data' .. tostring(num)]:gsub(
category_in_empty_row_pattern, ''
):match('^%S') then
local data = args['data' .. tostring(num)]
if data:gsub(category_in_empty_row_pattern, ''):match('%S') then
lastheader = nil
end
end
end
end
if lastheader then
for _,imgIndex in ipairs(imageIndices) do
args['header' .. tostring(lastheader)] = nil
table.insert(images, origArgs['image' .. tostring(imgIndex)])
table.insert(captions, origArgs['caption' .. tostring(imgIndex)] or '')
end
end
end


-- Gets the union of the header and data argument numbers,
local imageclass = origArgs["imageclass"]
-- and renders them all in order
local imagestyle = origArgs["imagestyle"]
local function renderRows()
local captionstyle = origArgs["captionstyle"]
 
for i = 1,#images do
local rownums = union(getArgNums('header'), getArgNums('data'))
if images[i] ~= "" then
table.sort(rownums)
local imageRow = mw.html.create('tr')
for k, num in ipairs(rownums) do
local imageTd = imageRow
addRow({
:tag('td'):attr('colspan', tostring(parentColSpan))
header = args['header' .. tostring(num)],
:addClass(imageclass)
label = args['label' .. tostring(num)],
:css('text-align', 'center')
data = args['data' .. tostring(num)],
:cssText(imagestyle)
datastyle = args.datastyle,
:wikitext(images[i])
class = args['class' .. tostring(num)],
if captions[i] ~= "" then
rowclass = args['rowclass' .. tostring(num)],
imageTd:tag('br')
-- @deprecated next; target .infobox-<name> rowclass
imageTd:tag('div'):cssText('margin-top: 3px')
rowstyle = args['rowstyle' .. tostring(num)],
imageTd:tag('span'):cssText(captionstyle)
rowcellstyle = args['rowcellstyle' .. tostring(num)]
:wikitext(captions[i])
})
end
end
out = out .. tostring(imageRow)
end
 
local function renderNavBar()
if not args.name then return end
 
has_rows = true
root
:tag('tr')
:tag('td')
:attr('colspan', '2')
:addClass('infobox-navbar')
:wikitext(require('Module:Navbar')._navbar{
args.name,
mini = 1,
})
end
 
local function renderItalicTitle()
local italicTitle = args['italic title'] and mw.ustring.lower(args['italic title'])
if italicTitle == '' or italicTitle == 'force' or italicTitle == 'yes' then
root:wikitext(require('Module:Italic title')._main({}))
end
end
 
-- Categories in otherwise empty rows are collected in empty_row_categories.
-- This function adds them to the module output. It is not affected by
-- args.decat because this module should not prevent module-external categories
-- from rendering.
local function renderEmptyRowCategories()
for _, s in ipairs(empty_row_categories) do
root:wikitext(s)
end
end
 
-- Render tracking categories. args.decat == turns off tracking categories.
local function renderTrackingCategories()
if args.decat == 'yes' then return end
if args.child == 'yes' then
if args.title then
root:wikitext(
'[[Category:Pages using embedded infobox templates with the title parameter]]'
)
end
end
elseif #(getArgNums('data')) == 0 and mw.title.getCurrentTitle().namespace == 0 then
root:wikitext('[[Category:Articles using infobox templates with no data rows]]')
end
end
--[=[
Loads the templatestyles for the infobox.
TODO: FINISH loading base templatestyles here rather than in
MediaWiki:Common.css. There are 4-5000 pages with 'raw' infobox tables.
See [[Mediawiki_talk:Common.css/to_do#Infobox]] and/or come help :).
When we do this we should clean up the inline CSS below too.
Will have to do some bizarre conversion category like with sidebar.
]=]
local function loadTemplateStyles()
local frame = mw.getCurrentFrame()
local hlist_templatestyles = ''
if lists.hlist_t.found then
hlist_templatestyles = frame:extensionTag{
name = 'templatestyles', args = { src = lists.hlist_t.styles }
}
end
end
local plainlist_templatestyles = ''
-- rows
if lists.plainlist_t.found then
local labelstyle = origArgs["labelstyle"] or ""
plainlist_templatestyles = frame:extensionTag{
local datastyle = origArgs["datastyle"] or ""
name = 'templatestyles', args = { src = lists.plainlist_t.styles }
local headerstyle = origArgs["headerstyle"] or ""
}
local elementIndex = 1
end
local headers = {}
local data = {}
-- See function description
local labels = {}
local base_templatestyles = frame:extensionTag{
local classes = {}
name = 'templatestyles', args = { src = 'Module:Infobox/styles.css' }
local styles = {}
}
local lblstyles = {}
local processingOrder = {}


local templatestyles = ''
for k,v in pairs(origArgs) do
if args['templatestyles'] then
local headerStart
templatestyles = frame:extensionTag{
local headerEnd
name = 'templatestyles', args = { src = args['templatestyles'] }
local labelStart
}
local labelEnd
end
local dataStart
local dataEnd
local child_templatestyles = ''
local styleStart
if args['child templatestyles'] then
local styleEnd
child_templatestyles = frame:extensionTag{
local lblStyleStart
name = 'templatestyles', args = { src = args['child templatestyles'] }
local lblStyleEnd
}
headerStart, headerEnd = mw.ustring.find(k, "header")
end
labelStart, labelEnd = mw.ustring.find(k, "label")
dataStart, dataEnd = mw.ustring.find(k, "data")
local grandchild_templatestyles = ''
styleStart, styleEnd = mw.ustring.find(k, "style")
if args['grandchild templatestyles'] then
lblStyleStart, lblStyleEnd = mw.ustring.find(k, "lblstyle")
grandchild_templatestyles = frame:extensionTag{
name = 'templatestyles', args = { src = args['grandchild templatestyles'] }
}
end
return table.concat({
-- hlist -> plainlist -> base is best-effort to preserve old Common.css ordering.
-- this ordering is not a guarantee because the rows of interest invoking
-- each class may not be on a specific page
hlist_templatestyles,
plainlist_templatestyles,
base_templatestyles,
templatestyles,
child_templatestyles,
grandchild_templatestyles
})
end
 
-- common functions between the child and non child cases
local function structure_infobox_common()
renderSubheaders()
renderImages()
preprocessRows()
renderRows()
renderBelowRow()
renderNavBar()
renderItalicTitle()
renderEmptyRowCategories()
renderTrackingCategories()
cleanInfobox()
end
 
-- Specify the overall layout of the infobox, with special settings if the
-- infobox is used as a 'child' inside another infobox.
local function _infobox()
if args.child ~= 'yes' then
root = mw.html.create('table')
 
root
:addClass(args.subbox == 'yes' and 'infobox-subbox' or 'infobox')
:addClass(args.bodyclass)
-- @deprecated next; target .infobox-<name>
:cssText(args.bodystyle)
has_list_class({ args.bodyclass })
local nr = ""
if dataStart == 1 then
nr = mw.ustring.sub(k, 1 + dataEnd, mw.ustring.len(k))
elseif labelStart == 1 then
nr = mw.ustring.sub(k, 1 + labelEnd, mw.ustring.len(k))
elseif headerStart == 1 then
nr = mw.ustring.sub(k, 1 + headerEnd, mw.ustring.len(k))
elseif styleStart == 1 then
nr = mw.ustring.sub(k, 1 + styleEnd, mw.ustring.len(k))
elseif lblStyleStart == 1 then
nr = mw.ustring.sub(k, 1 + lblStyleEnd, mw.ustring.len(k))
end


renderTitle()
if nr ~= "" and processingOrder[nr] == nil and tonumber(nr) ~= nil then
renderAboveRow()
headers[elementIndex] = origArgs["header" .. nr] or ""
else
labels[elementIndex] =  origArgs["label" .. nr] or ""
root = mw.html.create()
data[elementIndex] = origArgs["data" .. nr] or ""
 
classes[elementIndex] = origArgs["class" .. nr] or ""
root
styles[elementIndex] = origArgs["style" .. nr] or ""
:wikitext(args.title)
lblstyles[elementIndex] = origArgs["lblstyle" .. nr] or ""
end
processingOrder[tonumber(nr)] = elementIndex
structure_infobox_common()
elementIndex = elementIndex + 1
return loadTemplateStyles() .. root
end
 
-- If the argument exists and isn't blank, add it to the argument table.
-- Blank arguments are treated as nil to match the behaviour of ParserFunctions.
local function preprocessSingleArg(argName)
if origArgs[argName] and origArgs[argName] ~= '' then
args[argName] = origArgs[argName]
end
end
 
-- Assign the parameters with the given prefixes to the args table, in order, in
-- batches of the step size specified. This is to prevent references etc. from
-- appearing in the wrong order. The prefixTable should be an array containing
-- tables, each of which has two possible fields, a "prefix" string and a
-- "depend" table. The function always parses parameters containing the "prefix"
-- string, but only parses parameters in the "depend" table if the prefix
-- parameter is present and non-blank.
local function preprocessArgs(prefixTable, step)
if type(prefixTable) ~= 'table' then
error("Non-table value detected for the prefix table", 2)
end
if type(step) ~= 'number' then
error("Invalid step value detected", 2)
end
 
-- Get arguments without a number suffix, and check for bad input.
for i,v in ipairs(prefixTable) do
if type(v) ~= 'table' or type(v.prefix) ~= "string" or
(v.depend and type(v.depend) ~= 'table') then
error('Invalid input detected to preprocessArgs prefix table', 2)
end
preprocessSingleArg(v.prefix)
-- Only parse the depend parameter if the prefix parameter is present
-- and not blank.
if args[v.prefix] and v.depend then
for j, dependValue in ipairs(v.depend) do
if type(dependValue) ~= 'string' then
error('Invalid "depend" parameter value detected in preprocessArgs')
end
preprocessSingleArg(dependValue)
end
end
end
end
end


-- Get arguments with number suffixes.
local processingElement = 1
local a = 1 -- Counter variable.
while processingElement <= table.maxn(processingOrder) do
local moreArgumentsExist = true
elementIndex = processingOrder[processingElement]
while moreArgumentsExist == true do
if elementIndex ~= nil then
moreArgumentsExist = false
local crtHeader = headers[elementIndex]
for i = a, a + step - 1 do
local crtData = data[elementIndex]
for j,v in ipairs(prefixTable) do
local crtLabel = labels[elementIndex]
local prefixArgName = v.prefix .. tostring(i)
local crtLblStyle = lblstyles[elementIndex]
if origArgs[prefixArgName] then
local crtClass = classes[elementIndex]
-- Do another loop if any arguments are found, even blank ones.
local crtStyle = styles[elementIndex]
moreArgumentsExist = true
if crtHeader ~= "" then
preprocessSingleArg(prefixArgName)
local headerTr = mw.html.create('tr')
end
local headerTh = headerTr:tag('th')
-- Process the depend table if the prefix argument is present
:attr('colspan', tostring(parentColSpan))
-- and not blank, or we are processing "prefix1" and "prefix" is
:css('text-align', 'center')
-- present and not blank, and if the depend table is present.
:css('background-color', culoare_cadru)
if v.depend and (args[prefixArgName] or (i == 1 and args[v.prefix])) then
:css('color', culoare_text)
for j,dependValue in ipairs(v.depend) do
:addClass(crtClass)
local dependArgName = dependValue .. tostring(i)
:cssText(headerstyle)
preprocessSingleArg(dependArgName)
:wikitext(crtHeader)
end
out = out .. tostring(headerTr)
elseif crtLabel ~= "" then
if crtData ~= "" then
local dataAndLabelTr = mw.html.create('tr')
:tag('th'):cssText(labelstyle):cssText(crtLblStyle)
:wikitext(crtLabel):done()
:tag('td')
:attr('colspan', tostring(parentColSpan - 1))
:addClass(crtClass)
:cssText(datastyle):cssText(crtStyle)
:wikitext(crtData):allDone()
out = out .. tostring(dataAndLabelTr)
end
end
elseif crtData ~= "" then
local dataTr = mw.html.create('tr')
:tag('td'):attr('colspan', tostring(parentColSpan))
:addClass(crtClass)
:css('text-align', 'center')
:cssText(datastyle)
:cssText(crtStyle)
:wikitext(crtData):allDone()
out = out .. tostring(dataTr)
end
end
end
end
a = a + step
processingElement = processingElement + 1
end
end
end
 
--below
-- Parse the data parameters in the same order that the old {{infobox}} did, so
local belowstyle = origArgs["belowstyle"] or ""
-- that references etc. will display in the expected places. Parameters that
local below = origArgs["below"] or ""
-- depend on another parameter are only processed if that parameter is present,
if below ~= "" then
-- to avoid phantom references appearing in article reference lists.
out = out .. tostring(mw.html.create('tr')
local function parseDataParameters()
:tag('td')
 
:attr('colspan', tostring(parentColSpan))
preprocessSingleArg('autoheaders')
:css('text-align', 'center')
preprocessSingleArg('child')
:cssText(belowstyle)
preprocessSingleArg('bodyclass')
:wikitext(below))
preprocessSingleArg('subbox')
preprocessSingleArg('bodystyle')
preprocessSingleArg('title')
preprocessSingleArg('titleclass')
preprocessSingleArg('titlestyle')
preprocessSingleArg('above')
preprocessSingleArg('aboveclass')
preprocessSingleArg('abovestyle')
preprocessArgs({
{prefix = 'subheader', depend = {'subheaderstyle', 'subheaderrowclass'}}
}, 10)
preprocessSingleArg('subheaderstyle')
preprocessSingleArg('subheaderclass')
preprocessArgs({
{prefix = 'image', depend = {'caption', 'imagerowclass'}}
}, 10)
preprocessSingleArg('captionstyle')
preprocessSingleArg('imagestyle')
preprocessSingleArg('imageclass')
preprocessArgs({
{prefix = 'header'},
{prefix = 'data', depend = {'label'}},
{prefix = 'rowclass'},
{prefix = 'rowstyle'},
{prefix = 'rowcellstyle'},
{prefix = 'class'}
}, 50)
preprocessSingleArg('headerclass')
preprocessSingleArg('headerstyle')
preprocessSingleArg('labelstyle')
preprocessSingleArg('datastyle')
preprocessSingleArg('below')
preprocessSingleArg('belowclass')
preprocessSingleArg('belowstyle')
preprocessSingleArg('name')
-- different behaviour for italics if blank or absent
args['italic title'] = origArgs['italic title']
preprocessSingleArg('decat')
preprocessSingleArg('templatestyles')
preprocessSingleArg('child templatestyles')
preprocessSingleArg('grandchild templatestyles')
end
 
-- If called via #invoke, use the args passed into the invoking template.
-- Otherwise, for testing purposes, assume args are being passed directly in.
function p.infobox(frame)
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
else
origArgs = frame
end
end
parseDataParameters()
--tnavbar
local name = origArgs["name"] or ""
local navbar = origArgs["navbar"]
if name ~= "" and navbar ~= 'false' then
local navBarTr = mw.html.create('tr'):tag('td')
:css('text-align', 'right')
:attr('colspan', tostring(parentColSpan))
:wikitext(mw.getCurrentFrame():expandTemplate{title = "Tnavbar", args = { name }}):done()
out = out .. tostring(navBarTr)
end
if child ~= "yes" then
local doc = origArgs["doc"] or ""
if doc ~= "" then
local infodocTr = mw.html.create('tr'):wikitext(mw.getCurrentFrame():expandTemplate{title = "infodoc", args = {colspan = tostring(parentColSpan), culoare = culoare_cadru, link = doc, wikidata = wikidataEnabled }})
out = out .. tostring(infodocTr)
end
out = out .. "</table>"
end
return _infobox()
return out
end
end


-- For calling via #invoke within a template
p.infobox = function(frame)
function p.infoboxTemplate(frame)
local origArgs = getArgs(frame)
origArgs = {}
return p._infobox(origArgs)
for k,v in pairs(frame.args) do origArgs[k] = mw.text.trim(v) end
parseDataParameters()
return _infobox()
end
end
return p
return p

Versiunea curentă din 18 iulie 2024 11:50

Documentația acestui modul poate fi creată la Modul:Infobox/doc

Eroare în script: Lua error: Cannot create process: proc_open(/dev/null): Failed to open stream: Operation not permitted

--
-- This module implements {{Infobox}}
--
 
local p = {}
local getArgs = require('Modul:Arguments').getArgs
local StringUtils = require('Modul:StringUtils')
local TableTools = require('Modul:TableTools')
local args = {}

p._infobox = function(origArgs)

	local child = origArgs["child"] or origArgs["embed"] or "no"
	local bodyclass = origArgs["bodyclass"] or "infocaseta"
	local antet = origArgs["antet"] or "default"
	local aboveclass = origArgs["aboveclass"] or antet
	local abovestyle = origArgs["abovestyle"] or ""
	local culoare_cadru = origArgs["culoare cadru"] or "F5F5DC"
	if mw.ustring.match(culoare_cadru, '^%x%x%x$') or mw.ustring.match(culoare_cadru, '^%x%x%x%x%x%x$') then culoare_cadru = StringUtils._prependIfMissing({culoare_cadru, '#'}) end
	local culoare_text = origArgs["culoare text"] or "000000"
	if mw.ustring.match(culoare_text, '^%x%x%x$') or mw.ustring.match(culoare_text, '^%x%x%x%x%x%x$') then culoare_text = StringUtils._prependIfMissing({culoare_text, '#'}) end
	local titlestyle = origArgs["titlestyle"] or ""
	local title = origArgs["title"] or origArgs["titlu"] or ""
	title = StringUtils._capitalize({title})
	local above = origArgs["above"] or ""
	local parentColSpanArg = origArgs['parent_colspan']
	local parentColSpan
	if parentColSpanArg and mw.ustring.gsub(parentColSpanArg, '%d+', '', 1) == '' then
		parentColSpan = tonumber(parentColSpanArg)
	else
		parentColSpan = 2
	end
	
	local wikidataEnabled = origArgs["wikidata"] or ""
	local out = ""
	
	if child ~= "yes" then
		out = out .. "<table class=\"" .. bodyclass .. "\">"
		-- caption
		out = out .. tostring(mw.html.create('tr')
								:tag('td')
								:attr('colspan', '2')
								:addClass('antet ' .. aboveclass)
								:css('background-color', culoare_cadru)
								:css('color', culoare_text)
								:cssText(titlestyle)
								:wikitext(title))
		-- header
		if above ~= "" then
			local aboveTr = mw.html.create('tr'):tag('td')
				:attr('colspan', '2')
				:addClass(aboveclass)
				:css('text-align', 'center')
				:css('font-size', '125%')
				:css('font-weight', 'bold')
				:cssText(abovestyle)
				:wikitext(above):allDone()
			out = out .. tostring(aboveTr)
		end
	else
		if title ~= "" then
			local newTr = mw.html.create('tr'):tag('td')
				:attr('colspan', tostring(parentColSpan))
				:css('background-color', culoare_cadru)
				:css('color', culoare_text)
				:css('font-size', '125%')
				:css('margin-bottom', '2')
				:css('text-align', 'center')
				:css('line-height', '1.2em')
				:wikitext(title):allDone()
			out = out .. "\'\'\'" .. tostring(newTr) .. "\'\'\'"
		end
	end

	--subheaders
	local subheaders = {}
	subheaders[1] = origArgs["subheader"] or origArgs["subheader1"] or ""
	local subhIndex = 2
	while (origArgs["subheader" .. tostring(subhIndex)] or "") ~= "" do
		subheaders[subhIndex] = origArgs["subheader" .. tostring(subhIndex)] or ""
		subhIndex = subhIndex + 1
	end
	local subheaderstyle = origArgs["subheaderstyle"] or ""
	local subheaderclass = origArgs["subheaderclass"] or ""
	for subHeaderIdx = 1,#subheaders do
		if subheaders[subHeaderIdx] ~= "" then
			local subhTr = mw.html.create('tr'):tag('td'):attr('colspan', tostring(parentColSpan))
				:addClass(subheaderclass)
				:css('text-align', 'center')
				:cssText(subheaderstyle)
				:wikitext(subheaders[subHeaderIdx]):allDone()
			out = out .. tostring(subhTr)
		end
	end
	
	--images
	local imageIndices = TableTools.affixNums(origArgs, 'image')
	local images = {}
	local captions = {}
	if origArgs['image'] then
		table.insert(images, origArgs['image'] or '')
		table.insert(captions, origArgs['caption'] or '')
	end
	for _,imgIndex in ipairs(imageIndices) do
		table.insert(images, origArgs['image' .. tostring(imgIndex)])
		table.insert(captions, origArgs['caption' .. tostring(imgIndex)] or '')
	end

	local imageclass = origArgs["imageclass"]
	local imagestyle = origArgs["imagestyle"]
	local captionstyle = origArgs["captionstyle"]
	for i = 1,#images do
		if images[i] ~= "" then
			local imageRow = mw.html.create('tr')
			local imageTd = imageRow
				:tag('td'):attr('colspan', tostring(parentColSpan))
					:addClass(imageclass)
					:css('text-align', 'center')
					:cssText(imagestyle)
					:wikitext(images[i])
			if captions[i] ~= "" then
				imageTd:tag('br')
				imageTd:tag('div'):cssText('margin-top: 3px')
				imageTd:tag('span'):cssText(captionstyle)
					:wikitext(captions[i])
			end
			out = out .. tostring(imageRow)
		end
	end
	
	-- rows
	local labelstyle = origArgs["labelstyle"] or ""
	local datastyle = origArgs["datastyle"] or ""
	local headerstyle = origArgs["headerstyle"] or ""
	local elementIndex = 1
	local headers = {}
	local data = {}
	local labels = {}
	local classes = {}
	local styles = {}
	local lblstyles = {}
	local processingOrder = {}

	for k,v in pairs(origArgs) do
		local headerStart
		local headerEnd
		local labelStart
		local labelEnd
		local dataStart
		local dataEnd
		local styleStart
		local styleEnd
		local lblStyleStart
		local lblStyleEnd
		headerStart, headerEnd = mw.ustring.find(k, "header")
		labelStart, labelEnd = mw.ustring.find(k, "label")
		dataStart, dataEnd = mw.ustring.find(k, "data")
		styleStart, styleEnd = mw.ustring.find(k, "style")
		lblStyleStart, lblStyleEnd = mw.ustring.find(k, "lblstyle")
		
		local nr = ""
		if dataStart == 1 then
			nr = mw.ustring.sub(k, 1 + dataEnd, mw.ustring.len(k))
		elseif labelStart == 1 then
			nr = mw.ustring.sub(k, 1 + labelEnd, mw.ustring.len(k))
		elseif headerStart == 1 then
			nr = mw.ustring.sub(k, 1 + headerEnd, mw.ustring.len(k))
		elseif styleStart == 1 then
			nr = mw.ustring.sub(k, 1 + styleEnd, mw.ustring.len(k))
		elseif lblStyleStart == 1 then
			nr = mw.ustring.sub(k, 1 + lblStyleEnd, mw.ustring.len(k))
		end

		if nr ~= "" and processingOrder[nr] == nil and tonumber(nr) ~= nil then
			headers[elementIndex] = origArgs["header" .. nr] or ""
			labels[elementIndex] =  origArgs["label" .. nr] or ""
			data[elementIndex] = origArgs["data" .. nr] or ""
			classes[elementIndex] =  origArgs["class" .. nr] or ""
			styles[elementIndex] = origArgs["style" .. nr] or ""
			lblstyles[elementIndex] = origArgs["lblstyle" .. nr] or ""
			processingOrder[tonumber(nr)] = elementIndex
			elementIndex = elementIndex + 1
		end
	end

	local processingElement = 1
	while processingElement <= table.maxn(processingOrder) do
		elementIndex = processingOrder[processingElement]
		if elementIndex ~= nil then
			local crtHeader = headers[elementIndex]
			local crtData = data[elementIndex]
			local crtLabel = labels[elementIndex]
			local crtLblStyle = lblstyles[elementIndex]
			local crtClass = classes[elementIndex]
			local crtStyle = styles[elementIndex]
			if crtHeader ~= "" then
				local headerTr = mw.html.create('tr')
				local headerTh = headerTr:tag('th')
					:attr('colspan', tostring(parentColSpan))
					:css('text-align', 'center')
					:css('background-color', culoare_cadru)
					:css('color', culoare_text)
					:addClass(crtClass)
					:cssText(headerstyle)
					:wikitext(crtHeader)
				out = out .. tostring(headerTr)
			elseif crtLabel ~= "" then
				if crtData ~= "" then
					local dataAndLabelTr = mw.html.create('tr')
						:tag('th'):cssText(labelstyle):cssText(crtLblStyle)
							:wikitext(crtLabel):done()
						:tag('td')
							:attr('colspan', tostring(parentColSpan - 1))
							:addClass(crtClass)
							:cssText(datastyle):cssText(crtStyle)
							:wikitext(crtData):allDone()
					out = out .. tostring(dataAndLabelTr)
				end
			elseif crtData ~= "" then
				local dataTr = mw.html.create('tr')
					:tag('td'):attr('colspan', tostring(parentColSpan))
						:addClass(crtClass)
						:css('text-align', 'center')
						:cssText(datastyle)
						:cssText(crtStyle)
						:wikitext(crtData):allDone()
				out = out .. tostring(dataTr)
			end
		end
		processingElement = processingElement + 1
	end
	
	--below
	local belowstyle = origArgs["belowstyle"] or ""
	local below = origArgs["below"] or ""
	if below ~= "" then
		out = out .. tostring(mw.html.create('tr')
								:tag('td')
									:attr('colspan', tostring(parentColSpan))
									:css('text-align', 'center')
									:cssText(belowstyle)
									:wikitext(below))
	end
	
	--tnavbar
	local name = origArgs["name"] or ""
	local navbar = origArgs["navbar"]
	if name ~= "" and navbar ~= 'false' then
		local navBarTr = mw.html.create('tr'):tag('td')
			:css('text-align', 'right')
			:attr('colspan', tostring(parentColSpan))
			:wikitext(mw.getCurrentFrame():expandTemplate{title = "Tnavbar", args = { name }}):done()
		out = out .. tostring(navBarTr)
	end
	if child ~= "yes" then
		local doc = origArgs["doc"] or ""
		if doc ~= "" then
			local infodocTr = mw.html.create('tr'):wikitext(mw.getCurrentFrame():expandTemplate{title = "infodoc", args = {colspan = tostring(parentColSpan), culoare = culoare_cadru, link = doc, wikidata = wikidataEnabled }})
			out = out .. tostring(infodocTr)
		end
		out = out .. "</table>"
	end
	
	return out
end

p.infobox = function(frame)
	local origArgs = getArgs(frame)
	return p._infobox(origArgs)
end
return p