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

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 whitelist = mw.loadData ('Module:Footnotes/whitelist/sandbox');
local Lang_obj = mw.language.getContentLanguage(); -- used by template_list_add() to uppercase first letter of template name TODO: better way to do that?
 
Line 383 ⟶ 384:
end;
template_name = template_name:gsub ('%s*$', ''); -- trim whitespace
-- template_name = template_name:lower(); -- and lowercase only
return template_name;
end
Line 441 ⟶ 442:
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 params = {}; -- table of template parameters
template_name = template_name_get (template); -- get trimmed template name as written; 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
 
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=
whitelist.wrapper_templates[template_name][1]; -- default names from whitelist
 
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 and not redirects_patent[template_name] then -- |ref=none; not supported by cite patent
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 630 ⟶ 703:
local anchor_id; -- place to hold an anchor id as it is extracted / decoded
 
-- local tstart, tend = Article_content:find ('%f[^{]{{[^{]'); -- find the first template; do not find template variables: {{{template var|}}}
local tstart, tend = Article_content:find ('{{'); -- find the first template; do not find template variables: {{{template var|}}}
 
while tstart do
template = Article_content:match ('%b{}', tstart); -- get the whole template
template_nameif =not template_name_get (template); then
break; -- template is nil for some reason (last template missing closing }} for example) so declare ourselves done
end
 
template_name = template_name_get (template);
template_list_add (template); -- add this template's name to the list
 
Line 700 ⟶ 777:
end
end
-- whatever it is that gets written for dynamic whitelist goes here
-- elseif template_name and whitelist.wrapper_templates[template_name] then
-- 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; -- reset the search starting index
-- tstart, tend = Article_content:find ('%f[^{]{{[^{]', tstart); -- search for another template
tstart, tend = Article_content:find ('{{', tstart); -- search for another template
end