Module:Delink: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Try parsing wikilinks and URLs on the same pass
(split the search algorithm out into its own function, add data type checks)
(Try parsing wikilinks and URLs on the same pass)
Line 115:
end
 
local function delinkLinkClass_delink(s, pattern, delinkFunctionargs)
iflocal not type(s)text == args[1] or "string" then
-- Iterate over the text string, and replace any matched text. usingwith the
error("Attempt to de-link non-string input.", 2)
end
if not ( type(pattern) == "string" and mw.ustring.sub(pattern, 1, 1) == "^" ) then
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 mw.ustring.len(stext) > 0 do
-- Replace text using one iteration of gsub.
stext = mw.ustring.gsub(stext, pattern"^%[%[.-%]%]", delinkFunctiondelinkWikilink, 1) -- De-link wikilinks.
text = delinkLinkClassmw.ustring.gsub(text, "^%[.-%]", delinkURL, 1) -- De-link URLs.
-- Append the left-most character to the result string.
result = result .. mw.ustring.sub(stext, 1, 1)
stext = mw.ustring.sub(stext, 2, -1)
end
textresult = mw.ustring.gsub(textresult, "%s+", " ") -- Remove extra whitespace.
return result
end
 
local function _delink(args)
local text = args[1] or ""
text = delinkLinkClass(text, "^%[%[.-%]%]", delinkWikilink) -- De-link wikilinks.
text = delinkLinkClass(text, "^%[.-%]", delinkURL) -- De-link URLs.
text = mw.ustring.gsub(text, "%s+", " ") -- Remove extra whitespace.
return text
end
 
Line 159 ⟶ 148:
args = frame
end
return _delink(args)
end
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu