Module:Delink: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
split the search algorithm out into its own function, add data type checks
(use pcall and mw.title.new to check for valid titles)
(split the search algorithm out into its own function, add data type checks)
Line 115:
end
 
local function _delinkdelinkLinkClass(argss, pattern, delinkFunction)
localif textnot type(s) = args[1] or= "string" then
error("Attempt to de-link non-string input.", 2)
local result = ""
-- Delink wikilinks. We need to search character by character rather
-- than just use gsub, otherwise nested links aren't detected properly.
while mw.ustring.len(text) > 0 do
text = mw.ustring.gsub(text, "^%[%[.-%]%]", delinkWikilink, 1)
result = result .. mw.ustring.sub(text, 1, 1)
text = mw.ustring.sub(text, 2, -1)
end
if not ( type(pattern) == "string" and mw.ustring.sub(pattern, 1, 1) == "^" ) then
error('Invalid pattern detected. Patterns must begin with "^".', 2)
-- Reset the variables.
text = resultend
-- Iterate over the text string, and replace any matched text. using the
result = ""
-- Delinkdelink wikilinksfunction. We need to searchiterate character by character rather
-- than just use gsub, otherwise nested links aren't detected properly.
-- Delink URLs.
local result = ""
while mw.ustring.len(text) > 0 do
text =while mw.ustring.gsublen(text,s) "^%[.-%]",> delinkURL,0 1)do
-- Replace text using one iteration of gsub.
result = result .. mw.ustring.sub(text, 1, 1)
texts = mw.ustring.subgsub(texts, 2pattern, delinkFunction, -1)
-- Append the left-most character to the result string.
result = result .. mw.ustring.sub(texts, 1, 1)
while s = mw.ustring.lensub(text)s, >2, 0 do-1)
end
-- Remove extra whitespace.
result = mw.ustring.gsub(result, "%s+", " ")
return result
end
 
local function _delink(args)
local text = args[1] or ""
text = mw.ustring.gsubdelinkLinkClass(text, "^%[%[.-%]%]", delinkWikilink,) 1)-- De-link wikilinks.
text = delinkLinkClass(text, "^%[.-%]", delinkURL) -- De-link URLs.
text = mw.ustring.subgsub(text, 2"%s+", -1" ") -- Remove extra whitespace.
return text
end
 
Line 158 ⟶ 159:
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