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

Content deleted Content added
fix fascicle logic
Restored revision 1291947075 by Hike395 (talk): Nope: wasn't my edits
 
(23 intermediate revisions by 2 users not shown)
Line 2:
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_addtemplate_name_get() to uppercase first letter of template name TODO: better way to do that?
 
local redirects_date = {
Line 92:
['Citation-attribution'] = true,
}
local global_article_content = nil
local Article_content;
 
local anchor_id_listglobal_anchor_id_list = {}; nil -- exported tables
local global_template_list = nil
local global_article_whitelist = nil
 
local anchor_id_list = {}; -- exported tables
local template_list = {};
local article_whitelist = {};
local template_list_done = false
 
 
Line 108:
 
local function article_content_get ()
if global_article_content then return global_article_content end
if not Article_content then
Article_content local article_content = mw.title.getCurrentTitle():getContent() or ''; -- get the content of the article or ''; new pages edited w/ve do not have 'content' until saved; ve does not preview; phab:T221625
for _, tag in ipairs (patterns_tags) do
Article_content article_content = Article_contentarticle_content:gsub (tag, ''); -- remove certain html-like tags and their content
end
end
global_article_content = article_content
return article_content
end
 
Line 373 ⟶ 374:
end
 
--[[--------------------------< T E M P L A T E _ LN IA SM TE _ AF DR 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 386 ⟶ 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;
Line 457 ⟶ 479:
 
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
 
otherwise assume that wrapper template generates correct target for default ref
]]
 
Line 499 ⟶ 523:
template_params_get (template, params); -- build a table of template parameters and their values
 
if local wrap_data = whitelist.wrapper_templates[template_name][1] then -- is this wrapper a simple-default wrapper?
 
name_default = whitelist.wrapper_templates[template_name][1]; -- get the default names
if wrap_data[1] then -- is this wrapper a simple-default wrapper?
date_default = whitelist.wrapper_templates[template_name][2]; -- get the default date
name_default = wrap_data[1]; -- get the default names
date_default = wrap_data[2]; -- get the default date
else
vol = params['volume'] or 'default';
Line 507 ⟶ 533:
if fascicle then
local subvol = vol..'/'..fascicle -- if fascicle is used, subvolume = "vol/fascicle"
if whitelist.wrapper_templates[template_name]wrap_data[subvol] then -- if subvolume exists, use it, otherwise fall back to volume
vol = subvol
end
end
if not whitelist.wrapper_templates[template_name]wrap_data[vol] then -- make sure this volume exists
vol = 'default'; -- doesn't exist, use default volume
end
name_default = whitelist.wrapper_templates[template_name]wrap_data[vol][1]; -- get the default names
date_default = whitelist.wrapper_templates[template_name]wrap_data[vol][2]; -- get the default date
end
 
Line 547 ⟶ 573:
return anchor_id; -- anchor_id text; nil else
end
 
 
 
 
 
Line 686 ⟶ 709:
list_add (anchor_id, anchor_id_list, true); -- add anchor ID to the list
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 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) TODO: better way to do this?
list_add (template, template_list); -- add to list with (unused) tally
end
end
Line 722 ⟶ 729:
 
local function anchor_id_list_make ()
return local anchor_id_list; = {}
article_content_get (); -- attempt to get this article's content
local template_list = {};
local article_whitelist = {};
local article_content = article_content_get (); -- attempt to get this article's content
 
if ''article_content == Article_content'' then -- when there is no article content
return ''; -- no point in continuing
end
Line 733 ⟶ 743:
 
local find_pattern = '%f[{]{{[^{]';
local tstart, tend = Article_contentarticle_content:find (find_pattern); -- find the first template; do not find template variables: {{{template var|}}}
 
while tstart do
template = Article_contentarticle_content:match ('%b{}', tstart); -- get the whole template
if not 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); -- get first char uppercase trimmed template name; ignore subpages ~/new, ~/sandbox
template_list_addlist_add (templatetemplate_name, template_list); -- add this template's name to the list
 
if data.known_templates_cs12 [template_name] then
Line 818 ⟶ 827:
end
 
tstart, tend = Article_contentarticle_content:find (find_pattern, tend); -- search for another template; begin at end of last search
end
template_list_done = true
 
mw.logObject (anchor_id_list, 'anchor_id_list');
Line 826 ⟶ 834:
mw.logObject (article_whitelist, 'article_whitelist');
 
 
return anchor_id_list;
global_anchor_id_list = anchor_id_list
global_template_list = template_list
global_article_whitelist = article_whitelist
end
 
Line 838 ⟶ 849:
 
local function citeref_patterns_make()
if not template_list_doneglobal_template_list then error("Template list not yet created")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 template_listglobal_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)
Line 859 ⟶ 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 = anchor_id_list_make()global_anchor_id_list or {}, -- table of anchor ids available in this article
article_whitelist = article_whitelistglobal_article_whitelist or {}, -- table of anchor ids with false-positive error message to be suppressed
template_list = template_listglobal_template_list or {}, -- table of templates used in this article
citeref_patterns = citeref_patterns_make() or {}, -- table of Lua patterns to search for citeref from wrappers
}