-- This module de-links most wikitext.
require('Module:No globals')
local p = {}
local getArgs
local function delinkReversePipeTrick(s)
if s:match("^%[%[|.*[|\n]") or s == "[[|]]" then -- Check for newlines or multiple pipes.
return s
else
return s:match("%[%[|(.*)%]%]")
end
end
local function delinkPipeTrick(s)
-- s the tile area, without bracket nor pipe
-- We need to deal with colons, brackets, and commas, per [[Help:Pipe trick]].
-- First, remove the text before the first colon, if any.
s = s:gsub("^(.-:)", "")
-- Next up, brackets and commas.
if s:match("%(.-%)$") then -- Brackets trump commas.
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(s)
-- s is a string starting with "[[" and ending with "]]". It does not contain any other "]]" strings.
-- Deal with nested links
local nested = "[[" .. s:sub(3):gsub("%[%[.-%]%]$", delinkWikilink)
if nested ~= s then
return nested
end
-- Deal with the reverse pipe trick.
if s:match("^%[%[|") then
return delinkReversePipeTrick(s)
end
-- Check for bad titles. To do this we need to match the
-- title area of the link, i.e. the part before any pipes.
local titlearea, display = s:match("^%[%[(.-)|(.*)%]%]$")
titlearea = titlearea or s:sub(3, -3)
titlearea = mw.uri.decode(titlearea, "PATH") -- decode percent-encoded entities. Leave underscores and plus signs.
titlearea = mw.text.decode(titlearea, true) -- decode HTML entities.
local temptitlearea, fragment = titlearea:match("^(.-)#(.*)$")
temptitlearea = temptitlearea or titlearea
fragment = fragment or ""
-- Check for bad characters.
if not mw.ustring.isutf8(titlearea)
or temptitlearea:match("[%[%]<>|{}%z%c\n]")
or temptitlearea:match("%%%x%x")
or temptitlearea:match("&..-;")
or fragment:match("[%[%]{}%c\n]")
then
return s
end
-- Check for categories, interwikis, and files.
local colonprefix = titlearea:match("^(.-):") 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)
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 titlearea:sub(1, 1) == ":" then
titlearea = titlearea:sub(2)
end
-- Deal with links using the [[Help:Pipe trick]].
if display == "" then
return delinkPipeTrick(titlearea )
end
-- match the display area of the wikilink
if not display then -- match if we're dealing with a piped link.
-- Remove new lines from the display of multiline piped links,
-- where the pipe is before the first new line.
titlearea = titlearea:gsub("\n", "")
end
return display or titlearea
end
local function delinkURL(s)
-- Assume we have already delinked internal wikilinks, and that
-- we have been passed some text between two square brackets [foo].
-- If the text contains a line break it is not formatted as a URL, regardless of other content.
if s:match("\n") then
return s
end
-- Check if the text has a valid URL prefix and at least one valid URL character.
local valid_url_prefixes = {"//", "http://", "https://", "ftp://", "gopher://", "mailto:", "news:", "irc://"}
local url_prefix
for i,v in ipairs(valid_url_prefixes) do
if s:find( '^%[' .. v ..'[^"%s].*%]' ) then
url_prefix = v
break
end
end
-- Get display text
if not url_prefix then
-- Deal with nested links or send back original string.
return "[" .. mw.ustring.gsub( mw.ustring.sub(s, 2), "%[.-%]", delinkURL )
end
s = s:match("^%[" .. url_prefix .. "(.*)%]") -- Grab all of the text after the URL prefix and before the final square bracket.
s = s:match('^.-(["<> [].*)') or "" -- Grab all of the text after the first URL separator character ("<> ).
s = s:match("^%s*(%S.*)$") or "" -- If the separating character was a space, trim it off.
local s_decoded = mw.text.decode(s, true)
if s_decoded:match("%c") then
return s
else
return s_decoded
end
end
function p._delink(args)
local text = args[1] or ""
if args.refs == "yes" then
-- Remove any [[Help:Strip markers]] representing ref tags. In most situations
-- this is not a good idea - only use it if you know what you are doing!
text = mw.text.unstrip(text)
end
if not (args.comments == "no") then
text = text:gsub("<!%-%-.-%-%->", "") -- Remove html comments.
end
if not (args.wikilinks == "no") then
text = text:gsub("%[%[.-%]%]", delinkWikilink) -- De-link wikilinks.
end
if not (args.urls == "no") then
text = text:gsub("%[.-%]", 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.
text = text:gsub("([^\n \t][ \t]*)\n([ \t]*[^\n \t])", "%1 %2")
--text = text:gsub("\n\t+", "\n") -- Remove leading tab.
text = text:gsub("[ \t]+", " ") -- Remove extra tabs and spaces.
end
if not (args.nowiki == "no") then
text = mw.text.unstripNoWiki(text)
end
return text
end
function p.delink(frame)
if not getArgs then
getArgs = require('Module:Arguments').getArgs
end
return p._delink(getArgs(frame, {wrappers = 'Template:Delink'}))
end
return p