Module:Harvc: Difference between revisions

Content deleted Content added
No edit summary
replace target_check() with call to target_check() in Module:Footnotes, which has full wrapper/whitelist functionality
 
(16 intermediate revisions by 6 users not shown)
Line 1:
require('Module:No globalsstrict')
local anchor_id_list = mw.loadData ('Module:Sandbox/trappist_the_monk/harv_link_test').citerefs;
 
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'] = {'cs1id-lock-registration', 'Free registration required'},
['limited'] = {'cs1id-lock-limited', 'Free access subject to limited trial, subscription normally required'},
['subscription'] = {'cs1id-lock-subscription', 'Paid subscription required'},
}
 
Line 12 ⟶ 11:
--[[--------------------------< 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
look for anchor_id (CITEREF name-list and year or text from |ref=) in anchor_id_list
 
setting |ignore-errors=yes in {{harvc}} will override this check
the 'no target' error may be suppressed with |ignore-err=yes when target cannot be found because target is inside
a template that wraps another template; 'multiple targets' error may not be suppressed
 
]]
 
local function target_check (anchor_id, ignore)
local whitelist_check = require('Module:Footnotes').target_check
local number = anchor_id_list[anchor_id]; -- nil when anchor_id not in list; else a number
local args = {ignore=ignore, template='Harvc', show=true}
local msg;
return whitelist_check(anchor_id, args)
local category;
 
if not number then
if ignore then
return ''; -- if ignore is true then no message, no category
end
msg = 'no target: ' .. anchor_id; -- anchor_id not found in this article
elseif 1 < number then
msg = 'multiple targets (' .. number .. '×): ' .. anchor_id; -- more than one anchor_id in this article
end
 
category = 0 == mw.title.getCurrentTitle().namespace and '[[Category:Harv and Sfn template errors]]' or ''; -- only categorize in article space
 
--TODO: use this version after initial testing
-- return msg and ' <span class="error harv-error" style="display: inline; font-size:100%">Harvc error: ' .. msg .. ' ([[:Category:Harv and Sfn template errors|help]])</span>' .. category or '';
--use this version during initial testing of the live module
return msg and ' <span class="error harv-error" style="display: none; font-size:100%">Harvc error: ' .. msg .. ' ([[:Category:Harv and Sfn template errors|help]])</span>' .. category or '';
end
 
Line 44 ⟶ 26:
--[[--------------------------< I S _ S E T >------------------------------------------------------------------
 
Whether variable is set or not. A varablevariable is set when it is not nil and not empty.
 
]]
Line 106 ⟶ 88:
 
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
Line 168 ⟶ 150:
local source = ''; -- editor/author date list that forms a CITEREF link to a full citation
local in_text = ' In ';
local result; -- the assemby of the above output
 
-- form the CITEREF anchor
Line 182 ⟶ 163:
--[[
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 211 ⟶ 192:
count = #args.last; -- set count to display all of the authors
end
 
while i <= count do
if is_set (contributors) then
Line 222 ⟶ 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 266 ⟶ 247:
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
Line 313 ⟶ 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 .. args.err_msg .. target_err_msg .. '</span>';
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
 
Line 355 ⟶ 354:
 
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.last_author_ampname_list_style = args.last_author_ampname_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 '';
args.last_author_amp = args.last_author_amp:lower(); -- make it case agnostic
if is_set (pframe.args.last) or is_set (pframe.args.last1) or
is_set (pframe.args.author) or is_set (pframe.args.author1) then -- must have at least this to continue
Line 381 ⟶ 378:
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
Line 431 ⟶ 432:
end
 
return table.concat ({core (args), frame:extensionTag ('templatestyles', '', {src='Module:Citation/CS1/styles.css'}), core (args)});
end