Module:Cs1 documentation support: Difference between revisions
Jump to navigation
Jump to search
Richardpruen (talk | contribs) m 1 revision imported |
script_lang_lister: 20em for lang cols wider than necessary; wastes too much space |
||
Line 3: | Line 3: | ||
local cfg = mw.loadData ('Module:Citation/CS1/Configuration'); -- load the configuration module | local cfg = mw.loadData ('Module:Citation/CS1/Configuration'); -- load the configuration module | ||
local whitelist = mw.loadData ('Module:Citation/CS1/Whitelist'); -- load the whitelist module | |||
local exclusion_lists = { -- TODO: move these tables into a separate ~/data module and mw.loadData() it | local exclusion_lists = { -- TODO: move these tables into a separate ~/data module and mw.loadData() it | ||
Line 217: | Line 219: | ||
list_format (result, name_list); | list_format (result, name_list); | ||
end | end | ||
local templatestyles = frame:extensionTag{ | |||
name = 'templatestyles', args = { src = "Div col/styles.css" } | |||
} | |||
table.sort (result); | table.sort (result); | ||
table.insert (result, 1, '<div class="div-col | table.insert (result, 1, templatestyles .. '<div class="div-col" style="column-width:16em">'); | ||
table.insert (out, table.concat (result, '\n*')); | table.insert (out, table.concat (result, '\n*')); | ||
table.insert (out, '</div>'); | table.insert (out, '</div>'); | ||
Line 239: | Line 245: | ||
]] | ]] | ||
local function script_lang_lister () | local function script_lang_lister (frame) | ||
local lang_code_src = cfg.script_lang_codes ; -- get list of allowed script language codes | local lang_code_src = cfg.script_lang_codes ; -- get list of allowed script language codes | ||
local override = cfg.lang_code_remap; | local override = cfg.lang_code_remap; | ||
Line 257: | Line 263: | ||
list_format (result, code_list); | list_format (result, code_list); | ||
local templatestyles = frame:extensionTag{ | |||
name = 'templatestyles', args = { src = "Div col/styles.css" } | |||
} | |||
table.sort (result); | table.sort (result); | ||
table.insert (result, 1, '<div class="div-col | table.insert (result, 1, templatestyles .. '<div class="div-col" style="column-width:16em">'); | ||
table.insert (out, table.concat (result, '\n*')); | table.insert (out, table.concat (result, '\n*')); | ||
table.insert (out, '</div>'); | table.insert (out, '</div>'); | ||
Line 662: | Line 672: | ||
local function cat_link_make (cat) | local function cat_link_make (cat) | ||
return table.concat ({'[[:Category:', cat, ']]'}); | return table.concat ({'[[:Category:', cat, ']]'}); | ||
end | |||
--[[--------------------------< S C R I P T _ C A T _ L I S T E R >-------------------------------------------- | |||
utility function to get script-language categories | |||
]] | |||
local lang_list_t = mw.language.fetchLanguageNames ('en', 'all'); | |||
local function script_cat_lister (script_lang_codes_t, lang_code_remap_t, cats_list_t) | |||
for _, lang_code in ipairs (script_lang_codes_t) do | |||
local lang_name = lang_code_remap_t[lang_code] or lang_list_t[lang_code]; -- use remap table to get Bengali instead of Bangla and the like; else use standard MediaWiki names | |||
local cat = 'CS1 uses ' .. lang_name .. '-language script (' .. lang_code .. ')'; -- build a category name | |||
cats_list_t[cat] = 1; -- and save it | |||
end | |||
end | end | ||
Line 738: | Line 765: | ||
list_live_cats[cat] = 1; -- add to the list | list_live_cats[cat] = 1; -- add to the list | ||
end | end | ||
script_cat_lister (cfg.script_lang_codes, cfg.lang_code_remap, list_live_cats); -- get live module's foriegn language script cats | |||
if sandbox then -- if ~/sandbox module exists and |sandbox= not set to 'no' | if sandbox then -- if ~/sandbox module exists and |sandbox= not set to 'no' | ||
for _, cat in pairs (sb_cfg.prop_cats) do -- get the | for _, cat in pairs (sb_cfg.prop_cats) do -- get the sandbox module's categories | ||
cat = cat:gsub ('|(.*)$', ''); -- strip sort key if any | cat = cat:gsub ('|(.*)$', ''); -- strip sort key if any | ||
list_sbox_cats[cat] = 1; -- add to the list | list_sbox_cats[cat] = 1; -- add to the list | ||
end | end | ||
script_cat_lister (sb_cfg.script_lang_codes, sb_cfg.lang_code_remap, list_sbox_cats); -- get sandbox module's foriegn language script cats | |||
end | end | ||
else | else | ||
Line 784: | Line 815: | ||
')' -- close | ')' -- close | ||
}) | }) | ||
local templatestyles = frame:extensionTag{ | |||
name = 'templatestyles', args = { src = "Div col/styles.css" } | |||
} | |||
header = table.concat ({ -- make a useable header | header = table.concat ({ -- make a useable header | ||
_header_make ({header, hdr_lvl}), | _header_make ({header, hdr_lvl}), | ||
'\n<div class="div-col | '\n' .. templatestyles .. '<div class="div-col">' -- opening <div> for columns | ||
}); | }); | ||
Line 804: | Line 839: | ||
header = table.concat ({ -- make a useable header | header = table.concat ({ -- make a useable header | ||
_header_make ({header, hdr_lvl+1}), | _header_make ({header, hdr_lvl+1}), | ||
'\n<div class="div-col | '\n' .. templatestyles .. '<div class="div-col">' | ||
}); | }); | ||
Line 822: | Line 857: | ||
header = table.concat ({ -- make a useable header | header = table.concat ({ -- make a useable header | ||
_header_make ({header, hdr_lvl+1}), | _header_make ({header, hdr_lvl+1}), | ||
'\n<div class="div-col | '\n' .. templatestyles .. '<div class="div-col">' | ||
}); | }); | ||
Line 913: | Line 948: | ||
out_str = frame:expandTemplate ({title='resize', args = {'120%', out_str} }) | out_str = frame:expandTemplate ({title='resize', args = {'120%', out_str} }) | ||
return table.concat ({out_str, frame:extensionTag ('templatestyles', '', {src='Module:Citation/CS1/styles.css'})}); | return table.concat ({out_str, frame:extensionTag ('templatestyles', '', {src='Module:Citation/CS1/styles.css'})}); | ||
end | |||
--[[--------------------------< T E M P L A T E S _ T >-------------------------------------------------------- | |||
This table is a k/v table of sequence tables. The keys in this table are collapsed lowercase form of the cs1|2 | |||
template names ({{ROOTPAGENAME}}): | |||
Template:Cite AV media -> citeavmedia | |||
Each subsequence table holds: | |||
[1] documentation page where the TemplateData json is stored ({{cite book}} is the oddball) | |||
[2] key to 'preprint_arguments' and unique_arguments' tables in Module:Citation/CS1/Whitelist; these keys | |||
dictate which of the basic or limited arguments and numbered arguments tables will be used to validate | |||
the content of the TemplateData | |||
]] | |||
local templates_t = { | |||
citearxiv = {'Template:Cite_arXiv/doc', 'arxiv'}, -- preprint arguments | |||
citeavmedia = {'Template:Cite AV media/doc', 'audio-visual'}, -- unique arguments | |||
citeavmedianotes = {'Template:Cite AV media notes/doc'}, -- no template data | |||
citebiorxiv = {'Template:Cite bioRxiv/doc', 'biorxiv'}, -- preprint arguments | |||
citebook = {'Template:Cite book/TemplateData'}, | |||
citeciteseerx = {'Template:Cite citeseerx/doc', 'citeseerx'}, -- no template data; preprint uses limited arguments | |||
citeconference = {'Template:Cite conference/doc'}, | |||
citeencyclopedia = {'Template:Cite encyclopedia/doc'}, | |||
citeepisode = {'Template:Cite episode/doc', 'episode'}, -- unique arguments | |||
citeinterview = {'Template:Cite interview/doc'}, | |||
citejournal = {'Template:Cite journal/doc'}, | |||
citemagazine = {'Template:Cite magazine/doc'}, | |||
citemailinglist = {'Template:Cite mailing list/doc', 'mailinglist'}, -- unique arguments -- no template data | |||
citemap = {'Template:Cite map/doc', 'map'}, -- unique arguments -- no template data | |||
citenews = {'Template:Cite news/doc'}, | |||
citenewsgroup = {'Template:Cite newsgroup/doc', 'newsgroup'}, -- unique arguments | |||
citepodcast = {'Template:Cite podcast/doc'}, | |||
citepressrelease = {'Template:Cite press release/doc'}, | |||
citereport = {'Template:Cite report/doc'}, | |||
citeserial = {'Template:Cite serial/doc', 'serial'}, -- unique arguments -- no template data | |||
citesign = {'Template:Cite sign/doc'}, | |||
citespeech = {'Template:Cite speech/doc'}, -- no template data | |||
citessrn = {'Template:Cite ssrn/doc', 'ssrn'}, -- preprint arguments -- no template data | |||
citetechreport = {'Template:Cite techreport/doc'}, | |||
citethesis = {'Template:Cite thesis/doc'}, | |||
citeweb = {'Template:Cite web/doc'}, | |||
citation = {'Template:Citation/doc'}, | |||
} | |||
--[[--------------------------< I D E N T I F I E R _ A L I A S E S _ T >-------------------------------------- | |||
a table of the identifier aliases | |||
]] | |||
local identifier_aliases_t = {} | |||
for identifier, handler in pairs (cfg.id_handlers) do -- for each identifier | |||
local aliases_t = {}; -- create a table | |||
for _, alias in ipairs (handler.parameters) do -- get the alaises | |||
aliases_t[alias] = true; -- and add them to the table in a form that mimics the whitelist tables | |||
end | |||
identifier_aliases_t[identifier:lower()] = aliases_t; -- add new table to the identifier aliases table; use lowercase identifier base name for the key | |||
end | |||
--[[--------------------------< T E M P L A T E _ D A T A _ J S O N _ G E T >---------------------------------- | |||
get template doc page content and extract the content of the TemplateData tags (case insensitive) | |||
<template> is the canonical name of the template doc page (with namespace) that holds the template data; usually | |||
Template:Cite xxx/doc (except Template:Cite book/TemplateData) | |||
]] | |||
local function template_data_json_get (template) | |||
local json = mw.title.new (template):getContent() or ''; -- get the content of the article or ''; new pages edited w/ve do not have 'content' until saved; ve does not preview; phab:T221625 | |||
json = json:match ('<[Tt]emplate[Dd]ata>(.-)</[Tt]emplate[Dd]ata>'); -- remove everything exept the content of the TemplatData tags | |||
return json and mw.text.jsonDecode (json); -- decode the json string and return as a table; nil if not found | |||
end | |||
--[[--------------------------< V A L I D A T E _ U N I Q U E _ P A R A M >------------------------------------ | |||
looks for <param> (can be the canonical parameter name or can be an alias) in whitelist.basic_arguments{} and if | |||
necessary in whitelist.numbered_arguments{}. When found, returns true; nil else | |||
<param> is the parameter's name as listed in the TemplateData | |||
]] | |||
local function validate_basic_param (param) | |||
if true == whitelist.basic_arguments[param] or true == whitelist.numbered_arguments[param] then | |||
return true; | |||
end | |||
end | |||
--[[--------------------------< V A L I D A T E _ P R E P R I N T _ P A R A M >-------------------------------- | |||
looks for <param> (can be the canonical parameter name or can be an alias) in whitelist.preprint_arguments{} or | |||
whitelist.limited_basic_arguments{} or whitelist.limited_numbered_arguments{}. When found, returns true; nil else | |||
<param> is the parameter's name as listed in the TemplateData | |||
<key> is key neccessary to look in the appropriate subtable of whitelist.preprint_arguments{} | |||
]] | |||
local function validate_preprint_param (param, key) | |||
if true == whitelist.preprint_arguments[key][param] or | |||
true == whitelist.limited_basic_arguments[param] or | |||
true == whitelist.limited_numbered_arguments[param] then | |||
return true; | |||
end | |||
end | |||
--[[--------------------------< V A L I D A T E _ U N I Q U E _ P A R A M >------------------------------------ | |||
looks for <param> (can be the canonical parameter name or can be an alias) in whitelist.unique_arguments{} or | |||
whitelist.basic_arguments{} or whitelist.numbered_arguments{}. When found, returns true; nil else | |||
<param> is the parameter's name as listed in the TemplateData | |||
<key> is key neccessary to look in the appropriate subtable of whitelist.unique_arguments{} | |||
]] | |||
local function validate_unique_param (param, key) | |||
if true == whitelist.unique_arguments[key][param] or true == validate_basic_param (param) then | |||
return true; | |||
end | |||
end | |||
--[[--------------------------< V A L I D A T E _ I D _ P A R A M >-------------------------------------------- | |||
looks for <param> <alias> in identifier_aliases_t{}. When found, returns true; nil else | |||
<param> is the parameter's name as listed in the TemplateData | |||
<alias> is the alias that we're looking for | |||
]] | |||
local function validate_id_alias (param, alias) | |||
return identifier_aliases_t[param] and identifier_aliases_t[param][alias]; | |||
end | |||
--[[--------------------------< P A R A M _ E R R O R_ M S G >------------------------------------------------- | |||
]] | |||
local function param_error_msg (param) | |||
return '<code style="color: inherit; background: inherit; border: none; padding: inherit">|' .. param .. '=</code> not valid parameter'; | |||
end | |||
--[[--------------------------< A L I A S _ E R R O R_ M S G >------------------------------------------------- | |||
]] | |||
local function alias_error_msg (param, alias) | |||
return '<code style="color: inherit; background: inherit; border: none; padding: inherit">|' .. alias .. '=</code> not valid alias of: <code style="color: inherit; background: inherit; border: none; padding: inherit">|' .. param .. '=</code>'; | |||
end | |||
--[[--------------------------< T E M P L A T E _ D A T A _ V A L I D A T E >---------------------------------- | |||
compairs parameter names listed in a cs1|2 template's TemplateData structure (everything between <TemplateData> | |||
and </TemplateData> tag case insensitive). Returns error messages when errors found, empty string else. | |||
{{#invoke:Cs1 documentation support|template_data_validate|{{ROOTPAGENAME}}}} | |||
When called from a different page: | |||
{{#invoke:Sandbox/trappist the monk/template data|template_data_validate|<canonical template name>}} | |||
where the <canonical template name> is the template's canonical name with or without namespace and or subpages | |||
]] | |||
local function template_data_validate (frame) | |||
local args_t = getArgs (frame); | |||
if not args_t[1] then | |||
return '<span style=\"font-size: 100%; font-style: normal;\" class=\"error\">Error: cs1|2 template name required</span>'; | |||
end | |||
local template_idx = args_t[1]:lower():match ('cit[ae][^/]+'); -- args_t[1] has something | |||
if not template_idx then -- but if not a cs1|2 template abandon with error message | |||
return '<span style=\"font-size: 100%; font-style: normal;\" class=\"error\">Error: cs1|2 template name required</span>'; | |||
else | |||
template_idx = template_idx:gsub (' ', ''); -- is what appears to be a cs1|2 template so strip spaces | |||
end | |||
local template_t = templates_t[template_idx]; | |||
local out = {}; | |||
local template_doc = template_t[1]; | |||
local json_t = template_data_json_get (template_doc); | |||
if not json_t then | |||
table.insert (out, 'Error: can\'t find TemplateData'); | |||
else | |||
for param, param_t in pairs (json_t['params']) do | |||
local param_i = param:gsub ('%d+', '#'); -- in case an enumerated parameter, convert the enumerate digits to a single '#' character | |||
local param_is_valid; -- boolean true when param is valid; nil else | |||
if template_t[2] then -- if template is a preprint or uses unique parameters | |||
if whitelist.preprint_arguments[template_t[2]] then -- if a preprint template | |||
param_is_valid = validate_preprint_param (param_i, template_t[2]); | |||
if param_is_valid then | |||
if param_t['aliases'] then | |||
for _, alias in ipairs (param_t['aliases']) do | |||
local alias_i = alias:gsub ('%d+', '#'); -- in case an enumerated parameter, convert the enumerator digits to a single '#' character | |||
if not validate_preprint_param (alias_i, template_t[2]) then | |||
table.insert (out, alias_error_msg (param, alias)); | |||
end | |||
end | |||
end | |||
else -- here when param not valid preprint param | |||
table.insert (out, param_error_msg (param)) | |||
end | |||
elseif whitelist.unique_arguments[template_t[2]] then -- if a unique parameters template | |||
param_is_valid = validate_unique_param (param_i, template_t[2]); | |||
if param_is_valid then | |||
if param_t['aliases'] then | |||
for _, alias in ipairs (param_t['aliases']) do | |||
local alias_i = alias:gsub ('%d+', '#'); -- in case an enumerated parameter, convert the enumerate digits to a single '#' character | |||
if not validate_unique_param (alias_i, template_t[2]) then | |||
table.insert (out, alias_error_msg (param, alias)); | |||
end | |||
end | |||
end | |||
else -- here when param not valid unique parameter | |||
table.insert (out, param_error_msg (param)) | |||
end | |||
else -- should never be here if coder is doing the right thing ... | |||
table.insert (out, 'internal error: unexpected keyword in templates_t: ' .. template_t[2]); | |||
break; | |||
end | |||
else -- here when not unique or preprint | |||
param_is_valid = validate_basic_param (param_i); | |||
if param_is_valid then | |||
if param_t['aliases'] then | |||
for _, alias in ipairs (param_t['aliases']) do | |||
local alias_i = alias:gsub ('%d+', '#'); -- in case an enumerated parameter, convert the enumerate digits to a single '#' character | |||
if not validate_basic_param (alias_i) and not validate_id_alias (param, alias) then -- for isbn13 (while still supported) must not mask the digits | |||
table.insert (out, alias_error_msg (param, alias)); | |||
end | |||
end | |||
end | |||
else -- here when param not valid | |||
table.insert (out, param_error_msg (param)) | |||
end | |||
end | |||
end | |||
end | |||
if 0 ~= #out then | |||
table.sort (out); | |||
out[1] = '*' .. out[1]; -- add a splat to the first error message | |||
return table.concat ({'[[' .. template_doc .. ']] TemplateData has errors:<div style=\"font-size: 100%; font-style: normal;\" class=\"error\">\n', table.concat (out, '\n*'), '</div>'}); | |||
else | |||
return; -- no errors detected; return nothing | |||
end | |||
end | end | ||
Line 933: | Line 1,231: | ||
lang_lister = lang_lister, | lang_lister = lang_lister, | ||
script_lang_lister = script_lang_lister, | script_lang_lister = script_lang_lister, | ||
template_data_validate = template_data_validate, | |||
}; | }; |