Module:Delink: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(Try parsing wikilinks and URLs on the same pass) |
m (Undid revision 548632488 by Mr. Stradivarius (talk) nope, doesn't quite work) |
||
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 |
|||
if not ( type(pattern) == "string" and mw.ustring.sub(pattern, 1, 1) == "^" ) then |
|||
error('Invalid pattern detected. Patterns must begin with "^".', 2) |
|||
end |
|||
⚫ | |||
-- delink function. We need to iterate character by character rather |
-- delink function. We need to iterate character by character rather |
||
-- than just use gsub, otherwise nested links aren't detected properly. |
-- than just use gsub, otherwise nested links aren't detected properly. |
||
local result = "" |
local result = "" |
||
while mw.ustring.len( |
while mw.ustring.len(s) > 0 do |
||
-- Replace text using one iteration of gsub. |
-- Replace text using one iteration of gsub. |
||
s = mw.ustring.gsub(s, pattern, delinkFunction, 1) |
|||
⚫ | |||
-- Append the left-most character to the result string. |
-- Append the left-most character to the result string. |
||
result = result .. mw.ustring.sub( |
result = result .. mw.ustring.sub(s, 1, 1) |
||
s = mw.ustring.sub(s, 2, -1) |
|||
end |
end |
||
⚫ | |||
return result |
return result |
||
end |
|||
local function _delink(args) |
|||
local text = args[1] or "" |
|||
text = delinkLinkClass(text, "^%[%[.-%]%]", delinkWikilink) -- De-link wikilinks. |
|||
⚫ | |||
⚫ | |||
return text |
|||
end |
end |
||
Line 148: | Line 159: | ||
args = frame |
args = frame |
||
end |
end |
||
return _delink(args) |
return _delink(args) |
||
end |
end |