Content deleted Content added
sync |
|||
(32 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
local redirects_date = {
Line 92:
['Citation-attribution'] = true,
}
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 ⟶ 108:
local function article_content_get ()
if global_article_content then return global_article_content end
end
global_article_content = article_content
return article_content
end
Line 372 ⟶ 374:
end
--[[--------------------------< T E M P L A T E _ N A M E _ F R 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 385 ⟶ 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 500 ⟶ 523:
template_params_get (template, params); -- build a table of template parameters and their values
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
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
end
name_default =
date_default =
end
Line 677 ⟶ 709:
list_add (anchor_id, anchor_id_list, true); -- add anchor ID to the list
end
end
end
Line 713 ⟶ 729:
local function anchor_id_list_make ()
local anchor_id_list = {}
local template_list = {}
local article_whitelist = {}
local article_content = article_content_get (); -- attempt to get this article's content
if
return ''; -- no point in continuing
end
Line 724 ⟶ 743:
local find_pattern = '%f[{]{{[^{]';
local tstart, tend =
while tstart do
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
if data.known_templates_cs12 [template_name] then
Line 798 ⟶ 816:
end
end
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
Line 808 ⟶ 827:
end
tstart, tend =
end
Line 815 ⟶ 834:
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
Line 822 ⟶ 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 =
article_whitelist =
template_list =
citeref_patterns = citeref_patterns_make() or {}, -- table of Lua patterns to search for citeref from wrappers
}
|