Module:Harvc: Difference between revisions

Content deleted Content added
Synch from sandbox;
replace target_check() with call to target_check() in Module:Footnotes, which has full wrapper/whitelist functionality
 
(24 intermediate revisions by 6 users not shown)
Line 1:
require('strict')
local f = {};
 
local code_open_tag = '<code class="cs1-code">'; -- cs1-code class defined in Module:Citation/CS1/styles.css
local lock_icons = { --icon classes are defined in Module:Citation/CS1/styles.css
['registration'] = {'id-lock-registration', 'Free registration required'},
['limited'] = {'id-lock-limited', 'Free access subject to limited trial, subscription normally required'},
['subscription'] = {'id-lock-subscription', 'Paid subscription required'},
}
 
 
--[[--------------------------< T A R G E T _ C H E C K >------------------------------------------------------
 
check to see if target of harvc exists in the page somewhere. Uses the target check mechanism from Module:Footnotes
 
setting |ignore-errors=yes in {{harvc}} will override this check
 
]]
 
local function target_check (anchor_id, ignore)
local whitelist_check = require('Module:Footnotes').target_check
local args = {ignore=ignore, template='Harvc', show=true}
return whitelist_check(anchor_id, args)
end
 
 
--[[--------------------------< I S _ S E T >------------------------------------------------------------------
 
Whether variable is set or not. A varablevariable is set when it is not nil and not empty.
 
]]
Line 10 ⟶ 33:
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)
YYYY–YY (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
'^(%d%d%d%d–%d%d)%l?$' -- YYYY–YY
}
 
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 29 ⟶ 122:
if is_set (mask) then -- mask this author
mask =if tonumber (mask); -- because the value provided might not be a numberthen
if is_set (mask) then
name = string.rep ('—', mask) -- make a string that number length of mdashes
else
name = mask; -- mask is not a number so use the mask text
end
end
Line 52 ⟶ 146:
 
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 mimicsin_text harvnb= output' exceptIn year in parentheses';
local in_text = ' In '; --
local result; -- the assemby of the above output
 
-- form the CITEREF anchor
if is_set (args.id) then
args.id = mw.uri.anchorEncode (args.id)
span_open_tag = '<span id="' .. args.id .. '" class="citation">'; -- for use when contributor name is same as source name
else
span_open_taglocal citeref = '<span id="CITEREF' .. table.concat (args.citeref) .. (is_set (args.['anchor-year']) ..and args['" class="citation">anchor-year'] or args.year);
citeref = mw.uri.anchorEncode (citeref);
span_open_tag = '<span id="' .. citeref .. '" class="citation">';
end
--[[
form the contributors display list:
if |name-list-formatstyle=harv, display is similar to {{sfn}} and {{harv}}, 1 to 4 last names;
if |display-authors= is empty or omitted, display is similar to cs1|2: display all names in last, first order
if |display-authors=etal then displays all author names in last, first order and append et al.
if value assigned to |display-authors= is less than the number of author last names, displays the specified number of author names in last, first order followed by et al.
]]
if 'harv' ~= args.name_list_formatname_list_style then -- default cs1|2 style contributor list
local i = 1;
local count;
Line 84 ⟶ 180:
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 96 ⟶ 192:
count = #args.last; -- set count to display all of the authors
end
 
while i <= count do
if is_set (contributors) then
Line 107 ⟶ 203:
if true == etal then
contributors = contributors .. ' et al.'; -- append et al.
elseif 'yesamp' == args.last_author_ampname_list_style then
contributors = contributors:gsub('; ([^;]+)$', ' & %1') -- replace last separator with ' & '
end
Line 137 ⟶ 233:
end
 
source = source .. ' ' .. args.open .. args.year .. args.close; -- add the year with or without brackets
if args.year:match('^[1-9]%d%d%d?%l?$') or args.year:match('^n%.d%.%l?$') or args.year:match('^nd%l?$') then -- 3 or 4 digits, n.d., or nd and optional disambiguator
source = source .. ' (' .. args.year .. ')';
else
args.err_msg = args.err_msg .. ' invalid or missing year.'; -- error message if year not provided or is imalformed
end
 
--assemble CITEREF wikilink
local anchor_id;
source = "[[#CITEREF" .. mw.uri.anchorEncode(table.concat ({args.in1, args.in2, args.in3, args.in4, args.year})) .. "|" .. source .. "]]";
local target_err_msg;
if '' ~= args.ref then
anchor_id = mw.uri.anchorEncode (args.ref)
else
anchor_id = mw.uri.anchorEncode(table.concat ({'CITEREF', args.in1, args.in2, args.in3, args.in4, args.year}));
end
target_err_msg = target_check (anchor_id, args.ignore); -- see if there is a target for this anchor_id
source = '[[#' .. anchor_id .. "|" .. source .. "]]";
 
-- special case for afterword, foreword, introduction, preface
local no_quotes = ({['afterword']=true, ['foreword']=true, ['introduction']=true, ['preface']=true})[args.contribution:lower()];
 
--combine contribution with url to make external link
if args.url ~= '' then
args.contribution = '[' .. args.url .. ' ' .. args.contribution .. ']'; -- format external link
 
if args['url-access'] then
if lock_icons[args['url-access']] then
args.contribution = table.concat ({ -- add access icon markup to this item
'<span class="', -- open the opening span tag; icon classes are defined in Module:Citation/CS1/styles.css
lock_icons[args['url-access']][1], -- add the appropriate lock icon class
'" title="', -- and the title attribute
lock_icons[args['url-access']][2], -- for an appropriate tool tip
'">', -- close the opening span tag
args.contribution,
'</span>', -- and close the span
});
end
end
end
 
if args.sepc ~= contributors:sub(-1) and args.sepc .. ']]' ~= contributors:sub(-3) then
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 175 ⟶ 297:
 
-- and put it all together
local result = {}; -- the assemby of the above output
result = span_open_tag .. contributors .. ' "' .. args.contribution .. '"' .. args.sepc .. in_text .. source .. args.p .. args.ps .. '</span>' .. args.err_msg;
table.insert (result, span_open_tag);
table.insert (result, contributors);
table.insert (result, no_quotes and ' ' or ' "'); -- foreword, afterword, introduction, preface contributions are not quoted; all other contributions are
table.insert (result, args.contribution);
table.insert (result, no_quotes and '' or '"'); -- foreword, afterword, introduction, preface contributions are not quoted; all other contributions are
table.insert (result, args.sepc);
table.insert (result, in_text);
table.insert (result, source);
table.insert (result, args.p);
table.insert (result, args.ps);
table.insert (result, args.err_msg);
if not is_set(args.err_msg) then
table.insert (result, target_err_msg)
end
table.insert (result, '</span>');
 
return table.concat (result); -- make a string and done
end
 
 
--[[--------------------------< F . H A R V C >--------------------------------------------------------------------
 
Entry point from {{harvc}} template. Fetches parent frame parameters, does a bit of simple error checking
 
]]
 
function f.harvc (frame)
local function harvc (frame)
local args = {
err_msg = '',
Line 193 ⟶ 331:
sepc = '.',
ps = '.',
open = '(', -- year brackets for source year
close = ')',
last = {},
first = {},
Line 213 ⟶ 353:
args.in4 = pframe.args.in4 or '';
 
args.display_authors = pframe.args['display-authors']; -- the number of contributor names to display; cs1/|2 format includes first names
args.name_list_formatname_list_style = pframe.args['name-list-formatstyle'] or ''; -- when set to 'harv' display contributor list in sfn or harv style
args.name_list_style = args.name_list_style:lower(); -- make it case agnostic
args.last_author_amp = pframe.args['last-author-amp'] or -- yes only; |last-author-amp=no does not work (though it does in CS1|2)
 
pframe.args['lastauthoramp'] or '';
if is_set (pframe.args.last) or is_set (pframe.args.last1) or
args.last_author_amp:lower(); -- make it case agnostic
is_set (pframe.args.author) or is_set (pframe.args.author1) then -- must have at least this to continue
if args.last[1] is_set= (pframe.args['last1']).last or is_setpframe.args.last1 or (pframe.args['last']).author thenor pframe.args.author1; -- mustget havefirst atcontributor's least this tolast continuename
args.lastciteref[1] = pframe.args.last or pframe.args.last1[1]; -- getadd firstit contributor'sto lastthe nameciteref
args.citereffirst[1] = pframe.args.last[1]first or pframe.args.first1; -- addget itfirst tocontributor's thefirst citerefname
args.firstlink[1] = pframe.args.first['author-link'] or pframe.args.first1['author-link1']; -- get first contributor's firstarticle namelink
args.linkmask[1] = pframe.args['author-linkmask'] or pframe.args['author-link1mask1']; -- get first contributor's article link
args.mask[1] = pframe.args['author-mask'] or pframe.args['author-mask1']; -- get first contributor's article link
local i = 2; -- index for the rest of the names
local while is_set (pframe.args['last'..i]) =or 2; is_set (pframe.args['author'..i]) do -- indexloop through pframe.args and forget the rest of the names
while args.last[i] is_set= (pframe.args['last'..i]) do or pframe.args['author'..i]; -- loop through pfram.args and get the rest of thelast names
args.lastfirst[i] = pframe.args['lastfirst'..i]; -- lastfirst names
args.firstlink[i] = pframe.args['firstauthor-link'..i]; -- first nameslinks
args.linkmask[i] = pframe.args['author-linkmask'..i]; -- first namesmasks
if 5 > i then
args.mask[i] = pframe.args['author-mask'..i]; -- first names
args.citeref[i] = args.last[i]; -- collect first four last names for CITEREF anchor
if 5 > i then
end
args.citeref[i] = args.last[i]; -- collect first four last names for CITEREF anchor
i = i + 1 -- bump the index
end
i = i + 1 -- bump the index
end
end
 
args.pif 0 == pframe.#args.plast or '';then -- source page number(s)|last= oris ___locationrequired
args.pperr_msg = pframe.args.pperr_msg or.. ' no authors in contributor list.';
end
 
args.p = pframe.args.p or pframe.args.page or ''; -- source page number(s) or ___location
args.pp = pframe.args.pp or pframe.args.pages or '';
args.loc = pframe.args.loc or '';
args.ref = pframe.args.ref or pframe.args.Ref or ''; -- used to match |ref=<text> in cs1|2 source template
args.ignore = 'yes' == pframe.args['ignore-err']; -- suppress false-positive 'no target' errors
 
if 'cs2' == pframe.args.mode then
Line 259 ⟶ 404:
end -- end of scope limit
 
args.urlif 'yes' == pframe.args.urlnb or '';then -- urlif no brackets around year in forlink chapterto orcs1|2 contributiontemplate
args.open = ''; -- unset these
args.close = '';
end
args.url = pframe.args.url or -- url for chapter or contribution
pframe.args['chapter-url'] or
pframe.args['contribution-url'] or '';
args['url-access'] = pframe.args['url-access'];
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 273 ⟶ 429:
args.last[4] == args.in4 and
not is_set (args.id) then
args.err_msg = args.err_msg .. ' required &#124;' .. code_open_tag .. '|id=</code> parameter missing.'; -- error message if contributor and source are the same
end
 
return table.concat ({frame:extensionTag ('templatestyles', '', {src='Module:Citation/CS1/styles.css'}), core (args)});
return core (args);
end
 
 
return f;
--[[--------------------------< E X P O R T E D F U N C T I O N S >------------------------------------------
]]
 
return {
harvc = harvc
};