http://atlasnacional.ign.es/index.php?title=M%C3%B3dulo:Citation/CS1/Utilities&feed=atom&action=historyMódulo:Citation/CS1/Utilities - Historial de revisiones2024-03-29T14:09:49ZHistorial de revisiones para esta página en el wikiMediaWiki 1.31.0http://atlasnacional.ign.es/index.php?title=M%C3%B3dulo:Citation/CS1/Utilities&diff=3280&oldid=prevAne: 1 revisión importada2018-11-16T07:59:20Z<p>1 revisión importada</p>
<p><b>Página nueva</b></p><div>local u = {}<br />
<br />
local z = {<br />
error_categories = {}; -- for categorizing citations that contain errors<br />
error_ids = {};<br />
message_tail = {};<br />
maintenance_cats = {}; -- for categorizing citations that aren't erroneous per se, but could use a little work<br />
properties_cats = {}; -- for categorizing citations based on certain properties, language of source for instance<br />
};<br />
<br />
<br />
--[[--------------------------< F O R W A R D D E C L A R A T I O N S >--------------------------------------<br />
]]<br />
<br />
local cfg; -- table of tables imported from selected Module:Citation/CS1/Configuration<br />
<br />
<br />
--[[--------------------------< I S _ S E T >------------------------------------------------------------------<br />
<br />
Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string.<br />
<br />
]]<br />
<br />
local function is_set( var )<br />
return not (var == nil or var == '');<br />
end<br />
<br />
<br />
--[[--------------------------< I N _ A R R A Y >--------------------------------------------------------------<br />
<br />
Whether needle is in haystack<br />
<br />
]]<br />
<br />
local function in_array( needle, haystack )<br />
if needle == nil then<br />
return false;<br />
end<br />
for n,v in ipairs( haystack ) do<br />
if v == needle then<br />
return n;<br />
end<br />
end<br />
return false;<br />
end<br />
<br />
<br />
--[[--------------------------< S U B S T I T U T E >----------------------------------------------------------<br />
<br />
Populates numbered arguments in a message string using an argument table.<br />
<br />
]]<br />
<br />
local function substitute( msg, args )<br />
return args and mw.message.newRawMessage( msg, args ):plain() or msg;<br />
end<br />
<br />
<br />
--[[--------------------------< E R R O R _ C O M M E N T >----------------------------------------------------<br />
<br />
Wraps error messages with css markup according to the state of hidden.<br />
<br />
]]<br />
<br />
local function error_comment( content, hidden )<br />
return substitute( hidden and cfg.presentation['hidden-error'] or cfg.presentation['visible-error'], content );<br />
end<br />
<br />
<br />
--[=[-------------------------< M A K E _ W I K I L I N K >----------------------------------------------------<br />
<br />
Makes a wikilink; when bot link and display text is provided, returns a wikilink in the form [[L|D]]; if only<br />
link is provided, returns a wikilink in the form [[L]]; if neither are provided or link is omitted, returns an<br />
empty string.<br />
<br />
]=]<br />
<br />
local function make_wikilink (link, display)<br />
if is_set (link) then<br />
if is_set (display) then<br />
return table.concat ({'[[', link, '|', display, ']]'});<br />
else<br />
return table.concat ({'[[', link, ']]'});<br />
end<br />
else<br />
return '';<br />
end<br />
end<br />
<br />
<br />
--[[--------------------------< S E T _ E R R O R >--------------------------------------------------------------<br />
<br />
Sets an error condition and returns the appropriate error message. The actual placement of the error message in the output is<br />
the responsibility of the calling function.<br />
<br />
]]<br />
<br />
local function set_error( error_id, arguments, raw, prefix, suffix )<br />
local error_state = cfg.error_conditions[ error_id ];<br />
<br />
prefix = prefix or "";<br />
suffix = suffix or "";<br />
<br />
if error_state == nil then<br />
error( cfg.messages['undefined_error'] ); -- because missing error handler in Module:Citation/CS1/Configuration<br />
elseif is_set( error_state.category ) then<br />
table.insert( z.error_categories, error_state.category );<br />
end<br />
<br />
local message = substitute( error_state.message, arguments );<br />
<br />
message = table.concat (<br />
{<br />
message,<br />
' (',<br />
make_wikilink (<br />
table.concat (<br />
{<br />
cfg.messages['help page link'],<br />
'#',<br />
error_state.anchor<br />
}),<br />
cfg.messages['help page label']),<br />
')'<br />
});<br />
<br />
-- message = table.concat ({message, ' (', substitute (cfg.presentation['wikilink'], <br />
-- {cfg.messages['help page link'] .. '#' .. error_state.anchor, cfg.messages['help page label']}), ')'});<br />
-- message = message .. " ([[" .. cfg.messages['help page link'] .. <br />
-- "#" .. error_state.anchor .. "|" ..<br />
-- cfg.messages['help page label'] .. "]])";<br />
<br />
z.error_ids[ error_id ] = true;<br />
if in_array( error_id, { 'bare_url_missing_title', 'trans_missing_title' } )<br />
and z.error_ids['citation_missing_title'] then<br />
return '', false;<br />
end<br />
<br />
message = table.concat({ prefix, message, suffix });<br />
<br />
if raw == true then<br />
return message, error_state.hidden;<br />
end <br />
<br />
return error_comment( message, error_state.hidden );<br />
end<br />
<br />
<br />
--[[-------------------------< I S _ A L I A S _ U S E D >-----------------------------------------------------<br />
<br />
This function is used by select_one() to determine if one of a list of alias parameters is in the argument list<br />
provided by the template.<br />
<br />
Input:<br />
args – pointer to the arguments table from calling template<br />
alias – one of the list of possible aliases in the aliases lists from Module:Citation/CS1/Configuration<br />
index – for enumerated parameters, identifies which one<br />
enumerated – true/false flag used choose how enumerated aliases are examined<br />
value – value associated with an alias that has previously been selected; nil if not yet selected<br />
selected – the alias that has previously been selected; nil if not yet selected<br />
error_list – list of aliases that are duplicates of the alias already selected<br />
<br />
Returns:<br />
value – value associated with alias we selected or that was previously selected or nil if an alias not yet selected<br />
selected – the alias we selected or the alias that was previously selected or nil if an alias not yet selected<br />
<br />
]]<br />
<br />
local function is_alias_used (args, alias, index, enumerated, value, selected, error_list)<br />
if enumerated then -- is this a test for an enumerated parameters?<br />
alias = alias:gsub ('#', index); -- replace '#' with the value in index<br />
else<br />
alias = alias:gsub ('#', ''); -- remove '#' if it exists<br />
end<br />
<br />
if is_set(args[alias]) then -- alias is in the template's argument list<br />
if value ~= nil and selected ~= alias then -- if we have already selected one of the aliases<br />
local skip;<br />
for _, v in ipairs(error_list) do -- spin through the error list to see if we've added this alias<br />
if v == alias then<br />
skip = true;<br />
break; -- has been added so stop looking <br />
end<br />
end<br />
if not skip then -- has not been added so<br />
table.insert( error_list, alias ); -- add error alias to the error list<br />
end<br />
else<br />
value = args[alias]; -- not yet selected an alias, so select this one<br />
selected = alias;<br />
end<br />
end<br />
return value, selected; -- return newly selected alias, or previously selected alias<br />
end<br />
<br />
<br />
--[[--------------------------< A D D _ M A I N T _ C A T >------------------------------------------------------<br />
<br />
Adds a category to z.maintenance_cats using names from the configuration file with additional text if any.<br />
To prevent duplication, the added_maint_cats table lists the categories by key that have been added to z.maintenance_cats.<br />
<br />
]]<br />
<br />
local added_maint_cats = {} -- list of maintenance categories that have been added to z.maintenance_cats<br />
local function add_maint_cat (key, arguments)<br />
if not added_maint_cats [key] then<br />
added_maint_cats [key] = true; -- note that we've added this category<br />
table.insert( z.maintenance_cats, substitute (cfg.maint_cats [key], arguments)); -- make name then add to table<br />
end<br />
end<br />
<br />
<br />
--[[--------------------------< S A F E _ F O R _ I T A L I C S >----------------------------------------------<br />
<br />
Protects a string that will be wrapped in wiki italic markup '' ... ''<br />
<br />
Note: We cannot use <i> for italics, as the expected behavior for italics specified by ''...'' in the title is that<br />
they will be inverted (i.e. unitalicized) in the resulting references. In addition, <i> and '' tend to interact<br />
poorly under Mediawiki's HTML tidy.<br />
<br />
]]<br />
<br />
local function safe_for_italics( str )<br />
if not is_set(str) then<br />
return str;<br />
else<br />
if str:sub(1,1) == "'" then str = "<span></span>" .. str; end<br />
if str:sub(-1,-1) == "'" then str = str .. "<span></span>"; end<br />
<br />
-- Remove newlines as they break italics.<br />
return str:gsub( '\n', ' ' );<br />
end<br />
end<br />
<br />
<br />
--[[--------------------------< W R A P _ S T Y L E >----------------------------------------------------------<br />
<br />
Applies styling to various parameters. Supplied string is wrapped using a message_list configuration taking one<br />
argument; protects italic styled parameters. Additional text taken from citation_config.presentation - the reason<br />
this function is similar to but separate from wrap_msg().<br />
<br />
]]<br />
<br />
local function wrap_style (key, str)<br />
if not is_set( str ) then<br />
return "";<br />
elseif in_array( key, { 'italic-title', 'trans-italic-title' } ) then<br />
str = safe_for_italics( str );<br />
end<br />
<br />
return substitute( cfg.presentation[key], {str} );<br />
end<br />
<br />
<br />
--[[--------------------------< S E L E C T _ O N E >----------------------------------------------------------<br />
<br />
Chooses one matching parameter from a list of parameters to consider. The list of parameters to consider is just<br />
names. For parameters that may be enumerated, the position of the numerator in the parameter name is identified<br />
by the '#' so |author-last1= and |author1-last= are represented as 'author-last#' and 'author#-last'.<br />
<br />
Because enumerated parameter |<param>1= is an alias of |<param>= we must test for both possibilities.<br />
<br />
<br />
Generates an error if more than one match is present.<br />
<br />
]]<br />
<br />
local function select_one( args, aliases_list, error_condition, index )<br />
local value = nil; -- the value assigned to the selected parameter<br />
local selected = ''; -- the name of the parameter we have chosen<br />
local error_list = {};<br />
<br />
if index ~= nil then index = tostring(index); end<br />
<br />
for _, alias in ipairs( aliases_list ) do -- for each alias in the aliases list<br />
if alias:match ('#') then -- if this alias can be enumerated<br />
if '1' == index then -- when index is 1 test for enumerated and non-enumerated aliases<br />
value, selected = is_alias_used (args, alias, index, false, value, selected, error_list); -- first test for non-enumerated alias<br />
end<br />
value, selected = is_alias_used (args, alias, index, true, value, selected, error_list); -- test for enumerated alias<br />
else<br />
value, selected = is_alias_used (args, alias, index, false, value, selected, error_list); --test for non-enumerated alias<br />
end<br />
end<br />
<br />
if #error_list > 0 and 'none' ~= error_condition then -- for cases where this code is used outside of extract_names()<br />
local error_str = "";<br />
for _, k in ipairs( error_list ) do<br />
if error_str ~= "" then error_str = error_str .. cfg.messages['parameter-separator'] end<br />
error_str = error_str .. wrap_style ('parameter', k);<br />
end<br />
if #error_list > 1 then<br />
error_str = error_str .. cfg.messages['parameter-final-separator'];<br />
else<br />
error_str = error_str .. cfg.messages['parameter-pair-separator'];<br />
end<br />
error_str = error_str .. wrap_style ('parameter', selected);<br />
table.insert( z.message_tail, { set_error( error_condition, {error_str}, true ) } );<br />
end<br />
<br />
return value, selected;<br />
end<br />
<br />
<br />
--[=[-------------------------< R E M O V E _ W I K I _ L I N K >----------------------------------------------<br />
<br />
Gets the display text from a wikilink like [[A|B]] or [[B]] gives B<br />
<br />
The str:gsub() returns either A|B froma [[A|B]] or B from [[B]] or B from B (no wikilink markup).<br />
<br />
In l(), l:gsub() removes the link and pipe (if they exist); the second :gsub() trims white space from the label<br />
if str was wrapped in wikilink markup. Presumably, this is because without wikimarkup in str, there is no match<br />
in the initial gsub, the replacement function l() doesn't get called.<br />
<br />
]=]<br />
<br />
local function remove_wiki_link (str)<br />
return (str:gsub( "%[%[([^%[%]]*)%]%]", function(l)<br />
return l:gsub( "^[^|]*|(.*)$", "%1" ):gsub("^%s*(.-)%s*$", "%1");<br />
end));<br />
end<br />
<br />
<br />
--[=[-------------------------< I S _ W I K I L I N K >--------------------------------------------------------<br />
<br />
Determines if str is a wikilink, extracts, and returns the the wikilink type, link text, and display text parts.<br />
If str is a complex wikilink ([[L|D]]):<br />
returns wl_type 2 and D and L from [[L|D]];<br />
if str is a simple wikilink ([[D]])<br />
returns wl_type 1 and D from [[D]] and L as empty string;<br />
if not a wikilink:<br />
returns wl_type 0, str as D, and L as empty string.<br />
<br />
trims leading and trailing white space and pipes from L and D ([[L|]] and [[|D]] are accepted by MediaWiki and<br />
treated like [[D]]; while [[|D|]] is not accepted by MediaWiki, here, we accept it and return D without the pipes).<br />
<br />
]=]<br />
<br />
local function is_wikilink (str)<br />
local D, L<br />
local wl_type = 2; -- assume that str is a complex wikilink [[L|D]]<br />
<br />
L, D = str:match ('%[%[([^|]+)|([^%]]+)%]%]'); -- get L and D from [[L|D]] <br />
<br />
if not is_set (D) then -- if no separate link<br />
D = str:match ('%[%[([^%]]*)|*%]%]'); -- get D from [[D]]<br />
wl_type = 1; <br />
end<br />
<br />
if not is_set (D) then -- no wikilink markup<br />
D = str; -- return the string as D<br />
wl_type = 0; -- but say that it is not a wikilink<br />
end<br />
<br />
D = mw.text.trim (D, '%s|'); -- trim white space and pipe characters <br />
L = L and mw.text.trim (L, '%s|');<br />
<br />
return wl_type, D, L or '';<br />
end<br />
<br />
<br />
--[[--------------------------< S E T _ S E L E C T E D _ M O D U L E S >--------------------------------------<br />
<br />
Sets local cfg table to same (live or sandbox) as that used by the other modules.<br />
<br />
]]<br />
<br />
local function set_selected_modules (cfg_table_ptr)<br />
cfg = cfg_table_ptr;<br />
<br />
end<br />
<br />
<br />
<br />
return { -- return exported functions and tables<br />
is_set = is_set,<br />
in_array = in_array,<br />
substitute = substitute,<br />
error_comment = error_comment,<br />
set_error = set_error,<br />
select_one = select_one,<br />
add_maint_cat = add_maint_cat,<br />
wrap_style = wrap_style,<br />
safe_for_italics = safe_for_italics,<br />
remove_wiki_link = remove_wiki_link,<br />
is_wikilink = is_wikilink,<br />
make_wikilink = make_wikilink,<br />
set_selected_modules = set_selected_modules,<br />
z = z,<br />
}</div>Ane