Content deleted Content added
No edit summary |
No edit summary |
||
Line 1:
require('Module:No globals');
local data = mw.loadData ('Module:Footnotes/anchor id list/data');
local Lang_obj = mw.language.getContentLanguage(); -- used by template_list_add() to uppercase first letter of template name TODO: better way to do that?
local redirects_date = {
['date'] = true,
Line 38 ⟶ 10:
['isotomos'] = true,
}
local redirects_patent = { -- special case cs1-like templates because uses different parameters for name and date in anchor ID
['cite patent'] = true,
['citeref patent'] = true,
Line 118 ⟶ 86:
'<source.->.-</source>', -- deprecated alias of syntaxhighlight tag
}
local template_skip = { -- TODO: necessary to keep this? with known template tables templates not in those tables will be skipped
['citation-attribution'] = true,
}
local Article_content;
local anchor_id_list = {}; -- exported tables
local template_list = {};
local article_whitelist = {};
Line 406 ⟶ 377:
local function template_name_get (template)
local template_name = template:match ('{{%s*([^#][^#/|]+)'); -- get template name; ignore subpages ~/new, ~/sandbox; parser functions, magic words don't count as templates
Line 433 ⟶ 403:
for param, value in template:gmatch ('|%s*([^=]-)%s*=%s*([^|}]+)') do -- build a table of template parameters and their values
if value then -- there must be a value
if '' ~= value and not value:match ('^%s$') then -- skip when value is empty string or only whitespace
params[param] = mw.text.trim (value); -- add trimmed value else
Line 449 ⟶ 419:
local function anchor_id_make_harvc (template)
local date = date_get (template, alias_patterns_harvc_date);
local anchor_id;
local params = {}; -- table of harvc parameters
Line 465 ⟶ 435:
end
anchor_id = names_get (params, aliases_harvc_author);
if anchor_id then
return 'CITEREF' .. anchor_id .. date;
end
Line 474 ⟶ 444:
--[[--------------------------<
for cs1|2 template and cs1-like templates
inspect |ref= to decide what to do:
|ref= - empty or missing: get names and date from template parameters
|ref=harv - get names and date from template parameters
|ref={{SfnRef|name|name|name|name|year}} - assemble an anchor id from {{sfnref}} positional parameters
Line 483 ⟶ 455:
|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
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 params = {}; -- table of
template_name = template_name_get (template); -- get lowercase trimmed template name; ignore subpages ~/new, ~/sandbox
Line 506 ⟶ 471:
if redirects_patent[template_name] then
date = date_get (template, alias_patterns_patent_date);
else
date = date_get (template, alias_patterns_date);
Line 522 ⟶ 487:
end
end
end
Line 545 ⟶ 495:
return 'CITEREF' .. (params.last or '') .. (params.year or ''); -- cite LSA always creates an anchor id using only |last= and |year= (no aliases)
end
end
if 'harv' == ref or not ref then -- |ref=harv specified or |ref= missing or empty
if redirects_patent[template_name] then -- if this is a cite patent template
anchor_id = names_get (params, aliases_inventor); -- inventor names only
Line 580 ⟶ 528:
--[[--------------------------< L I S T _ A D D >--------------------------------------------------------------
adds an <item> to
]]
local function list_add (item, list, encode)
if item then -- if there was an
if encode then -- for anchor IDs ...
item = mw.uri.anchorEncode (item); -- encode to remove wikimarkup, convert spaces to underscores etc
end
if not list[item] then -- if not already saved
list[item] = 1; -- save it
else -- here when this
list[item] = list[item] + 1; -- to indicate that there are multiple
end
end
Line 607 ⟶ 555:
local function anchor_id_make_anchor (template, anchor_id_list)
template = template:gsub ('^{{[^|]+|', ''):gsub ('}}$', '', 1);
template = wikilink_strip (template); -- strip any wikilink markup (there shouldn't be any but just in case)
Line 614 ⟶ 562:
local anchor_id;
for param in template:gmatch ('%b{}') do -- loop through the template; remove and save templates (presumed to be sfnref or harvid)
table.insert (params, param); -- save it
template = template:gsub ('%b{}', '', 1); -- remove it from source template
Line 621 ⟶ 569:
for _, t in ipairs (params) do -- spin through the templates in params
anchor_id = sfnref_get (t); -- attempt to decode {{sfnref}} and {{harvid}}
if anchor_id then -- nil when not {{sfnref}}
list_add (anchor_id, anchor_id_list, true); -- add anchor ID to the list
end
Line 645 ⟶ 593:
]]
local function template_list_add (template)
Line 652 ⟶ 598:
if template and not template:match ('^#') then -- found a template or magic word; ignore magic words
template=mw.text.trim (template); -- trim whitespace
template = Lang_obj:ucfirst (template); -- first character in template name must be uppercase (same as canonical template name) TODO: better way to do this?
list_add (template, template_list); -- add to list with (unused) tally
end
end
--[[--------------------------<
makes a list of anchor ids from cs1|2, cs1|2-like, vcite xxx,
Because cs1|2 wrapper templates can, and often do, hide
these parameters are not available in the article's wikisource so {{harv}}, {{sfn}}, and {{harvc}} templates that
link correctly to those wrapper templates will incorrectly show error messages. Use |ignore-err=yes in the {{harv}},
{{sfn}}, and {{harvc}} templates to supress the error message.
creates a list of templates used in the article for use with the whitelist
creates a list of article-local whitelisted anchor IDs from {{sfn whitelist}}
]]
Line 689 ⟶ 636:
if data.known_templates_cs12 [template_name] then
anchor_id =
list_add (anchor_id, anchor_id_list, true)
Line 739 ⟶ 686:
elseif data.known_templates_anchor [template_name] then
anchor_id_make_anchor (template, anchor_id_list); -- extract anchor ids from this template if any
elseif data.known_templates_sfn_whitelist [template_name] then
template = template:gsub ('^{{[^|]+|', ''):gsub ('}}$', '', 1); -- remove outer {{ and }} and template name
template = mw.text.trim (template, '%s|'); -- trim leading trailing white space and pipes
template = mw.text.split (template, '%s*|%s*'); -- make a table of the template's parameters
for _, anchor_id in ipairs (template) do -- spin through this template's parameter
if '' ~= anchor_id and not article_whitelist[anchor_id] then
article_whitelist[anchor_id] = 1; -- add to the whitelist
end
end
end
end
mw.logObject (
mw.logObject (template_list, 'template_list');
mw.logObject (article_whitelist, 'article_whitelist');
return anchor_id_list;
end
Line 828 ⟶ 713:
return {
anchor_id_list = anchor_id_list_make(), -- table of anchor ids available in this article
article_whitelist =
template_list = template_list, -- table of templates used in this article
}
|