Module:Delink/sandbox: Difference between revisions

Content deleted Content added
performance enhancement
Synced (rip one testcase but whatever), dramatically improved efficiency of one of the regex patterns
 
(One intermediate revision by one other user 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 94:
 
-- Remove the colon if the link is using the [[Help:Colon trick]].
-- if result:match("%[%[:") then
-- result = "[[" .. result:match("%[%[:(.*%]%])")
-- end
if result:sub(3,3) == ':' then
result = "[[" .. result:sub(4,-1)
end
 
Line 159 ⟶ 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('%[')
local n
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, n = mw.ustring.gsub(text, pattern, delinkFunction, 1)
-- Append the left-most character to the result string.
sb = text:find('%[', 2)
result = "[["result .. result:mw.ustring.sub(4text,- 1, 1)
resulttext = result .. mw.ustring.sub(text, 12, sb - 1)
end
result = result .. text
return result
end
Line 190 ⟶ 184:
end
 
if args.wikilinks ~= "no" and args.wikilinks ~= "target" then
if text:match("%[") then -- does the text contain a square bracket?
if-- De-link args.wikilinks ~=and "no"return andthe args.wikilinkslabel ~=portion "target"of thenthe wikilink.
text = delinkLinkClass(text, "^%[%[.-%]%]", getDelinkedLabel)
-- De-link wikilinks and return the label portion of the wikilink.
if elseif args.urlswikilinks ~== "notarget" then
text = delinkLinkClass(text, "^%[%[.-%]%]", getDelinkedLabel)
elseif-- De-link args.wikilinks ==and return the "target" thenportions of the wikilink.
text = delinkLinkClass(text, "^%[%[.-%]%]", getDelinkedTarget)
-- De-link wikilinks and return the target portions of the wikilink.
-- end
text = delinkLinkClass(text, "^%[%[.-%]%]", getDelinkedTarget)
if args.urls ~= "no" then
end
text = delinkLinkClass(text, "^%[.-%]", delinkURL) -- De-link URLs.
if args.urls ~= "no" then
text = delinkLinkClass(text, "^%[.-%]", delinkURL) -- De-link URLs.
end
end
 
if args.whitespace ~= "no" then
-- Replace single new lines with a single space, but leave double new lines