Module:Delink/sandbox: Difference between revisions

Content deleted Content added
m Dummy edit. That came out wrong - that should have been "start making this a bit more sane".
use string library functions, and use single quotes rather than double
Line 18:
 
function p._delinkReversePipeTrick(s)
if mw.ustring.matchs:find(s, "'^%[%[|.*[|\n]"') or s == "'[[|]]"' then -- Check for newlines or multiple pipes.
return s
else
return mw.ustring.s:match(s, "'%[%[|(.*)%]%]"')
end
end
Line 30:
 
-- First, remove the text before the first colon, if any.
s = mw.ustring.s:gsub(s, "'^(.-:)"', ""'')
 
-- Next up, brackets and commas.
if mw.ustring.matchs:find(s, "'%(.-%)$"') then -- Brackets trump commas.
s = mw.ustring.s:match(s, "'(.-) ?%(.-%)$"')
elseif mw.ustring.matchs:find(s, "',"') then -- If there are no brackets, display only the text before the first comma.
s = mw.ustring.s:match(s, "'(.-),.*$"')
end
return s
Line 42:
 
function p._delinkWikilink(s)
-- s should be a string starting bywith "'[["' and ending bywith "']]"'. It should not contain other "']]"'
 
-- Deal with nested links
local nested = "'[["' .. mw.ustring.gsub( mw.ustring.s:sub(s, 3), ":gsub('%[%[.-%]%]"', p._delinkWikilink)
local nested
nested = "[[" .. mw.ustring.gsub( mw.ustring.sub(s, 3), "%[%[.-%]%]", p._delinkWikilink)
if nested ~= s then
return nested
Line 52 ⟶ 51:
 
-- Deal with the reverse pipe trick.
if mw.ustring.match(s, ":find('^%[%[|"') then
return p._delinkReversePipeTrick(s)
end
 
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.
 
-- Check for bad titles. To do this we need to find the
-- title area of the link, i.e. the part before any pipes.
local titlearea, display = mw.ustring.decoded:match(decoded, "'^%[%[([^|%]]*)|?(.*)%]%]"')
 
-- Check for bad characters.
if mw.ustring.match(titlearea, ":find('[%[%]<>{}%%%c\n]"') then
return s
end
 
-- Check for categories, interwikis, and files.
local colonprefix = mw.ustring.titlearea:match(titlearea, "'^(.-):"') 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.canonicalNameid == "File"6 or ns.canonicalNameid == "Category" 14) ) then
then
return ""''
end
 
-- Remove the colon if the link is using the [[Help:Colon trick]].
if mw.ustring.titlearea:sub(titlearea, 1, 1) == "':"' then
titlearea = mw.ustring.titlearea:sub(titlearea, 2)
end
 
-- Deal with links using the [[Help:Pipe trick]].
if display == ""'' then
return p._delinkPipeTrick(titlearea )
end
 
Line 90:
-- Remove new lines from the display of multiline piped links,
-- where the pipe is before the first new line.
titlearea = mw.ustring.titlearea:gsub(titlearea, "'\n"', ""'')
end
 
Line 101:
 
-- If the text contains a line break it is not formatted as a URL, regardless of other content.
if mw.ustring.match(s, ":find('\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 mw.ustring.match(s, :find('^%[' .. v ..'[^"%s].*%]' ) then
url_prefix = v
break
Line 118:
if not url_prefix then
-- Deal with nested links or send back original string.
return "'["' .. mw.ustring.gsub( mw.ustring.s:sub(s, 2), ":gsub('%[.-%]"', p._delinkURL )
end
s = mw.ustring.s:match(s, "'^%["' .. url_prefix .. "'(.*)%]"') -- Grab all of the text after the URL prefix and before the final square bracket.
s = mw.ustring.s:match(s, '^.-(["<> [].*)') or ""'' -- Grab all of the text after the first URL separator character ("<> ).
s = mw.ustring.s:match(s, "'^%s*(%S.*)$"') or ""'' -- If the separating character was a space, trim it off.
 
local s_decoded = mw.text.decode(s, true)
if mw.ustring.match(s_decoded, ":find('%c"') then
return s
else