Module:Footnotes/anchor id list: Difference between revisions

m 1 revision imported
fix errors when using Visual Editor
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 = {};
local template_list_done = false




Line 108: 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 697: 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 723: 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 734: 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 743: 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 819: 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
    template_list_done = true


mw.logObject (anchor_id_list, 'anchor_id_list');
mw.logObject (anchor_id_list, 'anchor_id_list');
Line 827: 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
end


Line 839: Line 845:


local function citeref_patterns_make()
local function citeref_patterns_make()
     if not template_list_done then error("Template list not yet created") end
     if not global_template_list then return end
     local citeref_patterns = {}
     local citeref_patterns = {}
     local template_patterns = whitelist.wrapper_template_patterns
     local template_patterns = whitelist.wrapper_template_patterns
     for _, p in ipairs(template_patterns) do
     for _, p in ipairs(template_patterns) do
         for _, t in ipairs(p[1]) do                                          -- loop through list of template wrappers
         for _, t in ipairs(p[1]) do                                          -- loop through list of template wrappers
             if template_list[t] then                                         -- if wrapper is found in article, record corresponding patterns
             if global_template_list[t] then                                   -- if wrapper is found in article, record corresponding patterns
                 for _, pat in ipairs(p[2]) do
                 for _, pat in ipairs(p[2]) do
                     table.insert(citeref_patterns, pat)
                     table.insert(citeref_patterns, pat)
Line 860: 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()                                 -- table of Lua patterns to search for citeref from wrappers
     citeref_patterns = citeref_patterns_make() or {}                                -- table of Lua patterns to search for citeref from wrappers
}
}