Module:Delink: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content deleted Content added
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) |
|||
⚫ | |||
⚫ | |||
local function delinkPipeTrick(s) |
local function delinkPipeTrick(s) |
||
local linkarea, display = "", "" |
local linkarea, display = "", "" |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
-- 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) |
||
-- |
-- Deal with the reverse pipe trick, an easy case. |
||
⚫ | |||
return delinkReversePipeTrick(s) |
|||
⚫ | |||
⚫ | |||
-- 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. |
|||
⚫ | |||
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 |
||
⚫ | |||
if mw.ustring.match(s, "[^|].*|%]%]") or mw.ustring.match(s, "%[%[|") then |
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 |
|||
-- Find the display area of the wikilink |
|||
⚫ | |||
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. |
||
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. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
-- 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 |