Module:Footnotes/anchor id list/sandbox: Difference between revisions

Content deleted Content added
sync from live;
 
No edit summary
Line 1:
require('Module:No globals');
local data = mw.loadData ('Module:Footnotes/anchor id list/data');
 
local anchor_id_list = {};
 
--local redirect_patterns_anchor = {
-- '{{%s*[Aa]nchor',
-- '{{%s*[Aa]nchor for redirect',
-- '{{%s*[Aa]nchors',
-- '{{%s*[Aa]nchro',
-- '{{%s*[Aa]ncor',
-- }
--local redirect_patterns_harvc = {
-- '{{%s*[Hh]arvc',
-- '{{%s*[Cc]itec',
-- }
local redirect_patterns_sfn_whitelist = {
'{{%s*[Ss]fn whitelist',
'{{%s*[Hh]arv whitelist',
}
--local redirect_patterns_vcite = {
-- '{{%s*[Vv]cite',
-- '{{%s*[Vv]ancite',
---- '{{%s*[Cc]it ', -- disabled 'cit journal & cit paper' redirect to vcite journal but 'cit book', 'cit new', 'cit web' are cs1 redirects
-- }
local redirects_citation = {
['citation'] = true,
Line 37 ⟶ 38:
['isotomos'] = true,
}
--local redirects_harvc = {
-- ['harvc'] = true,
-- ['citec'] = true,
-- }
local redirects_patent = {
['cite patent'] = true,
Line 121 ⟶ 122:
}
local Article_content;
local template_list = {};
 
 
--[[--------------------------< A R T I C L E _ C O N T E N T _ G E T >----------------------------------------
Line 403 ⟶ 406:
 
local function template_name_get (template)
-- local template_name = template:match ('{{%s*([^/|]+)'); -- get template name; ignore subpages ~/new, ~/sandbox
local template_name = template:match ('{{%s*([^#][^#/|]+)'); -- get template name; ignore subpages ~/new, ~/sandbox; parser functions, magic words don't count as templates
 
if not template_name then
return nil; -- could not get template name from (possibly corrupt) template; extraneous opening { mid template can cause this;
Line 565 ⟶ 570:
return nil; -- anchor id expicitly suppressed
-- elseif '' ~= ref then -- ref is never empty string here -- |ref=<text>
else
anchor_id = ref; -- |ref=<text> may match an anchor id override value in {{harv}} template |ref= parameter
Line 633 ⟶ 637:
end
end
end
 
 
--[[--------------------------< T E M P L A T E _ L I S T _ A D D >--------------------------------------------
 
makes a list of templates use in the article.
 
]]
 
local Lang_obj = mw.language.getContentLanguage();
 
local function template_list_add (template)
local template = template:match ('{{%s*(.-)[|}]'); -- keep the case of the template - this is different from template_name_get()
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)
list_add (template, template_list); -- add to list with (unused) tally
end
 
--mw.logObject (template_list, 'template_list')
-- return template_list;
end
 
Line 655 ⟶ 680:
local template; -- place to hold the template that we found
local template_name;
local anchor_id; -- place to hold an anchor id as it is extracted / decoded
local tstart, tend = Article_content:find ('{{%s*[Cc]it[ae]'); -- find the first cs1|2-like template
 
for template in Article_content:gmatch ('{%b{}}') do
while tstart do -- nil when cs1|2 template not found
template_name = template_name_get (template);
template = Article_content:match ('%b{}', tstart); -- get the whole template
template_list_add (template); -- add this template's name to the list
 
if data.known_templates_cs12 [template_name] then
if template then -- necessary?
anchor_id = anchor_id_make (template); -- extract an anchor id from this template
list_add (anchor_id, anchor_id_list, true)
end
tstart = tend; -- reset the search starting index
tstart, tend = Article_content:find ('{{%s*[Cc]it[ae]', tstart); -- search for another cs1|2 template
end
 
elseif data.known_templates_vcite [template_name] then
for _, pattern in ipairs (redirect_patterns_harvc) do
local ref = template:match ('|%s*ref%s*=%s*(%b{})'); -- first look for |ref={{sfnref}} or |ref={{harvid}} because we will strip templates from the vcite template
tstart, tend = Article_content:find (pattern); -- find the first harvc template
if ref then -- |ref={{template}}
anchor_id = sfnref_get (ref); -- returns content of {{sfnref}} or {{harvid}}; nil else
while tstart do -- nil when harvc template not found
template = Article_content:match ('%b{}', tstart); -- get the whole template
if template then -- necessary?
anchor_id = anchor_id_make_harvc (template); -- extract an anchor id from this template
list_add (anchor_id, anchor_id_list, true);
endelse
local params = {};
tstart = tend; -- reset the search starting index
tstart, tend = Article_content:find (pattern, tstart); -- search for another harvc template
end
end
 
template_params_get (template, params); -- build a table of template parameters and their values
for _, pattern in ipairs (redirect_patterns_vcite) do -- for each of the vcite family template base patterns
tstart, tend = Article_content:find (pattern); -- find the first vcite template
while tstart do -- nil when vcite template not found
template = Article_content:match ('%b{}', tstart); -- get the whole template
if template then -- necessary?
local ref = template:match ('|%s*ref%s*=%s*(%b{})'); -- first look for |ref={{sfnref}} or |ref={{harvid}} because we will strip templates from the vcite template
if ref then -- |ref={{template}}
anchor_id = sfnref_get (ref); -- returns content of {{sfnref}} or {{harvid}}; nil else
list_add (anchor_id, anchor_id_list, true);
else
local params = {};
local template_name = template_name_get (template); -- get lowercase trimmed template name; ignore subpages ~/new, ~/sandbox
 
template_params_getanchor_id (template,= params)['ref']; -- buildwhen aboth table ofset, templatevcite parametersuses andvalue theirfrom values|ref=
if not anchor_id and params['harvid'] then
 
anchor_id = 'CITEREF' .. params['refharvid']; -- whenin both setvcite, vcite|harvid= auto-adds 'CITEREF' prefix to usesthe value fromin |refharvid=
if not anchor_id and params['harvid'] then
anchor_id = 'CITEREF' .. params['harvid']; -- in vcite, |harvid= auto-adds 'CITEREF' prefix to the value in |harvid=
end
list_add (anchor_id, anchor_id_list, true);
end
list_add (anchor_id, anchor_id_list, true);
end
 
elseif data.known_templates_harvc [template_name] then
tstart = tend; -- reset the search starting index
tstart, tendanchor_id = Article_content:findanchor_id_make_harvc (pattern, tstarttemplate); -- searchextract an anchor forid anotherfrom vcitethis template
list_add (anchor_id, anchor_id_list, true);
end
end
elseif data.known_templates_wikicite [template_name] then
tstart, tend = Article_content:find ('{{%s*[Ww]ikicite'); -- find the first {{wikicite}} template
 
while tstart do -- nil when cs1|2 template not found
template = Article_content:match ('%b{}', tstart); -- get the whole template
 
if template then
local ref = template:match ('|%s*ref%s*=%s*(%b{})'); -- first look for |ref={{sfnref}} or |ref={{harvid}}
Line 729 ⟶ 723:
elseif template:match ('|%s*id%s*=%s*(%b{})') then
anchor_idref = template:match ('|%s*id%s*=%s*(%b{})');
anchor_id = 'Reference-' .. sfnref_get (ref);
 
elseif template:match ('|%s*id%s*=([^|}]+)') then
anchor_id = 'Reference-' .. template:match ('|%s*id%s*=([^|}]+)'); -- plain-text
Line 741 ⟶ 736:
list_add (anchor_id, anchor_id_list, true);
end
 
end
elseif data.known_templates_anchor [template_name] then
tstart = tend; -- reset the search starting index
anchor_id_make_anchor (template, anchor_id_list); -- extract anchor ids from this template if any
tstart, tend = Article_content:find ('{{%s*[Ww]ikicite', tstart); -- search for another cs1|2 template
end
for _, pattern in ipairs (redirect_patterns_anchor) do
tstart, tend = Article_content:find (pattern); -- find the first anchor template
while tstart do -- nil when anchor template not found
template = Article_content:match ('%b{}', tstart); -- get the whole template
if template then -- necessary?
anchor_id_make_anchor (template, anchor_id_list); -- extract anchor ids from this template if any
end
tstart = tend; -- reset the search starting index
tstart, tend = Article_content:find (pattern, tstart); -- search for another anchor template
end
end
 
mw.logObject (anchor_id_list, 'anchor_id_list')
mw.logObject (template_list, 'template_list')
 
mw.logObject (anchor_id_list, 'anchor_id_list')
return anchor_id_list;
end
Line 771 ⟶ 754:
 
]]
--[[
 
local Lang_obj = mw.language.getContentLanguage();
 
Line 794 ⟶ 777:
return template_list;
end
]]
 
 
--[[--------------------------< A R T I C L E _ L O C A L _ W H I T E L I S T _ M A K E >----------------------
Line 846 ⟶ 829:
anchor_id_list = anchor_id_list_make(), -- table of anchor ids available in this article
article_whitelist = article_local_whitelist_make(), -- table of anchor ids with false-positive error message to be suppressed
-- template_list = template_list_make(), -- table of templates used in this article
template_list = template_list, -- table of templates used in this article
}