Module:Footnotes/anchor id list: Difference between revisions

no edit summary
m (1 revision imported: Citation templates)
No edit summary
Line 325: Line 325:


local function template_strip (template)
local function template_strip (template)
template = template:gsub ('^{{', ''):gsub ('}}$', '', 1); -- remove outer {{ and }} (cs1|2 template delimiters)
template = template:gsub ('^{{%s*', ''):gsub ('%s*}}$', '', 1); -- remove outer {{ and }} (cs1|2 template delimiters with trailing/leading whitespace)
template = template:gsub ('%b{}', ''); -- remove any templates from the cs1|2 template
template = template:gsub ('%b{}', ''); -- remove any templates from the cs1|2 template
return template;
return template;
Line 396: Line 396:
]]
]]


local function template_params_get (template, params)
local function template_params_get (template, params_t)
template = wikilink_strip (template); -- because piped wikilinks confuse code that builds params{} and because wikilinks not allowed in an anchor id
template = wikilink_strip (template); -- because piped wikilinks confuse code that builds params_t{} and because wikilinks not allowed in an anchor id
-- strip templates after getting |ref= value because |ref={{sfnref}} and |ref={{harvid}} are allowed
-- strip templates after getting |ref= value because |ref={{sfnref}} and |ref={{harvid}} are allowed
template = template_strip (template); -- because template markup can confuse code that builds params{} and because templates in name parameters are not allowed
template = template_strip (template); -- because template markup can confuse code that builds params_t{} and because templates in name parameters are not allowed


template = template:gsub ('|%s*|', '|'); -- when pipe follows pipe with or without white space, remove extraneous pipe
local temp_t = mw.text.split (template, '%s*|%s*'); --split on the pipe
 
for _, param in ipairs (temp_t) do
for param, value in template:gmatch ('|%s*([^=]-)%s*=%s*([^|}]+)') do -- build a table of template parameters and their values
if param:find ('=', 1, true) then -- a named parameter?
if value then -- there must be a value
local k, v = param:match ('%s*([^=]-)%s*=%s*([^|}]+)');
if '' ~= value and not value:match ('^%s$') then -- skip when value is empty string or only whitespace
if v then -- there must be a value
params[param] = mw.text.trim (value); -- add trimmed value else
if '' ~= v and not v:match ('^%s$') then -- skip when value is empty string or only whitespace
params_t[k] = mw.text.trim (v); -- add trimmed value else
end
end
end
end
end