Content deleted Content added
Optimization (especially nested links) and closer to mediawiki handling of links |
Syncing sandbox code with main template (sync-template-sandbox.js) |
||
Line 8:
local function delinkReversePipeTrick(s)
if s:match("^%[%[|.*[|\n]")
return s
else
Line 16:
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 there are no colons, grab all of the text apart from the square brackets and the pipe.
s = s:match("%[%[(.*)|%]%]")
end▼
-- Next up, brackets and commas.
Line 32 ⟶ 37:
local function delinkWikilink(s)
local result = s
if nested ~= s then▼
return nested▼
▲ end
-- Deal with the reverse pipe trick.
if
return delinkReversePipeTrick(
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 = mw.uri.decode(titlearea, "PATH") -- decode percent-encoded entities. Leave underscores and plus signs.
▲ titlearea = mw.text.decode(titlearea, true) -- decode HTML entities.
▲ -- title area of the link, i.e. the part before any pipes.
local titlearea
if result:match("|") then -- Find if we're dealing with a piped link.
titlearea = result:match("^%[%[(.-)|.*%]%]")
▲ else
titlearea = result:match("^%[%[(.-)%]%]")
▲ end
-- Check for bad characters.
▲ return s
end
-- Check for categories, interwikis, and files.
local colonprefix =
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
end
-- Remove the colon if the link is using the [[Help:Colon trick]].
if
end
-- Deal with links using the [[Help:Pipe trick]].
if
return delinkPipeTrick(
end
--
if
result = result:match("^%[%[.-|(.+)%]%]")
-- Remove new lines from the display of multiline piped links,
-- where the pipe is before the first new line.
result = result:match("^%[%[(.-)%]%]")
end
return
end
Line 109 ⟶ 103:
local url_prefix
for i,v in ipairs(valid_url_prefixes) do
if
url_prefix = v
break
Line 117 ⟶ 111:
-- Get display text
if not url_prefix then
return s
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('^.-(["<>
s =
local s_decoded = mw.text.decode(s, true)
if
return s
else
Line 132 ⟶ 125:
end
local function delinkLinkClass(s, pattern, delinkFunction)
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
-- Iterate over the text string, and replace any matched text. using the
-- delink function. We need to iterate character by character rather
-- than just use gsub, otherwise nested links aren't detected properly.
local result = ""
while s ~= '' do
-- Replace text using one iteration of gsub.
s = mw.ustring.gsub(s, pattern, delinkFunction, 1)
-- Append the left-most character to the result string.
s = mw.ustring.sub(s, 2, -1)
end
end
function p._delink(args)
Line 138 ⟶ 151:
-- 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.
end
if not (args.comments == "no") then
Line 144 ⟶ 157:
end
if not (args.wikilinks == "no") then
text =
end
if not (args.urls == "no") then
text =
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 = text:gsub("[ \t]+", " ") -- Remove extra tabs and spaces.
▲ end
end
return text
|