Module:Delink/sandbox: Difference between revisions

Content deleted Content added
a few tweaks
attempt to simplify the p._delinkWikilink function
Line 3:
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
 
-- Often-used functions and variables
local htmlDecode = mw.text.decode
local uriDecode = mw.uri.decode
local isKnownLanguageTag = mw.language.isKnownLanguageTag
local namespaces = mw.site.namespaces
 
p = {}
Line 15 ⟶ 21:
end
return yesno(val)
end
 
function p._delinkReversePipeTrick(s)
if s:find('^%[%[|.*[|\n]') or s == '[[|]]' then -- Check for newlines or multiple pipes.
return s
else
return s:match('%[%[|(.*)%]%]')
end
end
 
function p._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:find('%(.-%)$') then -- Brackets trump commas.
s = s:match('(.-) ?%(.-%)$')
elseif s:find(',') then -- If there are no brackets, display only the text before the first comma.
s = s:match('(.-),.*$')
end
return s
end
 
function p._delinkWikilink(s)
-- s is a string starting with '[[' and ending with ']]'. It does not contain any other ']]' strings.
local linkText = s:sub(3, -3)
 
-- Deal with nested links
localif nestedlinkText:find('%[%[') =then
return '[[' .. s:sub(3):gsub('%[%[.-%]%]$', p._delinkWikilink)
if nested ~= s then
return nested
end
 
local titleArea, display = linkText:match('^(.-)|(.*)$')
-- Deal with the reverse pipe trick.
 
if s:find('^%[%[|') then
-- Process links with display areas. Pipe tricks aren't processed here, as we need to know more about the link title first.
return p._delinkReversePipeTrick(s)
if display then
display = htmlDecode(display, true) -- decode HTML entities.
if titleArea == '' then
-- We are dealing with a reverse pipe trick.
if display:find('[|\n]') or s == '[[|]]' then
-- The link is invalid.
return s
else
return display
end
elseif display ~= '' then
-- We are dealing with a normal piped link.
return display
end
end
 
titleArea = titleArea or linkText
local decoded = mw.uri.decode(s, 'PATH') -- decode percent-encoded entities. Leave underscores and plus signs.
decoded = mw.text.decode(decoded, true) -- decode HTML entities.
 
-- Decode percent-encoded and HTML-encoded characters.
-- Check for bad titles. To do this we need to find the
titleArea = uriDecode(titleArea, 'PATH')
-- title area of the link, i.e. the part before any pipes.
titleArea = htmlDecode(titleArea, true)
local titlearea, display = decoded:match('^%[%[([^|%]]*)|?(.*)%]%]$')
 
local temptitlearea, fragment = titlearea:match('^(.-)#(.*)$')
-- Find the fragment, if any.
titlearea = temptitlearea or titlearea
local titleAreaNoFragment, fragment = titleArea:match('^(.-)#(.*)$')
titleAreaNoFragment = titleAreaNoFragment or titleArea
 
-- Check for bad characters.
if titleareatitleAreaNoFragment:find('[%[%]<>{}%%%c\n]') then
return s
end
 
-- Find the interwiki and the title. Actually, only the prefix before the first
-- Check for categories, interwikis, and files.
-- comma is counted as the interwiki, so the "title" may contain another interwiki
local colonprefix = titlearea:match('^(.-):') or '' -- Get the text before the first colon.
-- prefix and/or a namespace name, but it's close enough for our purposes.
local ns = mw.site.namespaces[colonprefix] -- see if this is a known namespace
local interwiki, title = titleAreaNoFragment:match('^(.-):(.*)$') or ''
if mw.language.isKnownLanguageTag(colonprefix)
title = title or titleAreaNoFragment
 
-- Check for unescaped categories, interwikis, and files. If any are found,
-- return the blank string, as nothing would be displayed.
local ns = namespaces[interwiki]
if isKnownLanguageTag(interwiki)
or ns and (ns.id == 6 or ns.id == 14)
then
Line 79 ⟶ 83:
 
-- Remove the colon if the link is using the [[Help:Colon trick]].
titleArea = titleArea:match('^:(.*)$') or titleArea
if titlearea:sub(1, 1) == ':' then
titlearea = titlearea:sub(2)
end
 
-- Deal with links using the [[Help:Pipe trick]].
if display == '' then
if fragment then
return p._delinkPipeTrick(titlearea)
-- Fragments in a pipe trick are invalid, so return the input string.
end
return s
 
end
-- Find the display area of the wikilink
-- Pipe tricks don't display interwikis, so we only need the title text here.
if not display then -- Find if we're dealing with a piped link.
-- We need to remove parentheses and commas. Parentheses have priority.
-- Remove new lines from the display of multiline piped links,
local pipeTrickText = title:match('^(.-) ?%(.-%)$')
-- where the pipe is before the first new line.
if pipeTrickText then
titlearea = titlearea:gsub('\n', '')
return pipeTrickText
else
-- If there are no parentheses, display only the text before the first comma.
pipeTrickText = title:match('(.-),.*$') or title
return pipeTrickText
end
end
 
-- If we haven't returned any text yet, display the title area.
return display or titlearea
return titleArea
end