Modul:Header/attribution
Documentația acestui modul poate fi creată la Modul:Header/attribution/doc
require('strict')
local p = {}
local yesno = require('Modul:Yesno')
local TableTools = require('Modul:TableTools')
local contrib_types = require('Modul:Header/attribution data')
p.attr_data = {}
for k, v in pairs(contrib_types) do
p.attr_data[v['param_name']] = v
end
local function get_attribution_params(args)
local all_contributors = {
title = {},
section = {}
}
for k, v in pairs(args) do
local base_param_name = string.gsub(string.gsub(string.gsub(k, '%-display$', ''), '%-nolink$', ''), '%d*$', '')
local arg_param_name = string.gsub(base_param_name, '^override%-', '')
local param_name = string.gsub(arg_param_name, '^section%-', '')
local section_key
if string.match(arg_param_name, '^section%-') then
section_key = 'section'
else
section_key = 'title'
end
if p['attr_data'][param_name] then
local param_name_pattern = '^' .. string.gsub(base_param_name, '%-', '%%%-')
local nText = string.match(k, param_name_pattern .. '%d*$')
local nDisplay = string.match(k, param_name_pattern .. '%d*%-display$')
local nNoLink = string.match(k, param_name_pattern .. '%d*%-nolink$')
local n
local info
if nText then
n = string.gsub(nText, param_name_pattern .. '(%d*)$', '%1')
n = tonumber(n) or 1
info = 'text'
elseif nDisplay then
n = string.gsub(nDisplay, param_name_pattern .. '(%d*)%-display$', '%1')
n = tonumber(n) or 1
info = 'display'
elseif nNoLink then
n = string.gsub(nNoLink, param_name_pattern .. '(%d*)%-nolink$', '%1')
n = tonumber(n) or 1
info = 'nolink'
end
local index = p['attr_data'][param_name]['index']
all_contributors[section_key][index] = all_contributors[section_key][index] or {}
all_contributors[section_key][index]['param_name'] = all_contributors[section_key][index]['param_name'] or param_name
all_contributors[section_key][index]['arg_param_name'] = all_contributors[section_key][index]['arg_param_name'] or arg_param_name
all_contributors[section_key][index]['override'] = all_contributors[section_key][index]['override'] or args['override-' .. arg_param_name]
if n and not (args.template_name == 'Titlu traducere' and arg_param_name == 'traducător' and info == 'text' and string.lower(v) == 'wikisource') then
all_contributors[section_key][index]['values'] = all_contributors[section_key][index]['values'] or {}
all_contributors[section_key][index]['values'][n] = all_contributors[section_key][index]['values'][n] or {}
all_contributors[section_key][index]['values'][n][info] = all_contributors[section_key][index]['values'][n][info] or v
end
end
end
if args.template_name == 'Titlu traducere' then
local index = p['attr_data']['traducător']['index']
all_contributors['title'][index] = all_contributors['title'][index] or {}
all_contributors['title'][index]['param_name'] = all_contributors['title'][index]['param_name'] or 'traducător'
all_contributors['title'][index]['arg_param_name'] = all_contributors['title'][index]['arg_param_name'] or 'traducător'
all_contributors['title'][index]['override'] = all_contributors['title'][index]['override'] or nil
all_contributors['title'][index]['values'] = all_contributors['title'][index]['values'] or {}
table.insert(all_contributors['title'][index]['values'], {text = 'wikisource'})
end
local contributors = {
title = {},
section = {}
}
for section_key, section_values in pairs(all_contributors) do
for i, contrib in ipairs(TableTools.compressSparseArray(section_values)) do
local new_contrib = {}
for k, v in pairs(TableTools.shallowClone(contrib)) do
if k == 'values' then
new_contrib[k] = TableTools.compressSparseArray(v)
else
new_contrib[k] = v
end
end
table.insert(contributors[section_key], new_contrib)
end
end
return contributors
end
function p.get_attribution_params(args)
return get_attribution_params(args)
end
local function construct_attribution_span(args)
local aspan = mw.html.create('span')
:addClass('vcard')
:attr('id', 'header-' .. args.span_param_name .. '-text')
:tag('span'):addClass('fn'):wikitext(args.atext)
return tostring(mw.html.create('span')
:addClass('contributor-text')
:wikitext(args.prefix .. tostring(aspan))
)
end
local function construct_attribution(args)
local data = args.data
local param_name = data['param_name']
local span_param_name = param_name
local prefix = p['attr_data'][param_name]['prefix'] .. ' '
if data['arg_param_name'] == 'autor-secțiune' then
span_param_name = 'contribuitor'
end
if data['arg_param_name'] == 'autor' and data['override'] then
prefix = '' -- legacy parameter
elseif param_name == 'traducător' and args.languages then
prefix = 'traducere din ' .. (args.language_name or 'o limbă nerecunoscută') .. ' de '
end
-- override (legacy parameter)
local atext = data['override']
if atext then
if args.template_name == 'Titlu traducere' and param_name == 'traducător' then
atext = atext .. ' și ' .. p['attr_data']['traducător']['special_cases']['wikisource']
end
return construct_attribution_span({span_param_name = span_param_name, prefix = prefix, atext = atext})
end
local contributors = {}
for i, contributor in ipairs(data['values']) do
local text = contributor['text']
local display = contributor['display']
local nolink = yesno(contributor['nolink']) or false
if nolink then
table.insert(contributors, display or text)
elseif text then
local special = false
for k, v in pairs(p['attr_data'][param_name]['special_cases']) do
if string.lower(text) == k then
special = true
if type(v) == 'function' then
display = v(display)
else
display = v
end
break
end
end
if not special then
if not display then
display = string.gsub(text, ' %(.*%)$', '')
end
display = '[[Autor:' .. text .. '|' .. display .. ']]'
end
if display then
table.insert(contributors, display)
end
else
mw.logObject(contributor)
end
end
if #contributors == 0 then
return nil
elseif #contributors == 1 then
atext = contributors[1]
else
atext = table.concat(contributors, ', ', 1, #contributors - 1) .. ' și ' .. contributors[#contributors]
end
-- Temporary check for illustrator in subpages.
-- TODO: remove once
-- [[Special:PermanentLink/14324127#Replace illustrator header parameter with section illustrator in subpages of works]]
-- has been addressed.
local cat = ''
if param_name == 'ilustrator' and mw.title.getCurrentTitle().isSubpage then
cat = '[[' .. 'Categorie:' .. 'Subpagini care folosesc parametrul ilustrator' .. ']]'
end
local s = construct_attribution_span({span_param_name = span_param_name, prefix = prefix, atext = atext})
return s .. cat
end
function p.construct_attributions(args)
local attributions = {}
local a_args = TableTools.shallowClone(args)
local attr_params = get_attribution_params(args)['title']
for i, data in ipairs(attr_params) do
a_args['data'] = data
local atext = construct_attribution(a_args)
if atext then
table.insert(attributions, atext)
end
end
if #attributions == 0 then
return ''
end
return table.concat(attributions, ', ')
end
-- section
function p.construct_section(args)
local section_text = args['secțiune']
if not section_text or section_text == '' then
return ''
end
local attributions = {}
local a_args = TableTools.shallowClone(args)
local attr_params = get_attribution_params(args)['section']
for i, data in ipairs(attr_params) do
a_args['data'] = data
local atext = construct_attribution(a_args)
if atext then
table.insert(attributions, atext)
end
end
if #attributions > 0 then
local sep = ' '
if #attributions > 1 then
sep = '<br/>'
end
section_text = section_text .. sep .. table.concat(attributions, ', ')
end
return tostring(mw.html.create('div')
:addClass('header-section-text')
:wikitext(section_text)
)
end
--[=[
Construct the [[Help:Microformat]] for the page.
This is in the form:
<div id="ws-data" ...>
<span id="ws-title">Title here...</span>
...
<div>
]=]
function p.construct_microformat(args)
local mf_div = mw.html.create('div')
:addClass('ws-noexport')
:attr('id', 'ws-data')
:css({speak = 'none'})
-- override to show the microformat
if yesno(args['show-microformat']) then
mf_div:addClass('ws-data-show')
end
-- collect the MF values here
local mf = {};
-- page ID
mf['ws-article-id'] = mw.title.getCurrentTitle().id
-- title
if args['titlu'] then
mf['ws-title'] = args['titlu']
-- append section if there is one
if args['secțiune'] then
mf['ws-title'] = mf['ws-title'] .. " — " .. args['secțiune']
end
end
-- year
mf['ws-year'] = args['an']
-- cover
mf['ws-cover'] = args['copertă']
-- use section values if available, otherwise use work values
local attr_params = get_attribution_params(args)
local joined_attr_params = {}
for i, section_key in ipairs({'section', 'title'}) do
for i, data in ipairs(attr_params[section_key]) do
local index = p['attr_data'][data['param_name']]['index']
joined_attr_params[index] = joined_attr_params[index] or data
end
end
joined_attr_params = TableTools.compressSparseArray(joined_attr_params)
for i, data in ipairs(joined_attr_params) do
local atext = data['override']
local ws_translator = args.template_name == 'Titlu traducere' and data['param_name'] == 'traducător'
if atext and ws_translator then
atext = atext .. ' și Wikisource'
elseif not atext then
local attr_text = {}
for i, values in ipairs(data['values']) do
local display = values['text']
local special = false
if values['text'] then
for k, v in pairs(p['attr_data'][data['param_name']]['special_cases']) do
if string.lower(values['text']) == k then
special = true
if k == 'anon' or k == 'anonim' then
display = 'Anonim'
elseif k == 'wikisource' then
display = 'Wikisource'
elseif type(v) == 'function' then
display = v(display)
else
display = v
end
break
end
end
else
mw.logObject(values)
end
table.insert(attr_text, display)
end
if #attr_text == 1 then
atext = attr_text[1]
elseif #attr_text > 1 then
atext = table.concat(attr_text, ', ', 1, #attr_text - 1) .. ' și ' .. attr_text[#attr_text]
end
end
if atext then
mf['ws-' .. data['param_name']] = atext
end
end
for k, v in pairs(mf) do
mf_div:tag('span'):attr('id', k):wikitext(v)
end
return tostring(mf_div)
end
return p