Content deleted Content added
No edit summary |
sync from sandbox; |
||
Line 1:
require('Module:No globals')
local code_open_tag = '<code style="color: inherit; background: inherit; border: inherit; padding: inherit;">';
--[[--------------------------< I S _ S E T >------------------------------------------------------------------
Line 10 ⟶ 13:
return not (var == nil or var == '');
end
--[[--------------------------< C H E C K _ Y E A R S >--------------------------------------------------------
evaluates params to see if they are one of these forms with or without lowercase letter disambiguator (same as in
Module:Footnotes):
YYYY
n.d.
nd
c. YYYY
YYYY–YYYY (separator is endash)
when anchor_year present, year portion must be same as year param and must have disambiguator
returns empty string when params have correct form; error message else
]]
local function check_years (year, anchor_year)
local y, ay;
if not is_set (year) then -- year is required so return error message when not set
return ' missing ' .. code_open_tag .. '|year=</code>.';
end
local patterns = { -- allowed year patterns from Module:Footnotes (captures added here)
'^(%d%d%d%d?)%l?$', -- YYY or YYYY
'^(n%.d%.)%l?$', -- n.d.
'^(nd)%l?$', -- nd
'^(c%. %d%d%d%d?)%l?$', -- c. YYY or c. YYYY
'^(%d%d%d%d–%d%d%d%d)%l?$' -- YYYY–YYYY
}
for _, pattern in ipairs (patterns) do -- spin through the patterns
y = year:match (pattern); -- y is the year portion
if y then
break; -- when y is set, we found a match so done
end
end
if not y then
return ' invalid ' .. code_open_tag .. '|year=</code>.'; -- y not set, so year is malformed
end
if is_set (anchor_year) then -- anchor_year is optional
for _, pattern in ipairs (patterns) do -- spin through the patterns
ay = anchor_year:match (pattern); -- ay is the year portion
if ay then
break; -- when ay is set, we found a match so done
end
end
if not ay then
return ' invalid ' .. code_open_tag .. '|anchor-year</code>.'; -- ay not set, so anchor_year is malformed
end
if not anchor_year:match ('%l$') then
return ' ' .. code_open_tag .. '|anchor-year=</code> missing dab.'; -- anchor_year must end with a disambiguator letter
end
if y ~= ay then
return ' ' .. code_open_tag .. '|year=</code> / ' .. code_open_tag .. '|anchor-year=</code> mismatch.'; -- 'year' portions of year and anchor_year must be the same
end
end
return ''; -- both years are good; empty string for concatenation
end
--[[--------------------------< M A K E _ N A M E >------------------------------------------------------------
Line 52 ⟶ 123:
local function core( args )
local span_open_tag; -- holds CITEREF and css
local contributors = ''; -- chapter or contribution authors
local source = ''; -- editor/author date list that forms a CITEREF link to a full citation
local local result; -- the assemby of the above output
-- form the CITEREF anchor
Line 62 ⟶ 133:
span_open_tag = '<span id="' .. args.id .. '" class="citation">'; -- for use when contributor name is same as source name
else
span_open_tag = '<span id="CITEREF' .. table.concat (args.citeref) .. (is_set (args['anchor-year']) and args['anchor-year'] or args.year) .. '" class="citation">';
end
Line 84 ⟶ 155:
count = tonumber (args.display_authors) or 0; -- 0 if can't be converted to a number
if 0 >= count then
args.err_msg = args.err_msg .. ' invalid ' .. code_open_tag .. '|display-authors=</code>'; -- if zero, then emit error message
end
end
Line 137 ⟶ 208:
end
source = source .. ' ' .. args.open .. args.year .. args.close; -- add the year with or without brackets
--assemble CITEREF wikilink
Line 150 ⟶ 217:
args.contribution = '[' .. args.url .. ' ' .. args.contribution .. ']'; -- format external link
end
if is_set (args['anchor-year']) then
contributors = contributors .. ' (' .. args['anchor-year'] .. ')' .. args.sepc;
elseif args.sepc ~= contributors:sub(-1) and args.sepc .. ']]' ~= contributors:sub(-3) then
contributors = contributors .. args.sepc; -- add separator if not same as last character in name list (|first=John S. or et al.)
end
Line 181 ⟶ 251:
--[[--------------------------<
Entry point from {{harvc}} template. Fetches parent frame parameters, does a bit of simple error checking
]]
local function harvc (frame)
local args = {
err_msg = '',
Line 193 ⟶ 264:
sepc = '.',
ps = '.',
open = '(', -- year brackets for source year
close = ')',
last = {},
first = {},
Line 213 ⟶ 286:
args.in4 = pframe.args.in4 or '';
args.display_authors = pframe.args['display-authors']; -- the number of contributor names to display; cs1
args.name_list_format = pframe.args['name-list-format']; -- when set to 'harv' display contributor list in sfn or harv style
args.last_author_amp = pframe.args['last-author-amp'] or -- yes only; |last-author-amp=no does not work (though it does in
pframe.args['lastauthoramp'] or '';
args.last_author_amp = args.last_author_amp:lower();
if is_set (pframe.args
args.
args.
args.
args.
args.mask[1] = pframe.args['author-mask'] or pframe.args['author-mask1']; -- get first contributor's article link
args.
args.
args.
args.mask[i] = pframe.args['author-mask'..i]; -- masks
if 5 > i then
args.citeref[i] = args.last[i]; -- collect first four last names for CITEREF anchor
end
i = i + 1 -- bump the index
end
end
Line 259 ⟶ 333:
end -- end of scope limit
if 'yes' == pframe.args.nb then -- if no brackets around year in link to cs1|2 template
args.open = ''; -- unset these
args.close = '';
end
args.url = pframe.args.url or -- url for chapter or contribution
pframe.args['chapter-url'] or
Line 264 ⟶ 343:
args.year = pframe.args.year or ''; -- required
args['anchor-year'] = pframe.args['anchor-year'] or '';
args.err_msg = args.err_msg .. check_years (args.year, args['anchor-year']);
if not is_set (args.contribution) then
Line 275 ⟶ 356:
args.last[4] == args.in4 and
not is_set (args.id) then
args.err_msg = args.err_msg .. ' required
end
Line 281 ⟶ 362:
end
--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
]]
return {
harvc = harvc
};
|