Module:Delink/sandbox: Difference between revisions

Content deleted Content added
consistent spacing
Synced (rip one testcase but whatever), dramatically improved efficiency of one of the regex patterns
 
(8 intermediate revisions by 3 users not shown)
Line 1:
-- This module de-links most wikitext.
 
require('Module:No globals'"strict")
 
local p = {}
Line 10:
if s:match("^%[%[|.*[|\n]") then -- Check for newlines or multiple pipes.
return s
else
return s:match("%[%[|(.*)%]%]")
end
return s:match("%[%[|(.*)%]%]")
 
end
 
local function delinkPipeTrick(s)
local linkarea, display = "", ""
-- We need to deal with colons, brackets, and commas, per [[Help:Pipe trick]].
-- First, remove the text before the first colon, if any.
if s:match(":") then
Line 31 ⟶ 29:
s = s:match("(.-) ?%(.-%)$")
elseif s:match(",") then -- If there are no brackets, display only the text before the first comma.
s = s:match("(.-)[^,.]*$")
end
return s
end
 
local function delinkWikilink_tgt(s) -- returnReturn wikilink target |wikilinks=target
local function getDelinkedTarget(s)
local result = s
-- Deal with the reverse pipe trick.
Line 54 ⟶ 53:
target_area = result:match("^%[%[(.-)%]%]")
end
 
-- Check for bad characters.
if mw.ustring.match(target_area, "[%[%]<>{}%%%c\n]") and mw.ustring.match(target_area, "[%[%]<>{}%%%c\n]") ~= "?" then
return s
end
Line 62:
end
 
local function delinkWikilinkgetDelinkedLabel(s)
local result = s
-- Deal with the reverse pipe trick.
Line 68:
return delinkReversePipeTrick(result)
end
 
result = mw.uri.decode(result, "PATH") -- decode percent-encoded entities. Leave underscores and plus signs.
result = mw.text.decode(result, true) -- decode HTML entities.
 
-- Check for bad titles. To do this we need to find the
-- title area of the link, i.e. the part before any pipes.
Line 80:
target_area = result:match("^%[%[(.-)%]%]")
end
 
-- Check for bad characters.
if mw.ustring.match(target_area, "[%[%]<>{}%%%c\n]") and mw.ustring.match(target_area, "[%[%]<>{}%%%c\n]") ~= "?" then
return s
end
 
-- Check for categories, interwikis, and files.
local colonprefixcolon_prefix = result:match("%[%[(.-):.*%]%]") or "" -- Get the text before the first colon.
local ns = mw.site.namespaces[colonprefixcolon_prefix] -- see if this is a known namespace
if mw.language.isKnownLanguageTag(colonprefixcolon_prefix) or (ns and (ns.canonicalName == "File" or ns.canonicalName == "Category")) then
or ( ns and ( ns.canonicalName == "File" or ns.canonicalName == "Category" ) ) then
return ""
end
 
-- Remove the colon if the link is using the [[Help:Colon trick]].
if result:match("%[%[:") then
result = "[[" .. result:match("%[%[:(.*%]%])")
end
 
-- Deal with links using the [[Help:Pipe trick]].
if mw.ustring.match(result, "^%[%[[^|]*|%]%]") then
Line 128:
local valid_url_prefixes = {"//", "http://", "https://", "ftp://", "gopher://", "mailto:", "news:", "irc://"}
local url_prefix
for i_ ,v in ipairs(valid_url_prefixes) do
if mw.ustring.match(s, '^%[' .. v ..'[^"%s].*%]' ) then
url_prefix = v
Line 146:
if mw.ustring.match(s_decoded, "%c") then
return s
else
return s_decoded
end
 
return s_decoded
 
end
 
local function delinkLinkClass(stext, pattern, delinkFunction)
if not type(stext) =~= "string" then
error("Attempt to de-link non-string input.", 2)
end
if not ( type(pattern) =~= "string" andor mw.ustring.sub(pattern, 1, 1) =~= "^" ) then
error('Invalid pattern detected. Patterns must begin with "^".', 2)
end
Line 162 ⟶ 163:
-- than just use gsub, otherwise nested links aren't detected properly.
local result = ""
while stext ~= ''"" do
-- Replace text using one iteration of gsub.
stext = mw.ustring.gsub(stext, pattern, delinkFunction, 1)
-- 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
return result
Line 179 ⟶ 180:
text = mw.ustring.gsub(text, "UNIQ%w*%-ref%-%d*%-QINU", "")
end
if not (args.comments =~= "no") then
text = text:gsub("<!%-%-.-%-%->", "") -- Remove html comments.
end
 
if not (args.wikilinks =~= "no") and 'target'args.wikilinks ~= args.wikilinks"target" then
text = delinkLinkClass(text, "^%[%[.-%]%]", delinkWikilink) -- De-link wikilinks and return the label portion of the wikilink.
elseif 'target' == args.wikilinks then
text = delinkLinkClass(text, "^%[%[.-%]%]", delinkWikilink_tgtgetDelinkedLabel)
elseif args.wikilinks == "target" then
-- De-link wikilinks and return the target portions of the wikilink.
text = delinkLinkClass(text, "^%[%[.-%]%]", getDelinkedTarget)
end
if not (args.urls =~= "no") then
text = delinkLinkClass(text, "^%[.-%]", delinkURL) -- De-link URLs.
end
if not (args.whitespace =~= "no") then
-- Replace single new lines with a single space, but leave double new lines
-- and new lines only containing spaces or tabs before a second new line.