Module:Footnotes/anchor id list: Difference between revisions

-cite LSA support; template deleted;
 
Editor1 (talk | contribs)
m 1 revision imported
 
(4 intermediate revisions by 2 users not shown)
Line 92: Line 92:
['Citation-attribution'] = true,
['Citation-attribution'] = true,
}
}
local Article_content;
local global_article_content = nil
 
local global_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 = {};




Line 107: Line 108:


local function article_content_get ()
local function article_content_get ()
if not Article_content then
    if global_article_content then return global_article_content end
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
    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
for _, tag in ipairs (patterns_tags) do
Article_content = Article_content:gsub (tag, ''); -- remove certain html-like tags and their content
    article_content = article_content:gsub (tag, ''); -- remove certain html-like tags and their content
end
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


if whitelist.wrapper_templates[template_name][1] then -- is this wrapper a simple-default wrapper?
    local wrap_data = whitelist.wrapper_templates[template_name]
name_default = whitelist.wrapper_templates[template_name][1]; -- get the default names
 
date_default = whitelist.wrapper_templates[template_name][2]; -- get the default date
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';
if not whitelist.wrapper_templates[template_name][vol] then -- make sure this volume exists
        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 = whitelist.wrapper_templates[template_name][vol][1]; -- get the default names
name_default = wrap_data[vol][1];                                 -- get the default names
date_default = whitelist.wrapper_templates[template_name][vol][2]; -- get the default date
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 (); -- attempt to get this article's content
    local anchor_id_list = {}
    local template_list = {}
    local article_whitelist = {}
local article_content = article_content_get (); -- attempt to get this article's content


if '' == Article_content then -- when there is no article content
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 = Article_content:find (find_pattern); -- find the first template; do not find template variables: {{{template var|}}}  
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 = Article_content:match ('%b{}', tstart); -- get the whole 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
    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 = Article_content:find (find_pattern, tend); -- search for another template; begin at end of last search
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');


return anchor_id_list;
 
    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_make(), -- table of anchor ids available in this article
anchor_id_list = global_anchor_id_list or {},                         -- table of anchor ids available in this article
article_whitelist = article_whitelist, -- table of anchor ids with false-positive error message to be suppressed
article_whitelist = global_article_whitelist or {}, -- table of anchor ids with false-positive error message to be suppressed
template_list = template_list, -- table of templates used in this article
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
}
}