Module:Delink: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(move the colon trick processing to earlier in the chain)
(deal with most bad links)
Line 2: Line 2:


p = {}
p = {}

local function delinkReversePipeTrick(s)
return mw.ustring.match(s, "%[%[|(.*)%]%]")
end


local function delinkPipeTrick(s)
local function delinkPipeTrick(s)
local linkarea, display = "", ""
local linkarea, display = "", ""
-- Deal with the left-hand pipe trick, an easy case.
if mw.ustring.match(s, "%[%[|") then
return mw.ustring.match(s, "%[%[|(.*)%]%]")
end
-- If the link isn't a left-hand pipe trick, assume it's a right-hand pipe trick.
-- We need to deal with colons, brackets, and commas, per [[Help:Pipe trick]].
-- We need to deal with colons, brackets, and commas, per [[Help:Pipe trick]].
Line 32: Line 29:


local function delinkOne(s)
local function delinkOne(s)
-- First, check for categories, interwikis, and files.
-- Deal with the reverse pipe trick, an easy case.
if mw.ustring.match(s, "%[%[|") then
return delinkReversePipeTrick(s)
end
-- 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
if mw.ustring.match(s, "|") then -- Find if we're dealing with a piped link.
titlearea= mw.ustring.match(s, "^%[%[(.-)|.*%]%]")
else
titlearea = mw.ustring.match(s, "^%[%[(.-)%]%]")
end
-- Clear the title of the fragment (the section link).
if not mw.title.makeTitle("", titlearea) then
return s -- If it's not a valid link, return the whole string.
end
-- Check for categories, interwikis, and files.
local colonprefix = mw.ustring.match(s, "%[%[(.-):.*%]%]") or "" -- Get the text before the first colon.
local colonprefix = mw.ustring.match(s, "%[%[(.-):.*%]%]") or "" -- Get the text before the first colon.
if mw.language.isKnownLanguageTag(colonprefix)
if mw.language.isKnownLanguageTag(colonprefix)
Line 46: Line 61:
end
end
-- Deal with links using the [[Help:Pipe trick]].
if mw.ustring.match(s, "[^|].*|%]%]") or mw.ustring.match(s, "%[%[|") then -- Weed out the pipe tricks first.
if mw.ustring.match(s, "[^|].*|%]%]") or mw.ustring.match(s, "%[%[|") then
return delinkPipeTrick(s)
return delinkPipeTrick(s)
end
end
-- Find the link area and display area of the wikilink
local linkarea, display
-- Find the display area of the wikilink
local display
if mw.ustring.match(s, "|") then -- Find if we're dealing with a piped link.
if mw.ustring.match(s, "|") then -- Find if we're dealing with a piped link.
linkarea, display = mw.ustring.match(s, "^%[%[(.-)|(.+)%]%]")
display = mw.ustring.match(s, "^%[%[.-|(.+)%]%]")
else
else
display = mw.ustring.match(s, "^%[%[(.-)%]%]")
-- If the link isn't piped, the display area and the link area are the same.
linkarea = mw.ustring.match(s, "^%[%[(.-)%]%]")
display = linkarea
end
-- Check for bad links
if mw.ustring.match(linkarea, "%[") or mw.ustring.match(linkarea, "%]") then
error("Bad link detected. Bad links are not yet supported.")
end
end

return display
return display
end
end