Module:Delink/sandbox: Difference between revisions

Content deleted Content added
Undid revision 1085270311 by Desb42 (talk)
Synced (rip one testcase but whatever), dramatically improved efficiency of one of the regex patterns
 
(3 intermediate revisions by 2 users not shown)
Line 1:
-- This module de-links most wikitext.
 
require("Module:No globalsstrict")
 
local p = {}
Line 29:
s = s:match("(.-) ?%(.-%)$")
elseif s:match(",") then -- If there are no brackets, display only the text before the first comma.
s = s:match("(.-)[^,.]*$")
end
return s
Line 156:
error("Attempt to de-link non-string input.", 2)
end
-- if type(pattern) ~= "string" or stringmw.ustring.sub(pattern, 1, 1) ~= "^" then
-- unneccessary check
-- error('Invalid pattern detected. Patterns must begin with "^".', 2)
-- if type(pattern) ~= "string" or string.sub(pattern, 1, 1) ~= "^" then
-- end
-- error('Invalid pattern detected. Patterns must begin with "^".', 2)
-- end
-- Iterate over the text string, and replace any matched text. using the
-- delink function. We need to iterate character by character rather
-- than just use gsub, otherwise nested links aren't detected properly.
local result = ""
while sbtext ~= "" do
local sb = text:find('%[')
while sb do
result = result .. mw.ustring.sub(text, 1, sb - 1)
text = mw.ustring.sub(text, sb, -1)
-- Replace text using one iteration of gsub.
text = mw.ustring.gsub(text, pattern, delinkFunction, 1)
-- Append the left-most character to the result string.
sb = text:find('%[')
result = result .. mw.ustring.sub(text, 1, sb - 1)
text = mw.ustring.sub(text, sb2, -1)
end
result = result .. text
return result
end