Module:Footnotes/anchor id list: Difference between revisions
-cite LSA support; template deleted; |
m 1 revision imported |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 92: | Line 92: | ||
['Citation-attribution'] = true, | ['Citation-attribution'] = true, | ||
} | } | ||
local | local global_article_content = nil | ||
local global_anchor_id_list = nil -- exported tables | |||
local global_template_list = nil | |||
local global_article_whitelist = nil | |||
Line 107: | Line 108: | ||
local function article_content_get () | local function article_content_get () | ||
if global_article_content then return global_article_content end | |||
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:gsub (tag, ''); -- remove certain html-like tags and their content | |||
end | end | ||
global_article_content = article_content | |||
return article_content | |||
end | end | ||
Line 500: | Line 502: | ||
template_params_get (template, params); -- build a table of template parameters and their values | template_params_get (template, params); -- build a table of template parameters and their values | ||
local wrap_data = whitelist.wrapper_templates[template_name] | |||
name_default = | |||
date_default = | 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 | else | ||
vol = params['volume'] or 'default'; | 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 | vol = 'default'; -- doesn't exist, use default volume | ||
end | end | ||
name_default = | name_default = wrap_data[vol][1]; -- get the default names | ||
date_default = | date_default = wrap_data[vol][2]; -- get the default date | ||
end | end | ||
Line 687: | Line 698: | ||
]] | ]] | ||
local function template_list_add (template) | local function template_list_add (template, template_list) | ||
local template = template:match ('{{%s*(.-)[|}]'); -- keep the case of the template - this is different from template_name_get() | 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 | if template and not template:match ('^#') then -- found a template or magic word; ignore magic words | ||
Line 713: | Line 724: | ||
local function anchor_id_list_make () | local function anchor_id_list_make () | ||
article_content_get (); | local anchor_id_list = {} | ||
local template_list = {} | |||
local article_whitelist = {} | |||
local article_content = article_content_get (); -- attempt to get this article's content | |||
if '' | if article_content == '' then -- when there is no article content | ||
return ''; -- no point in continuing | return ''; -- no point in continuing | ||
end | end | ||
Line 724: | Line 738: | ||
local find_pattern = '%f[{]{{[^{]'; | local find_pattern = '%f[{]{{[^{]'; | ||
local tstart, tend = | local tstart, tend = article_content:find (find_pattern); -- find the first template; do not find template variables: {{{template var|}}} | ||
while tstart do | while tstart do | ||
template = | template = article_content:match ('%b{}', tstart); -- get the whole template | ||
if not template then | if not template then | ||
break; -- template is nil for some reason (last template missing closing }} for example) so declare ourselves done | break; -- template is nil for some reason (last template missing closing }} for example) so declare ourselves done | ||
Line 733: | Line 747: | ||
template_name = template_name_get (template); -- get first char uppercase trimmed template name; ignore subpages ~/new, ~/sandbox | template_name = template_name_get (template); -- get first char uppercase trimmed template name; ignore subpages ~/new, ~/sandbox | ||
template_list_add (template); -- add this template's name to the list | template_list_add (template, template_list); -- add this template's name to the list | ||
if data.known_templates_cs12 [template_name] then | if data.known_templates_cs12 [template_name] then | ||
Line 798: | Line 812: | ||
end | end | ||
end | end | ||
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 | anchor_id = anchor_id_make_wrapper (template); -- extract an anchor id from this template if possible | ||
list_add (anchor_id, anchor_id_list, true); | 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 | 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 | anchor_id = anchor_id_make_cs12 (template); -- extract an anchor id from this template if possible | ||
Line 808: | Line 823: | ||
end | end | ||
tstart, tend = | tstart, tend = article_content:find (find_pattern, tend); -- search for another template; begin at end of last search | ||
end | end | ||
Line 815: | Line 830: | ||
mw.logObject (article_whitelist, 'article_whitelist'); | 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 | end | ||
Line 822: | Line 866: | ||
]] | ]] | ||
-- 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 { | return { | ||
anchor_id_list = | anchor_id_list = global_anchor_id_list or {}, -- table of anchor ids available in this article | ||
article_whitelist = | article_whitelist = global_article_whitelist or {}, -- table of anchor ids with false-positive error message to be suppressed | ||
template_list = | template_list = global_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 | |||
} | } |