Module:Delink: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
since mw.ustring is 30x slower than string, only use it when necessary
(expose _delink)
(since mw.ustring is 30x slower than string, only use it when necessary)
Line 4:
 
local function delinkReversePipeTrick(s)
if mw.ustring.s:match(s, "^%[%[|.*[|\n]") then -- Check for newlines or multiple pipes.
return s
else
return mw.ustring.s:match(s, "%[%[|(.*)%]%]")
end
end
Line 16:
-- First, remove the text before the first colon, if any.
if mw.ustring.s:match(s, ":") then
s = mw.ustring.s:match(s, "%[%[.-:(.*)|%]%]")
-- If there are no colons, grab all of the text apart from the square brackets and the pipe.
else
s = mw.ustring.s:match(s, "%[%[(.*)|%]%]")
end
-- Next up, brackets and commas.
if mw.ustring.s:match(s, "%(.-%)$") then -- Brackets trump commas.
s = mw.ustring.s:match(s, "(.-) ?%(.-%)$")
elseif mw.ustring.s:match(s, ",") then -- If there are no brackets, display only the text before the first comma.
s = mw.ustring.s:match(s, "(.-),.*$")
end
return s
Line 35:
local result = s
-- Deal with the reverse pipe trick.
if mw.ustring.result:match(result, "%[%[|") then
return delinkReversePipeTrick(result)
end
Line 45:
-- title area of the link, i.e. the part before any pipes.
local titlearea
if mw.ustring.result:match(result, "|") then -- Find if we're dealing with a piped link.
titlearea = mw.ustring.result:match(result, "^%[%[(.-)|.*%]%]")
else
titlearea = mw.ustring.result:match(result, "^%[%[(.-)%]%]")
end
-- Check for bad characters.
Line 56:
-- Check for categories, interwikis, and files.
local colonprefix = mw.ustring.result:match(result, "%[%[(.-):.*%]%]") or "" -- Get the text before the first colon.
local ns = mw.site.namespaces[colonprefix] -- see if this is a known namespace
if mw.language.isKnownLanguageTag(colonprefix)
Line 64:
-- Remove the colon if the link is using the [[Help:Colon trick]].
if mw.ustring.result:match(result, "%[%[:") then
result = "[[" .. mw.ustring.result:match(result, "%[%[:(.*%]%])")
end
Line 74:
-- Find the display area of the wikilink
if mw.ustring.result:match(result, "|") then -- Find if we're dealing with a piped link.
result = mw.ustring.result:match(result, "^%[%[.-|(.+)%]%]")
-- Remove new lines from the display of multiline piped links,
-- where the pipe is before the first new line.
result = mw.ustring.result:gsub(result, "\n", "")
else
result = mw.ustring.result:match(result, "^%[%[(.-)%]%]")
end
 
Line 91:
-- If the text contains a line break it is not formatted as a URL, regardless of other content.
if mw.ustring.s:match(s, "\n") then
return s
end
Line 109:
return s
end
s = mw.ustring.s:match(s, "^%[" .. url_prefix .. "(.*)%]") -- Grab all of the text after the URL prefix and before the final square bracket.
s = mw.ustring.s:match(s, '^.-(["<> ].*)') or "" -- Grab all of the text after the first URL separator character ("<> ).
s = mw.ustring.match(s, "^%s*(%S.*)$") or "" -- If the separating character was a space, trim it off.
Line 132:
-- than just use gsub, otherwise nested links aren't detected properly.
local result = ""
while mw.ustring.len(s) >~= 0'' do
-- Replace text using one iteration of gsub.
s = mw.ustring.gsub(s, pattern, delinkFunction, 1)
Line 150:
end
if not (args.comments == "no") then
text = mw.ustring.text:gsub(text, "<!%-%-.-%-%->", "") -- Remove html comments.
end
if not (args.wikilinks == "no") then
Line 162:
-- and new lines only containing spaces or tabs before a second new line.
text = mw.ustring.gsub(text, "([^\n \t][ \t]*)\n([ \t]*[^\n \t])", "%1 %2")
text = mw.ustring.text:gsub(text, "[ \t]+", " ") -- Remove extra tabs and spaces.
end
return text
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu