Module:Footnotes/anchor id list and Module:Footnotes/anchor id list/sandbox: Difference between pages
(Difference between pages)
imported>Trappist the monk -cite LSA support; template deleted; |
imported>Trappist the monk Undid revision 1171066437 by Trappist the monk (talk) revert experiment; |
||
Line 1: | Line 1: | ||
require('strict'); | require('strict'); | ||
local data = mw.loadData ('Module:Footnotes/anchor id list/data'); | local data = mw.loadData ('Module:Footnotes/anchor id list/data'); | ||
local whitelist = mw.loadData ('Module:Footnotes/whitelist'); | local whitelist = mw.loadData ('Module:Footnotes/whitelist/sandbox'); | ||
local Lang_obj = mw.language.getContentLanguage(); -- used by template_list_add() to uppercase first letter of template name TODO: better way to do that? | local Lang_obj = mw.language.getContentLanguage(); -- used by template_list_add() to uppercase first letter of template name TODO: better way to do that? | ||
Line 60: | Line 60: | ||
'|%s*date%s*=%s*', | '|%s*date%s*=%s*', | ||
'|%s*publication%-?date%s*=%s*', | '|%s*publication%-?date%s*=%s*', | ||
} | } | ||
local alias_patterns_harvc_date = { -- normal lua patterns for harvc template | local alias_patterns_harvc_date = { -- normal lua patterns for harvc template | ||
Line 73: | Line 72: | ||
} | } | ||
local patterns_date = { -- normal lua patterns | local patterns_date = { -- normal lua patterns | ||
'(%d%d%d%d–%d%d%d%d%l?)$', -- YYYY–YYYY four-digit year range at end (Season YYYY–YYYY); with or without dab | |||
'^(%d%d%d%d–%d%d%l?)$', -- YYYY–YY two-digit year range; with or without dab | '^(%d%d%d%d–%d%d%l?)$', -- YYYY–YY two-digit year range; with or without dab | ||
'^(c%. %d%d%d%d?%l?)$', -- three- or four-digit circa year; with or without dab | '^(c%. %d%d%d%d?%l?)$', -- three- or four-digit circa year; with or without dab | ||
Line 158: | Line 156: | ||
local function date_get (template, aliases) | local function date_get (template, aliases) | ||
local date; | |||
local rvalue; | local rvalue; | ||
Line 186: | Line 185: | ||
if rvalue then | if rvalue then | ||
for _, pattern in ipairs (patterns_date) do -- spin through the recognized date formats | for _, pattern in ipairs (patterns_date) do -- spin through the recognized date formats | ||
date = rvalue:match (pattern); -- attempt to extract year portion according to the pattern | |||
if date then | if date then | ||
return date; -- matched so return; | return date; -- matched so return; | ||
end | end | ||
Line 329: | Line 324: | ||
local function template_strip (template) | local function template_strip (template) | ||
template = template:gsub ('^{{ | template = template:gsub ('^{{', ''):gsub ('}}$', '', 1); -- remove outer {{ and }} (cs1|2 template delimiters) | ||
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 400: | Line 395: | ||
]] | ]] | ||
local function template_params_get (template, | local function template_params_get (template, params) | ||
template = wikilink_strip (template); -- because piped wikilinks confuse code that builds | template = wikilink_strip (template); -- because piped wikilinks confuse code that builds params{} 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 | template = template_strip (template); -- because template markup can confuse code that builds params{} 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 | |||
for | |||
for param, value in template:gmatch ('|%s*([^=]-)%s*=%s*([^|}]+)') do -- build a table of template parameters and their values | |||
if value then -- there must be a value | |||
if '' ~= value and not value:match ('^%s$') then -- skip when value is empty string or only whitespace | |||
params[param] = mw.text.trim (value); -- add trimmed value else | |||
end | end | ||
end | end | ||
Line 589: | Line 582: | ||
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 not ref then -- |ref= not set, might be cite LSA which doesn't support |ref= | |||
if 'cite lsa' == template_name then | |||
return 'CITEREF' .. (params.last or '') .. (params.year or ''); -- cite LSA always creates an anchor id using only |last= and |year= (no aliases) | |||
end | |||
end | |||
if 'harv' == ref or not ref then -- |ref=harv specified or |ref= missing or empty | if 'harv' == ref or not ref then -- |ref=harv specified or |ref= missing or empty | ||
Line 734: | Line 733: | ||
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); -- add this template's name to the list | ||
--error (template_name) | |||
if data.known_templates_cs12 [template_name] then | if data.known_templates_cs12 [template_name] then | ||
anchor_id = anchor_id_make_cs12 (template); -- extract an anchor id from this template | anchor_id = anchor_id_make_cs12 (template); -- extract an anchor id from this template | ||
Line 794: | Line 793: | ||
for _, anchor_id in ipairs (template) do -- spin through this template's parameter | for _, anchor_id in ipairs (template) do -- spin through this template's parameter | ||
if '' ~= anchor_id and not article_whitelist[anchor_id] then | if '' ~= anchor_id and not article_whitelist[anchor_id] then | ||
article_whitelist[anchor_id] = 1; -- add to the whitelist | article_whitelist[anchor_id] = 1; -- add to the whitelist | ||
end | end |