Module:Delink: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(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: | Line 115: | ||
end |
end |
||
local function |
local function delinkLinkClass(s, pattern, delinkFunction) |
||
if not type(s) == "string" then |
|||
error("Attempt to de-link non-string input.", 2) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
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. |
|||
end |
|||
-- Iterate over the text string, and replace any matched text. using the |
|||
result = "" |
|||
⚫ | |||
⚫ | |||
-- Delink URLs. |
|||
⚫ | |||
while mw.ustring.len(text) > 0 do |
|||
while mw.ustring.len(s) > 0 do |
|||
-- Replace text using one iteration of gsub. |
|||
result = result .. mw.ustring.sub(text, 1, 1) |
|||
s = mw.ustring.gsub(s, pattern, delinkFunction, 1) |
|||
-- Append the left-most character to the result string. |
|||
⚫ | |||
⚫ | |||
end |
end |
||
-- Remove extra whitespace. |
|||
result = mw.ustring.gsub(result, "%s+", " ") |
|||
return result |
return result |
||
end |
|||
local function _delink(args) |
|||
local text = args[1] or "" |
|||
⚫ | |||
text = delinkLinkClass(text, "^%[.-%]", delinkURL) -- De-link URLs. |
|||
⚫ | |||
return text |
|||
end |
end |
||
Line 158: | Line 159: | ||
args = frame |
args = frame |
||
end |
end |
||
return _delink(args) |
return _delink(args) |
||
end |
end |