Module:Citation/CS1: Difference between revisions

Better version of strip_apostrophe_markup()?
m>Trappist the monk
(Bug fix in strip_apostrophe_markup();)
m>Trappist the monk
(Better version of strip_apostrophe_markup()?)
Line 709: Line 709:
--[[--------------------------< S T R I P _ A P O S T R O P H E _ M A R K U P >--------------------------------
--[[--------------------------< 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
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
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.
markup a title have, as a result, provided valid markup. When they don't, some single apostrophes are left behind.
 
]]
]]
function strip_apostrophe_markup (argument)
function strip_apostrophe_markup (argument)
local pattern='';
local cap='';
local cap2='';
if not is_set (argument) then return argument; end
if not is_set (argument) then return argument; end
 
while true do -- look for and remove all 5-apostrophe (bold and italic) markup
while true do
if argument:match ("%'%'%'%'%'") then -- is there an instance of bold-italic?
if argument:match ("%'%'%'%'%'") then -- bold italic (5)
if argument:match ("%'%'%'%'%'.*%'%'%'%'%'") then -- 5, 5
argument=argument:gsub("%'%'%'%'%'", ""); -- remove all instances of it
pattern, cap = argument:match ("(%'%'%'%'%'(.*)%'%'%'%'%')");
elseif argument:match ("%'%'%'%'") then -- italic start and end without content (4)
cap2 = ""; -- set to empty string so we do only one replacement at end
argument=argument:gsub("%'%'%'%'", "");
elseif argument:match ("%'%'%'%'%'.*%'%'%'.*%'%'") then -- bold italic followed by italic (5, 3, 2)
elseif argument:match ("%'%'%'") then -- bold (3)
pattern, cap, cap2 = argument:match ("(%'%'%'%'%'(.*)%'%'%'(.*)%'%')");
argument=argument:gsub("%'%'%'", "");
elseif argument:match ("%'%'%'%'%'.*%'%'.*%'%'%'") then -- bold italic followed by bold (5, 2, 3)
elseif argument:match ("%'%'") then -- italic (2)
pattern, cap, cap2 = argument:match ("(%'%'%'%'%'(.*)%'%'(.*)%'%'%')");
argument=argument:gsub("%'%'", "");
elseif argument:match ("%'%'%'.*%'%'.*%'%'%'%'%'") then -- bold italic followed by italic (3, 2, 5)
pattern, cap, cap2 = argument:match ("(%'%'%'(.*)%'%'(.*)%'%'%'%'%')");
elseif argument:match ("%'%'%'.*%'%'%'%'%'.*%'%'") then -- bold italic followed by italic (3, 5, 2)
pattern, cap, cap2 = argument:match ("(%'%'%'(.*)%'%'%'%'%'(.*)%'%')");
elseif argument:match ("%'%'.*%'%'%'%'%'.*%'%'%'") then -- bold italic followed by italic (2, 5, 3)
pattern, cap, cap2 = argument:match ("(%'%'(.*)%'%'%'%'%'(.*)%'%'%')");
elseif argument:match ("%'%'.*%'%'%'.*%'%'%'%'%'") then -- italic followed by bold (2, 3, 5)
pattern, cap, cap2 = argument:match ("(%'%'(.*)%'%'%'(.*)%'%'%'%'%')");
end
cap = escape_lua_magic_chars (cap); -- replace lua magic characters
cap2 = escape_lua_magic_chars (cap2); -- replace lua magic characters
pattern = escape_lua_magic_chars (pattern); -- replace lua magic characters
argument=argument:gsub(pattern, cap..cap2); -- remove the markup
else
else
break; -- none or no more 5-apostrophe matches
break;
end
end
while true do -- look for and remove all 3-apostrophe (bold) markup
if argument:match ("%'%'%'.*%'%'%'") then -- is there an instance of bold?
pattern, cap = argument:match ("(%'%'%'(.*)%'%'%')")
cap = escape_lua_magic_chars (cap); -- replace lua magic characters
pattern = escape_lua_magic_chars (pattern); -- replace lua magic characters
argument=argument:gsub(pattern, cap); -- remove the markup
else
break; -- none or no more 3 matches
end
end
while true do -- look for and remove all 2-apostrophe (italic) markup
if argument:match ("%'%'.*%'%'") then -- is there an instance of italic?
pattern, cap = argument:match ("(%'%'(.*)%'%')")
cap = escape_lua_magic_chars (cap); -- replace lua magic characters
pattern = escape_lua_magic_chars (pattern); -- replace lua magic characters
argument=argument:gsub(pattern, cap); -- remove the markup
else
break; -- none or no more 2 matches
end
end
end
end
Anonymous user