Module:Citation/CS1/Utilities: Difference between revisions
Content added Content deleted
m (1 revision imported: Help:Footnotes) |
(sync from sandbox;) |
||
Line 1: | Line 1: | ||
local u = {} |
|||
local z = { |
local z = { |
||
Line 13: | Line 12: | ||
]] |
]] |
||
local cfg; -- table of tables imported from |
local cfg; -- table of tables imported from selected Module:Citation/CS1/Configuration |
||
Line 19: | Line 18: | ||
Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string. |
Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string. |
||
This function is global because it is called from both this module and from Date validation |
|||
]] |
]] |
||
local function is_set( var ) |
local function is_set( var ) |
||
return not (var == nil or var == ''); |
return not (var == nil or var == ''); |
||
Line 62: | Line 61: | ||
]] |
]] |
||
local function error_comment( content, hidden ) |
local function error_comment( content, hidden ) |
||
return substitute( hidden and cfg.presentation['hidden-error'] or cfg.presentation['visible-error'], content ); |
return substitute( hidden and cfg.presentation['hidden-error'] or cfg.presentation['visible-error'], content ); |
||
end |
|||
--[=[-------------------------< M A K E _ W I K I L I N K >---------------------------------------------------- |
|||
Makes a wikilink; when bot link and display text is provided, returns a wikilink in the form [[L|D]]; if only |
|||
link is provided, returns a wikilink in the form [[L]]; if neither are provided or link is omitted, returns an |
|||
empty string. |
|||
]=] |
|||
local function make_wikilink (link, display) |
|||
if is_set (link) then |
|||
if is_set (display) then |
|||
return table.concat ({'[[', link, '|', display, ']]'}); |
|||
else |
|||
return table.concat ({'[[', link, ']]'}); |
|||
end |
|||
else |
|||
return ''; |
|||
end |
|||
end |
end |
||
Line 73: | Line 94: | ||
]] |
]] |
||
local function set_error( error_id, arguments, raw, prefix, suffix ) |
local function set_error( error_id, arguments, raw, prefix, suffix ) |
||
local error_state = cfg.error_conditions[ error_id ]; |
local error_state = cfg.error_conditions[ error_id ]; |
||
Line 80: | Line 102: | ||
if error_state == nil then |
if error_state == nil then |
||
error( cfg.messages['undefined_error'] ); |
error( cfg.messages['undefined_error'] ); -- because missing error handler in Module:Citation/CS1/Configuration |
||
elseif is_set( error_state.category ) then |
elseif is_set( error_state.category ) then |
||
table.insert( z.error_categories, error_state.category ); |
table.insert( z.error_categories, error_state.category ); |
||
Line 86: | Line 108: | ||
local message = substitute( error_state.message, arguments ); |
local message = substitute( error_state.message, arguments ); |
||
message = table.concat ( |
|||
message = message .. " ([[" .. cfg.messages['help page link'] .. |
|||
{ |
|||
"#" .. error_state.anchor .. "|" .. |
|||
message, |
|||
cfg.messages['help page label'] .. "]])"; |
|||
' (', |
|||
make_wikilink ( |
|||
table.concat ( |
|||
{ |
|||
cfg.messages['help page link'], |
|||
'#', |
|||
error_state.anchor |
|||
}), |
|||
cfg.messages['help page label']), |
|||
')' |
|||
}); |
|||
z.error_ids[ error_id ] = true; |
z.error_ids[ error_id ] = true; |
||
if in_array( error_id, { 'bare_url_missing_title', 'trans_missing_title' } ) |
if in_array( error_id, { 'bare_url_missing_title', 'trans_missing_title' } ) |
||
Line 266: | Line 299: | ||
Gets the display text from a wikilink like [[A|B]] or [[B]] gives B |
Gets the display text from a wikilink like [[A|B]] or [[B]] gives B |
||
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 white space from the label |
|||
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 273: | Line 312: | ||
return l:gsub( "^[^|]*|(.*)$", "%1" ):gsub("^%s*(.-)%s*$", "%1"); |
return l:gsub( "^[^|]*|(.*)$", "%1" ):gsub("^%s*(.-)%s*$", "%1"); |
||
end)); |
end)); |
||
end |
|||
--[=[-------------------------< 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]]; |
|||
if str is a simple wikilink ([[D]]) |
|||
returns wl_type 1 and D from [[D]] and L as empty string; |
|||
if not a wikilink: |
|||
returns wl_type 0, str as D, and L as empty string. |
|||
trims leading and trailing white space and pipes from L and D ([[L|]] and [[|D]] are accepted by MediaWiki and |
|||
treated like [[D]]; while [[|D|]] is not accepted by MediaWiki, here, we accept it and return D without the pipes). |
|||
]=] |
|||
local function is_wikilink (str) |
|||
local D, L |
|||
local wl_type = 2; -- assume that str is a complex wikilink [[L|D]] |
|||
if not str:match ('^%[%[[^%]]+%]%]$') then -- is str some sort of a wikilink (must have some sort of content) |
|||
return 0, str, ''; -- not a wililink; return wl_type as 0, str as D, and empty string as L |
|||
end |
|||
L, D = str:match ('^%[%[([^|]+)|([^%]]+)%]%]$'); -- get L and D from [[L|D]] |
|||
if not is_set (D) then -- if no separate display |
|||
D = str:match ('^%[%[([^%]]*)|*%]%]$'); -- get D from [[D]] or [[D|]] |
|||
wl_type = 1; |
|||
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 |
|||
--[[--------------------------< S T R I P _ A P O S T R O P H E _ M A R K U P >-------------------------------- |
|||
Strip wiki italic and bold markup from argument so that it doesn't contaminate COinS metadata. |
|||
This function strips common patterns of apostrophe markup. We presume that editors who have taken the time to |
|||
markup a title have, as a result, provided valid markup. When they don't, some single apostrophes are left behind. |
|||
Returns the argument without wiki markup and a number; the number is more-or-less meaningless except as a flag |
|||
to indicate that markup was replaced; do not rely on it as an indicator of how many of any kind of markup was |
|||
removed; returns the argument and nil when no markup removed |
|||
]] |
|||
local function strip_apostrophe_markup (argument) |
|||
if not is_set (argument) then |
|||
return argument, nil; -- no argument, nothing to do |
|||
end |
|||
if nil == argument:find ( "''", 1, true ) then -- Is there at least one double apostrophe? If not, exit. |
|||
return argument, nil; |
|||
end |
|||
local flag; |
|||
while true do |
|||
if argument:find ( "'''''", 1, true ) then -- bold italic (5) |
|||
argument, flag=argument:gsub("%'%'%'%'%'", ""); -- remove all instances of it |
|||
elseif argument:find ( "''''", 1, true ) then -- italic start and end without content (4) |
|||
argument, flag=argument:gsub("%'%'%'%'", ""); |
|||
elseif argument:find ( "'''", 1, true ) then -- bold (3) |
|||
argument, flag=argument:gsub("%'%'%'", ""); |
|||
elseif argument:find ( "''", 1, true ) then -- italic (2) |
|||
argument, flag=argument:gsub("%'%'", ""); |
|||
else |
|||
break; |
|||
end |
|||
end |
|||
return argument, flag; -- done |
|||
end |
end |
||
Line 284: | Line 399: | ||
local function set_selected_modules (cfg_table_ptr) |
local function set_selected_modules (cfg_table_ptr) |
||
cfg = cfg_table_ptr; |
cfg = cfg_table_ptr; |
||
end |
end |
||
--[[--------------------------< E X P O R T S >---------------------------------------------------------------- |
|||
]] |
|||
return { |
|||
return { -- return exported functions and tables |
|||
is_set = is_set, |
is_set = is_set, -- exported functions |
||
in_array = in_array, |
in_array = in_array, |
||
substitute = substitute, |
substitute = substitute, |
||
Line 296: | Line 414: | ||
select_one = select_one, |
select_one = select_one, |
||
add_maint_cat = add_maint_cat, |
add_maint_cat = add_maint_cat, |
||
wrap_style = wrap_style |
wrap_style = wrap_style, |
||
safe_for_italics = safe_for_italics |
safe_for_italics = safe_for_italics, |
||
remove_wiki_link = remove_wiki_link |
remove_wiki_link = remove_wiki_link, |
||
is_wikilink = is_wikilink, |
|||
set_selected_modules = set_selected_modules; |
|||
make_wikilink = make_wikilink, |
|||
z = z, |
|||
set_selected_modules = set_selected_modules, |
|||
strip_apostrophe_markup = strip_apostrophe_markup, |
|||
z = z, -- exported table |
|||
} |
} |