Content deleted Content added
No edit summary |
|||
(48 intermediate revisions by 5 users not shown) | |||
Line 1:
require('
local data = mw.loadData ('Module:Footnotes/anchor id list/data');
local whitelist = mw.loadData ('Module:Footnotes/whitelist/sandbox');
local Lang_obj = mw.language.getContentLanguage(); -- used by template_name_get() to uppercase first letter of template name TODO: better way to do that?
local redirects_date = {
Line 11 ⟶ 12:
}
local redirects_patent = { -- special case cs1-like templates because uses different parameters for name and date in anchor ID
['
['
['
}
local redirects_sfnref = {
Line 59 ⟶ 60:
'|%s*date%s*=%s*',
'|%s*publication%-?date%s*=%s*',
'|%s*air%-?date%s*=%s*',
}
local alias_patterns_harvc_date = { -- normal lua patterns for harvc template
Line 71 ⟶ 73:
}
local patterns_date = { -- normal lua patterns
'(%d%d%d%d)%D+(%d%d%d%d%l?)$', -- any range with four-digit years; with or without dab; not two captures
'^(%d%d%d%d–%d%d%l?)$', -- YYYY–YY two-digit year range; with or without dab
'^(c%. %d%d%d%d?%l?)$', -- three- or four-digit circa year; with or without dab
Line 86 ⟶ 89:
'<source.->.-</source>', -- deprecated alias of syntaxhighlight tag
}
local template_skip = { --
['
}
local global_article_content = nil
local global_anchor_id_list = nil -- exported tables
local global_template_list = nil
local global_article_whitelist = nil
Line 104 ⟶ 108:
local function article_content_get ()
if global_article_content then return global_article_content end
end
global_article_content = article_content
return article_content
end
Line 155 ⟶ 160:
local function date_get (template, aliases)
local rvalue;
Line 184 ⟶ 188:
if rvalue then
for _, pattern in ipairs (patterns_date) do -- spin through the recognized date formats
-- date = rvalue:match (pattern); -- attempt to extract year portion according to the pattern
local date, date2 = rvalue:match (pattern); -- attempt to extract year portion according to the pattern; <date2> gets second year in any range
if date then
if date2 then -- when a second year
date = table.concat ({date, '–', date2}); -- build a date range
end
return date; -- matched so return;
end
Line 323 ⟶ 331:
local function template_strip (template)
template = template:gsub ('^{{%s*', ''):gsub ('%s*}}$', '', 1);
template = template:gsub ('%b{}', ''); -- remove any templates from the cs1|2 template
return template;
Line 366 ⟶ 374:
end
--[[--------------------------< T E M P L A T E _ N A M E _ F R O M _ M O D U L E >----------------------------
if passed a module invocation, return the name of the template represented. Otherwise return the input.
{{#invoke:cite|foo|...}} or {{#invoke:cite bar||...}} will return "cite foo" and "cite bar", respectively.
]]
local function template_name_from_module (template, template_name)
if template_name and template_name:match ('^#invoke%s*:') then -- handle directly-invoked citation modules
template_name = template_name:match ('^#invoke%s*:%s*(.+)'); -- get module name
local func_name = template:match ('^{{[^|}]+%|%s*([^/|}]*)'); -- get function name
if template_name and func_name then -- valid module invocation
return template_name:gsub ('%s+$', '') .. ' ' .. func_name; -- ensure exactly one trailing whitespace between module and function name
end;
return nil -- could not get module and function name
end;
return template_name
end
--[[--------------------------< T E M P L A T E _ N A M E _ G E T >--------------------------------------------
Line 379 ⟶ 406:
local template_name = template:match ('^{{%s*([^/|}]+)'); -- get template name; ignore subpages ~/new, ~/sandbox; parser functions
template_name = template_name_from_module (template, template_name); -- if passed a module invocation, return the name of the template represented
if not template_name or template_name:match ('^#') then -- parser functions, magic words don't count as templates
return nil; -- could not get template name from (possibly corrupt) template; extraneous opening { mid template can cause this;
end;
template_name = template_name:gsub ('%s*$', ''); -- trim trailing whitespace; leading whitespace already removed
return Lang_obj:ucfirst (template_name); -- first character in template name must be uppercase (same as canonical template name) TODO: better way to do this?
end
Line 395 ⟶ 423:
]]
local function template_params_get (template,
template = wikilink_strip (template); -- because piped wikilinks confuse code that builds
-- strip templates after getting |ref= value because |ref={{sfnref}} and |ref={{harvid}} are allowed
template = template_strip (template); -- because template markup can confuse code that builds
for _, param in ipairs (temp_t) do
if param:find ('=', 1, true) then -- a named parameter?
local k, v = param:match ('%s*([^=]-)%s*=%s*([^|}]+)');
if v then -- there must be a value
if '' ~= end
end
end
Line 441 ⟶ 471:
end
return nil; -- no names; no anchor_id
end
--[[--------------------------< A N C H O R _ I D _ M A K E _ W R A P P E R >----------------------------------
for wrapper templates
inspect externally visible |ref= to decide what to do:
|ref= - empty or missing: get names and date from whitelist defaults; override defaults from externally visible template parameters
|ref=harv - same as empty or missing
|ref={{SfnRef|name|name|name|name|year}} - assemble an anchor id from {{sfnref}} positional parameters
|ref={{Harvid|name|name|name|name|year}} - assemble an anchor id from {{harvid}} positional parameters
|ref=none - skip; do nothing because an anchor id intentionally suppressed; TODO: keep with a type code of '0'?
|ref=<text> - save param value because may match an anchor id override value in {{harv}} template |ref= parameter or {{harvc}} |id= parameter
]]
local function anchor_id_make_wrapper (template)
local ref; -- content of |ref=
local template_name; -- name of the template
local anchor_id; -- the assembled anchor id from this template
local date;
local name_default;
local date_default;
local vol;
local params = {}; -- table of template parameters
template_name = template_name_get (template); -- get first char uppercase trimmed template name; ignore subpages ~/new, ~/sandbox
if not template_name or template_skip[template_name] then
return nil; -- could not extract template name from (possibly corrupted) template (extraneous opening { in the template will cause this)
end
date = date_get (template, alias_patterns_date); -- get date; done here because might be in {{date}}
-- if '' == date then
-- date = whitelist.wrapper_templates[template_name][2] or ''; -- no externally visible date so get default date
-- end
ref = template:match ('|%s*ref%s*=%s*(%b{})'); -- first look for |ref={{sfnref}} or |ref={{harvid}} because we will strip templates from the wrapper template
if not ref then
if template:match ('|%s*ref%s*=([^|}]+)') then -- |ref={{template}} not found; if there is a |ref= param with an assigned value
ref = template:match ('|%s*ref%s*=([^|}]+)'); -- get the value; whitespace is a 'value'
if ref then -- nil when |ref=|... or when |ref=}} (no spaces between assignment operator and pipe or closing brace)
ref = mw.text.trim (ref); -- something, could be just whitespace, so trim leading / trailing whitespace
if '' == ref then -- trimming a string of whitespace makes an empty string
ref = nil; -- make empty ref same as missing ref
end
end
end
end
template_params_get (template, params); -- build a table of template parameters and their values
local wrap_data = whitelist.wrapper_templates[template_name]
if wrap_data[1] then -- is this wrapper a simple-default wrapper?
name_default = wrap_data[1]; -- get the default names
date_default = wrap_data[2]; -- get the default date
else
vol = params['volume'] or 'default';
local fascicle = params['fascicle'] -- some templates use "fascicle" to mean "subvolume"
if fascicle then
local subvol = vol..'/'..fascicle -- if fascicle is used, subvolume = "vol/fascicle"
if wrap_data[subvol] then -- if subvolume exists, use it, otherwise fall back to volume
vol = subvol
end
end
if not wrap_data[vol] then -- make sure this volume exists
vol = 'default'; -- doesn't exist, use default volume
end
name_default = wrap_data[vol][1]; -- get the default names
date_default = wrap_data[vol][2]; -- get the default date
end
if 'harv' == ref or not ref then -- |ref=harv specified or |ref= missing or empty
anchor_id = names_get (params, aliases_contributor) or -- get contributor, author, or editor names
names_get (params, aliases_author) or
vnames_get (params, 'vauthors') or -- |vauthors=
names_get (params, aliases_editor) or
vnames_get (params, 'veditors') or -- |veditors=
name_default; -- default names from whitelist
-- whitelist.wrapper_templates[template_name][1]; -- default names from whitelist
if '' == date then -- if date not provided in the template
date = date_default; -- use the default date from whitelist
end
if anchor_id then -- if names were gotten
anchor_id = 'CITEREF' .. anchor_id .. date;
end
elseif ref:match ('%b{}') then -- ref holds a template
anchor_id = sfnref_get (ref); -- returns content of {{sfnref}} or {{harvid}}; nil else
elseif 'none' == ref then -- |ref=none
return nil; -- anchor id expicitly suppressed
else
anchor_id = ref; -- |ref=<text> may match an anchor id override value in {{harv}} template |ref= parameter
end
return anchor_id; -- anchor_id text; nil else
end
Line 465 ⟶ 596:
local params = {}; -- table of template parameters
template_name = template_name_get (template); -- get
if not template_name or template_skip[template_name] then
return nil; -- could not extract template name from (possibly corrupted) template (extraneous opening { in the template will cause this)
Line 490 ⟶ 621:
template_params_get (template, params); -- build a table of template parameters and their values
if 'harv' == ref or not ref then -- |ref=harv specified or |ref= missing or empty
Line 584 ⟶ 709:
list_add (anchor_id, anchor_id_list, true); -- add anchor ID to the list
end
end
end
Line 620 ⟶ 729:
local function anchor_id_list_make ()
local anchor_id_list = {}
local template_list = {}
local article_whitelist = {}
local article_content = article_content_get (); -- attempt to get this article's content
if
return ''; -- no point in continuing
end
Line 630 ⟶ 742:
local anchor_id; -- place to hold an anchor id as it is extracted / decoded
local find_pattern = '%f[{]{{[^{]';
local tstart, tend = article_content:find (find_pattern); -- find the first template; do not find template variables: {{{template var|}}}
while tstart do
template =
break; -- template is nil for some reason (last template missing closing }} for example) so declare ourselves done
end
template_name = template_name_get (template); -- get first char uppercase trimmed template name; ignore subpages ~/new, ~/sandbox
list_add (template_name, template_list); -- add this template's name to the list
if data.known_templates_cs12 [template_name] then
Line 697 ⟶ 812:
for _, anchor_id in ipairs (template) do -- spin through this template's parameter
if '' ~= anchor_id and not article_whitelist[anchor_id] then
anchor_id = mw.uri.anchorEncode (anchor_id)
article_whitelist[anchor_id] = 1; -- add to the whitelist
end
end
anchor_id = anchor_id_make_wrapper (template); -- extract an anchor id from this template if possible
list_add (anchor_id, anchor_id_list, true);
elseif template_name and template_name:match ('^Cit[ea]') then -- not known, not known wrapper; last gasp, try as cs1-like
anchor_id = anchor_id_make_cs12 (template); -- extract an anchor id from this template if possible
list_add (anchor_id, anchor_id_list, true);
end
tstart, tend =
end
Line 713 ⟶ 834:
mw.logObject (article_whitelist, 'article_whitelist');
global_anchor_id_list = anchor_id_list
global_template_list = template_list
global_article_whitelist = article_whitelist
end
--[[--------------------------< C I T E R E F _ P A T T E R N S _ M A K E >--------------------------------------------
Scans template_list to look for wrapper templates that generate citerefs that require Lua patterns.
This scan is only done once per page load, to save time
]]
local function citeref_patterns_make()
if not global_template_list then return end
local citeref_patterns = {}
local template_patterns = whitelist.wrapper_template_patterns
for _, p in ipairs(template_patterns) do
for _, t in ipairs(p[1]) do -- loop through list of template wrappers
if global_template_list[t] then -- if wrapper is found in article, record corresponding patterns
for _, pat in ipairs(p[2]) do
table.insert(citeref_patterns, pat)
end
break
end
end
end
mw.logObject(citeref_patterns,'citeref_patterns')
return citeref_patterns
end
Line 720 ⟶ 870:
]]
-- First create global_anchor_id_list, global_template_list, global_article_whitelist
anchor_id_list_make()
-- Then stuff them (and derived tables) into return table
return {
anchor_id_list =
article_whitelist =
template_list =
citeref_patterns = citeref_patterns_make() or {}, -- table of Lua patterns to search for citeref from wrappers
}
|