Module:Cs1 documentation support: Difference between revisions
Johnrdorazio (talk | contribs) m (1 revision imported) |
No edit summary |
||
Line 217: | Line 217: | ||
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:20em">'); | ||
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 243: | ||
]] | ]] | ||
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 261: | ||
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:20em">'); | ||
table.insert (out, table.concat (result, '\n*')); | table.insert (out, table.concat (result, '\n*')); | ||
table.insert (out, '</div>'); | table.insert (out, '</div>'); | ||
Line 615: | Line 623: | ||
]] | ]] | ||
local function | local function _header_make (args) | ||
if not args[1] then | if not args[1] then | ||
return ''; -- no header text | return ''; -- no header text | ||
Line 626: | Line 632: | ||
level = string.rep ('=', level); | level = string.rep ('=', level); | ||
return level .. args[1] .. level; | return level .. args[1] .. level; | ||
end | |||
--[[--------------------------< H E A D E R _ M A K E >-------------------------------------------------------- | |||
Entry from an {{#invoke:}} | |||
makes a section header from <header_text> and <level>; <level> defaults to 2; cannot be less than 2 | |||
]] | |||
local function header_make (frame) | |||
local args = getArgs (frame); | |||
return _header_make (args); | |||
end | end | ||
Line 631: | Line 650: | ||
--[[--------------------------< I D _ L I M I T S _ G E T >---------------------------------------------------- | --[[--------------------------< I D _ L I M I T S _ G E T >---------------------------------------------------- | ||
return the limit values for named identifier parameters that have <id> limits (pmc, pmid, ssrn, s2cid); the return | return the limit values for named identifier parameters that have <id> limits (pmc, pmid, ssrn, s2cid, oclc, osti, rfc); the return | ||
value used in template documentation and error message help-text | value used in template documentation and error message help-text | ||
Line 642: | Line 661: | ||
local handlers = cfg.id_handlers; -- get id_handlers {} table from ~/Configuration | local handlers = cfg.id_handlers; -- get id_handlers {} table from ~/Configuration | ||
return args[1] and handlers[args[1]:upper()].id_limit or ''; | return args[1] and handlers[args[1]:upper()].id_limit or ('<span style="font-size:100%" class="error">No limit defined for identifier: ' .. (args[1] or '<unknown name>') .. '</span>'); | ||
end | |||
--[[--------------------------< C A T _ L I N K _ M A K E >---------------------------------------------------- | |||
]] | |||
local function cat_link_make (cat) | |||
return table.concat ({'[[:Category:', cat, ']]'}); | |||
end | |||
--[[--------------------------< C S 1 _ C A T _ L I S T E R >-------------------------------------------------- | |||
This is a crude tool that reads the category names from Module:Citation/CS1/Configuration, makes links of them, | |||
and then lists them in sorted lists. A couple of parameters control the rendering of the output: | |||
|select= -- (required) takes one of three values: error, maint, prop | |||
|sandbox= -- takes one value: no | |||
|hdr-lvl= -- base header level (number of == that make a header); default:2 min:2 | |||
This tool will automatically attempt to load a sandbox version of ~/Configuration if one exists. | |||
Setting |sandbox=no will defeat this. | |||
{{#invoke:cs1 documentation support|cat_lister|select=<error|maint|prop>|sandbox=<no>}} | |||
]] | |||
local function cat_lister (frame) | |||
local args = getArgs (frame); | |||
local list_live_cats = {}; -- list of live categories | |||
local list_sbox_cats = {}; -- list of sandbox categories | |||
local live_sbox_out = {} -- list of categories that are common to live and sandbox modules | |||
local live_not_in_sbox_out = {} -- list of categories in live but not sandbox | |||
local sbox_not_in_live_out = {} -- list of categories in sandbox but not live | |||
local out = {}; -- final output assembled here | |||
local sandbox; -- boolean; true: evaluate the sandbox module | |||
local hdr_lvl; -- | |||
local sb_cfg; | |||
local sandbox, sb_cfg = pcall (mw.loadData, 'Module:Citation/CS1/Configuration/sandbox'); -- get sandbox configuration | |||
local cat; | |||
local select = args.select; | |||
if 'no' == args.sandbox then -- list sandbox? | |||
sandbox = false; -- no, live only | |||
end | |||
if hdr_lvl then -- if set and | |||
if tonumber (hdr_lvl) then -- can be converted to number | |||
if 2 > tonumber (hdr_lvl) then -- min is 2 | |||
hdr_lvl = 2; -- so set to min | |||
end | |||
else -- can't be converted | |||
hdr_lvl = 2; -- so default to min | |||
end | |||
else | |||
hdr_lvl = 2; -- not set so default to min | |||
end | |||
if 'error' == select or 'maint' == select then -- error and main categorys handling different from poperties cats | |||
for _, t in pairs (cfg.error_conditions) do -- get the live module's categories | |||
if ('error' == select and t.message) or ('maint' == select and not t.message) then | |||
cat = t.category:gsub ('|(.*)$', ''); -- strip sort key if any | |||
list_live_cats[cat] = 1; -- add to the list | |||
end | |||
end | |||
if sandbox then -- if ~/sandbox module exists and |sandbox= not set to 'no' | |||
for _, t in pairs (sb_cfg.error_conditions) do -- get the sandbox module's categories | |||
if ('error' == select and t.message) or ('maint' == select and not t.message) then | |||
cat = t.category:gsub ('|(.*)$', ''); -- strip sort key if any | |||
list_sbox_cats[cat] = 1; -- add to the list | |||
end | |||
end | |||
end | |||
elseif 'prop' == select then -- prop cats | |||
for _, cat in pairs (cfg.prop_cats) do -- get the live module's categories | |||
cat = cat:gsub ('|(.*)$', ''); -- strip sort key if any | |||
list_live_cats[cat] = 1; -- add to the list | |||
end | |||
if sandbox then -- if ~/sandbox module exists and |sandbox= not set to 'no' | |||
for _, cat in pairs (sb_cfg.prop_cats) do -- get the live module's categories | |||
cat = cat:gsub ('|(.*)$', ''); -- strip sort key if any | |||
list_sbox_cats[cat] = 1; -- add to the list | |||
end | |||
end | |||
else | |||
return '<span style=\"font-size:100%; font-style:normal;\" class=\"error\">error: unknown selector: ' .. select .. '</span>' | |||
end | |||
for k, _ in pairs (list_live_cats) do -- separate live/sbox common cats from cats not in sbox | |||
if not list_sbox_cats[k] and sandbox then | |||
table.insert (live_not_in_sbox_out, cat_link_make (k)); -- in live but not in sbox | |||
else | |||
table.insert (live_sbox_out, cat_link_make (k)); -- in both live and sbox | |||
end | |||
end | |||
for k, _ in pairs (list_sbox_cats) do -- separate sbox/live common cats from cats not in live | |||
if not list_live_cats[k] then | |||
table.insert (sbox_not_in_live_out, cat_link_make (k)); -- in sbox but not in live | |||
end | |||
end | |||
local function comp (a, b) -- local function for case-agnostic category name sorting | |||
return a:lower() < b:lower(); | |||
end | |||
local header; -- initialize section header with name of selected category list | |||
if 'error' == select then | |||
header = 'error'; | |||
elseif 'maint' == select then | |||
header = 'maintenance'; | |||
else | |||
header = 'properties'; | |||
end | |||
header = table.concat ({ -- build the main header | |||
'Live ', -- always include this | |||
((sandbox and 'and sandbox ') or ''), -- if sandbox evaluated, mention that | |||
header, -- add the list name | |||
' categories (', -- finish the name and add | |||
#live_sbox_out, -- count of categories listed | |||
')' -- close | |||
}) | |||
local templatestyles = frame:extensionTag{ | |||
name = 'templatestyles', args = { src = "Div col/styles.css" } | |||
} | |||
header = table.concat ({ -- make a useable header | |||
_header_make ({header, hdr_lvl}), | |||
'\n' .. templatestyles .. '<div class="div-col">' -- opening <div> for columns | |||
}); | |||
table.sort (live_sbox_out, comp); -- sort case agnostic acsending | |||
table.insert (live_sbox_out, 1, header); -- insert the header at the top | |||
table.insert (out, table.concat (live_sbox_out, '\n*')); -- make a big string of unordered list markup | |||
table.insert (out, '</div>\n'); -- close the </div> and add new line so the next header works | |||
if 0 ~= #live_not_in_sbox_out then -- when there is something in the table | |||
header = table.concat ({ -- build header for subsection | |||
'In live but not in sandbox (', | |||
#live_not_in_sbox_out, | |||
')' | |||
}); | |||
header = table.concat ({ -- make a useable header | |||
_header_make ({header, hdr_lvl+1}), | |||
'\n' .. templatestyles .. '<div class="div-col">' | |||
}); | |||
table.sort (live_not_in_sbox_out, comp); | |||
table.insert (live_not_in_sbox_out, 1, header); | |||
table.insert (out, table.concat (live_not_in_sbox_out, '\n*')); | |||
table.insert (out, '</div>\n'); | |||
end | |||
if 0 ~= #sbox_not_in_live_out then -- when there is something in the table | |||
header = table.concat ({ -- build header for subsection | |||
'In sandbox but not in live (', | |||
#sbox_not_in_live_out, | |||
')' | |||
}); | |||
header = table.concat ({ -- make a useable header | |||
_header_make ({header, hdr_lvl+1}), | |||
'\n' .. templatestyles .. '<div class="div-col">' | |||
}); | |||
table.sort (sbox_not_in_live_out, comp); | |||
table.insert (sbox_not_in_live_out, 1, header); | |||
table.insert (out, table.concat (sbox_not_in_live_out, '\n*')); | |||
table.insert (out, '</div>\n'); | |||
end | |||
return table.concat (out); -- concat into a huge string and done | |||
end | |||
--[=[--------------------------< H E L P _ T E X T _ C A T S >-------------------------------------------------- | |||
To create category links at the bottom of each error help text section and on the individual error category pages; | |||
fetches category names from ~/Configuration; replaces this: | |||
{{#ifeq:{{FULLPAGENAME}}|Category:CS1 errors: bioRxiv|Category:CS1 errors: bioRxiv|[[:Category:CS1 errors: bioRxiv]]}} | |||
with this: | |||
{{#invoke:Cs1 documentation support|help_text_cats|err_bad_biorxiv}} | |||
add |pages=yes to append the number of pages in the category | |||
]=] | |||
local function help_text_cats (frame) | |||
local args = getArgs (frame); | |||
local error_conditions = mw.loadData ('Module:Citation/CS1/Configuration').error_conditions; | |||
local out = {}; -- output goes here | |||
if args[1] and error_conditions[args[1]] then -- must have error_condition key and it must exist | |||
table.insert (out, '{{#ifeq:{{FULLPAGENAME}}|Category:'); -- the beginning with category prefix | |||
table.insert (out, error_conditions[args[1]].category); -- fetch the error category name (the reference that {{FULLPAGENAME}} must equate to) | |||
table.insert (out, '|Category:'); -- this category plain text when this invoke is on the category page | |||
table.insert (out, error_conditions[args[1]].category); -- fetch the error category name | |||
table.insert (out, '|'); -- the necessary pipe | |||
table.insert (out, cat_link_make (error_conditions[args[1]].category)); -- one the help page or elsewhere so link to the category | |||
table.insert (out, '}}'); -- and close the #ifeq | |||
if 'yes' == args.pages then | |||
table.insert (out, ' ({{PAGESINCATEGORY:'); | |||
table.insert (out, error_conditions[args[1]].category); -- fetch the error category name | |||
table.insert (out, '}} pages)'); | |||
end | |||
else | |||
return '<span style="font-size:100%" class="error">unknown error_conditions key: ' .. (args[1] or 'key missing') .. '</span>'; | |||
end | |||
return frame:preprocess (table.concat (out)); -- make a big string, preprocess, and done | |||
end | |||
--[[--------------------------< H E L P _ T E X T _ E R R O R _ M E S S A G E >-------------------------------- | |||
to render help text example error messages | |||
{{#invoke:Cs1 documentation support|help_text_error_messages|err_bad_biorxiv}} | |||
assign a single underscore to any of the |$n= parameters to insert an empty string in the error message: | |||
{{#invoke:Cs1 documentation support|help_text_error_messages|err_bad_issn|$1=_}} -> Check |issn= value | |||
{{#invoke:Cs1 documentation support|help_text_error_messages|err_bad_issn|$1=e}} -> Check |eissn= value | |||
]] | |||
local function help_text_error_messages (frame) | |||
local args = getArgs (frame); | |||
local error_conditions = mw.loadData ('Module:Citation/CS1/Configuration').error_conditions; | |||
local span_o = '<span class="cs1-visible-error error citation-comment">'; | |||
local span_c = '</span>'; | |||
local message; | |||
local out = {}; -- output goes here | |||
if args[1] and error_conditions[args[1]] then -- must have error_condition key and it must exist | |||
message = error_conditions[args[1]].message; | |||
local i=1; | |||
local count; | |||
local rep; | |||
repeat | |||
rep = '$'..i | |||
args[rep] = args[rep] and args[rep]:gsub ('^%s*_%s*$', '') or nil; -- replace empty string marker with actual empty string | |||
message, count = message:gsub (rep, args[rep] or rep) | |||
i = i + 1; | |||
until (0 == count); | |||
table.insert (out, span_o); | |||
table.insert (out, message); | |||
table.insert (out, span_c); | |||
else | |||
return '<span style="font-size:100%" class="error">unknown error_conditions key: ' .. (args[1] or 'key missing') .. '</span>'; | |||
end | |||
local out_str = table.concat (out); | |||
out_str = frame:expandTemplate ({title='resize', args = {'120%', out_str} }) | |||
return table.concat ({out_str, frame:extensionTag ('templatestyles', '', {src='Module:Citation/CS1/styles.css'})}); | |||
end | end | ||
--[[-------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------ | --[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------ | ||
]] | ]] | ||
Line 654: | Line 936: | ||
canonical_param_lister = canonical_param_lister, | canonical_param_lister = canonical_param_lister, | ||
canonical_name_get = canonical_name_get, | canonical_name_get = canonical_name_get, | ||
cat_lister = cat_lister, | |||
header_make = header_make, | header_make = header_make, | ||
help_text_cats = help_text_cats, | |||
help_text_error_messages = help_text_error_messages, | |||
id_limits_get = id_limits_get, | id_limits_get = id_limits_get, | ||
is_book_cite_template = is_book_cite_template, | is_book_cite_template = is_book_cite_template, |