Module:Citation/CS1/Utilities: Difference between revisions
m
1 revision imported
en>Trappist the monk (update per RfC;) |
Vivaporius (talk | contribs) m (1 revision imported) |
||
Line 1:
local u = {}
local z = {
error_ids = {};
message_tail = {};
};
Line 21 ⟶ 22:
]]
local function is_set
return not (var == nil or var == '');
end
Line 32 ⟶ 33:
]]
local function in_array
if needle == nil then
return false;
end
for n,
if v == needle then
return n;
Line 42 ⟶ 43:
end
return false;
end
Line 74 ⟶ 48:
--[[--------------------------< S U B S T I T U T E >----------------------------------------------------------
Populates numbered arguments in a message string using an argument table.
]]
local function substitute
return args and mw.message.newRawMessage
end
Line 86 ⟶ 59:
--[[--------------------------< E R R O R _ C O M M E N T >----------------------------------------------------
Wraps error messages with
]]
local function error_comment
return substitute
end
Line 158 ⟶ 70:
--[=[-------------------------< M A K E _ W I K I L I N K >----------------------------------------------------
Makes a wikilink; when
link is provided
]=]
local function make_wikilink (link, display)
if
if is_set (display) then
else
return table.concat ({'[[', link, ']]'});
end
else
return
end
end
--[[--------------------------< S E T _
Sets an error
the responsibility of the calling function.
]]
local function set_error( error_id, arguments, raw, prefix, suffix )
local error_state = cfg.error_conditions[ error_id ];
prefix = prefix or
suffix = suffix or
if error_state == nil then
error(
elseif is_set( error_state.category ) then
end
local message = substitute
message = table.concat (
Line 235 ⟶ 125:
});
-- message = table.concat ({message, ' (', substitute (cfg.presentation['wikilink'],
-- {cfg.messages['help page link'] .. '#' .. error_state.anchor, cfg.messages['help page label']}), ')'});
-- message = message .. " ([[" .. cfg.messages['help page link'] ..
-- "#" .. error_state.anchor .. "|" ..
-- cfg.messages['help page label'] .. "]])";
z.error_ids[ error_id ] = true;
if in_array( error_id, { 'bare_url_missing_title', 'trans_missing_title' } )
and z.error_ids['citation_missing_title'] then
return '', false;
end
message = table.concat
if
return message, error_state.hidden;
end
end
Line 262 ⟶ 156:
alias – one of the list of possible aliases in the aliases lists from Module:Citation/CS1/Configuration
index – for enumerated parameters, identifies which one
enumerated – true/false flag used
value – value associated with an alias that has previously been selected; nil if not yet selected
selected – the alias that has previously been selected; nil if not yet selected
Line 280 ⟶ 174:
end
if is_set
if value ~= nil and selected ~= alias then -- if we have already selected one of the aliases
local skip;
for _, v in ipairs
if v == alias then
skip = true;
Line 290 ⟶ 184:
end
if not skip then -- has not been added so
table.insert
end
else
Line 303 ⟶ 197:
--[[--------------------------< A D D _ M A I N T _ C A T >------------------------------------------------------
Adds a category to z.
To prevent duplication, the added_maint_cats table lists the categories by key that have been added to z.
]]
local added_maint_cats = {} -- list of maintenance categories that have been added to z.maintenance_cats
local function add_maint_cat (key, arguments)
if not added_maint_cats [key] then
added_maint_cats [key] = true; -- note that we've added this category
table.insert
end
end
Line 350 ⟶ 221:
]]
local function safe_for_italics
if not is_set
return str;
else
if str:sub(-1,-1) == "'" then str = str .. "<span></span>"; end
-- Remove newlines as they break italics.
return str:gsub( '\n', ' ' );
end
end
Line 369 ⟶ 243:
local function wrap_style (key, str)
if not is_set
return "";
elseif in_array
str = safe_for_italics
end
return substitute
end
Line 424 ⟶ 266:
]]
local function select_one
local value = nil; -- the value assigned to the selected parameter
local selected = ''; -- the name of the parameter we have chosen
Line 431 ⟶ 273:
if index ~= nil then index = tostring(index); end
for _, alias in ipairs
if alias:match ('#') then -- if this alias can be enumerated
if '1' == index then -- when index is 1 test for enumerated and non-enumerated aliases
value, selected = is_alias_used (args, alias, index, false, value, selected, error_list); -- first test for non-enumerated alias
end
value, selected = is_alias_used (args, alias, index, true, value, selected, error_list); -- test for enumerated alias
else
value, selected = is_alias_used (args, alias, index, false, value, selected, error_list); --
end
end
if #error_list > 0 and 'none' ~= error_condition then -- for cases where this code is used outside of extract_names()
local error_str = "";
for _, k in ipairs( error_list ) do
if error_str ~= "" then error_str = error_str .. cfg.messages['parameter-separator'] end
error_str = error_str .. wrap_style ('parameter', k);
end
if #error_list > 1 then
error_str = error_str .. cfg.messages['parameter-final-separator'];
else
error_str = error_str .. cfg.messages['parameter-pair-separator'];
end
table.insert( z.message_tail, { set_error( error_condition, {error_str}, true ) } );
end
Line 460 ⟶ 309:
The str:gsub() returns either A|B froma [[A|B]] or B from [[B]] or B from B (no wikilink markup).
In l(), l:gsub() removes the link and pipe (if they exist); the second :gsub() trims
if str was wrapped in wikilink markup. Presumably, this is because without wikimarkup in str, there is no match
in the initial gsub, the replacement function l() doesn't get called.
Line 467 ⟶ 316:
local function remove_wiki_link (str)
return (str:gsub
return l:gsub
end));
end
Line 475 ⟶ 324:
--[=[-------------------------< I S _ W I K I L I N K >--------------------------------------------------------
Determines if str is a wikilink, extracts, and returns the the wikilink type, link text, and display text parts.
If str is a complex wikilink ([[L|D]]):
returns wl_type 2 and D and L from [[L|D]];
Line 483 ⟶ 332:
returns wl_type 0, str as D, and L as empty string.
trims leading and trailing
treated like [[D]]; while [[|D|]] is not accepted by MediaWiki, here, we accept it and return D without the pipes).
Line 492 ⟶ 341:
local wl_type = 2; -- assume that str is a complex wikilink [[L|D]]
if not is_set (D) then -- if no separate
D = str:match ('
wl_type = 1;
end
if not is_set (D) then -- no wikilink markup
D = str; -- return the string as D
wl_type = 0; -- but say that it is not a wikilink
end
D = mw.text.trim (D, '%s|'); -- trim white space and pipe characters
L = L and mw.text.trim (L, '%s|');
return wl_type, D, L or '';
end
Line 560 ⟶ 372:
return { -- return exported functions and tables
is_set = is_set,
in_array = in_array,
substitute = substitute,
error_comment = error_comment,
set_error = set_error,
select_one = select_one,
add_maint_cat = add_maint_cat,
wrap_style = wrap_style,
safe_for_italics = safe_for_italics,
remove_wiki_link = remove_wiki_link,
is_wikilink = is_wikilink,
make_wikilink = make_wikilink,
set_selected_modules = set_selected_modules,
z = z,
}
|