Module:Delink: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content deleted Content added
tweak pipe trick spacing
deal with categories/interwikis and the colon trick
Line 3:
p = {}
 
local function delinkPipeTrick(s)
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
elseif mw.ustring.match(s, ":") then
s = mw.ustring.match(s, "%[%[.-:(.*)|%]%]")
-- 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]].
-- First, we check whether or not there are colons in the link, and trim it accordingly.
elseifif mw.ustring.match(s, ":") then
-- If the link contains a colon, it could be a category or an interwiki,
-- or it could use the [[Help:Colon trick]].
-- Check for categories and interwikis.
local colonprefix = mw.ustring.match(s, "%[%[(.-):.*|%]%]") or "" -- Get the text before the first colon.
if mw.language.isKnownLanguageTag(colonprefix) or mw.ustring.match(colonprefix, "^[Cc]ategory$") then
s = ""
-- Check for the colon trick.
if elseif mw.ustring.match(s, "%(.-[%)$[:") then
s = mw.ustring.match(s, "%[%[:.-:(.*)|%]%]")
-- Otherwise, remove the text before the first colon.
else
s = mw.ustring.match(s, "%[%[.-:(.*)|%]%]")
end
-- If there are no colons, grab all of the text apart from the square brackets and the pipe.
else
s = mw.ustring.match(s, "%[%[(.*)|%]%]")
end
if mw.ustring.match(s, "%(.-%)$") then
-- Next up, brackets and commas.
if mw.ustring.match(s, "%(.-%)$") then -- Brackets trump commas.
s = mw.ustring.match(s, "(.-) ?%(.-%)$")
elseif mw.ustring.match(s, ",") then -- If there are no brackets, display only the text before the first comma.
s = mw.ustring.match(s, "(.-),.*$")
end