Editing Module:Citation/CS1/Utilities

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 13: Line 13:
 
]]
 
]]
  
local cfg; -- table of tables imported from selected Module:Citation/CS1/Configuration
+
local cfg; -- table of tables imported from slected Module:Citation/CS1/Configuration
  
  
Line 19: Line 19:
  
 
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 62:
  
 
]]
 
]]
 
 
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 95: Line 73:
  
 
]]
 
]]
 
 
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 103: Line 80:
 
 
 
if error_state == nil then
 
if error_state == nil then
error( cfg.messages['undefined_error'] ); -- because missing error handler in Module:Citation/CS1/Configuration
+
error( cfg.messages['undefined_error'] );
 
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 109: Line 86:
 
 
 
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']),
 
')'
 
});
 
 
 
-- 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;
 
z.error_ids[ error_id ] = true;
Line 306: Line 266:
  
 
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 319: Line 273:
 
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]]
 
 
L, D = str:match ('%[%[([^|]+)|([^%]]+)%]%]'); -- get L and D from [[L|D]]
 
 
if not is_set (D) then -- if no separate link
 
D = str:match ('%[%[([^%]]*)|*%]%]'); -- get D from [[D]]
 
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
 
end
  
Line 368: Line 284:
 
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
  
Line 381: Line 296:
 
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,
 
set_selected_modules = set_selected_modules,
 
 
z = z,
 
z = z,
 
}
 
}

Please note that all contributions to Nonbinary Wiki are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) (see Nonbinary Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)

Template used on this page: