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._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
return '[[' .. s:sub(3):gsub('%[%[.-%]%]$', p._delinkWikilink) end
local titleArea, display = linkText:match('^(.-)|(.*)$')
-- Process links with display areas. Pipe tricks aren't processed here, as we need to know more about the link title first.
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
-- Decode percent-encoded and HTML-encoded characters.
titleArea = uriDecode(titleArea, 'PATH')
titleArea = htmlDecode(titleArea, true)
-- Find the fragment, if any.
local titleAreaNoFragment, fragment = titleArea:match('^(.-)#(.*)$')
titleAreaNoFragment = titleAreaNoFragment or titleArea
-- Check for bad characters.
if
return s
end
-- Find the interwiki and the title. Actually, only the prefix before the first
-- comma is counted as the interwiki, so the "title" may contain another interwiki
-- prefix and/or a namespace name, but it's close enough for our purposes.
local interwiki, title = titleAreaNoFragment:match('^(.-):(.*)$') or ''
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
-- Deal with links using the [[Help:Pipe trick]].
if display == '' then
if fragment then
-- Fragments in a pipe trick are invalid, so return the input string.
return s
end
-- Pipe tricks don't display interwikis, so we only need the title text here.
-- We need to remove parentheses and commas. Parentheses have priority.
local pipeTrickText = title:match('^(.-) ?%(.-%)$')
if pipeTrickText then
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 titleArea
end
|