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(
local p = {}
Line 10:
if s:match("^%[%[|.*[|\n]") then -- Check for newlines or multiple pipes.
return s
return s:match("%[%[|(.*)%]%]")▼
end
end
local function delinkPipeTrick(s)
-- 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 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
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
local ns = mw.site.namespaces[
if mw.language.isKnownLanguageTag(
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
if mw.ustring.match(s, '^%[' .. v ..'[^"%s].*%]' ) then
url_prefix = v
Line 146:
if mw.ustring.match(s_decoded, "%c") then
return s
return s_decoded▼
end
end
local function delinkLinkClass(
if
error("Attempt to de-link non-string input.", 2)
end
if
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
-- Replace text using one iteration of gsub.
-- Append the left-most character to the result string.
result = result .. mw.ustring.sub(
end
return result
Line 179 ⟶ 180:
text = mw.ustring.gsub(text, "UNIQ%w*%-ref%-%d*%-QINU", "")
end
if
text = text:gsub("<!%-%-.-%-%->", "") -- Remove html comments.
end
if
text = delinkLinkClass(text, "^%[%[.-%]%]",
elseif args.wikilinks == "target" then -- De-link wikilinks and return the target portions of the wikilink. text = delinkLinkClass(text, "^%[%[.-%]%]", getDelinkedTarget)
end
if
text = delinkLinkClass(text, "^%[.-%]", delinkURL) -- De-link URLs.
end
if
-- 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.
|