Module:Delink: Difference between revisions

deal with categories/interwikis and the colon trick
en>Mr. Stradivarius
(tweak pipe trick spacing)
en>Mr. Stradivarius
(deal with categories/interwikis and the colon trick)
Line 3: Line 3:
p = {}
p = {}


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
     if mw.ustring.match(s, "%[%[|") then
         return mw.ustring.match(s, "%[%[|(.*)%]%]")
         return mw.ustring.match(s, "%[%[|(.*)%]%]")
     elseif mw.ustring.match(s, ":") then
     end
         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.
    if 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.
        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
     else
         s = mw.ustring.match(s, "%[%[(.*)|%]%]")
         s = mw.ustring.match(s, "%[%[(.*)|%]%]")
     end
     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, "(.-) ?%(.-%)$")
         s = mw.ustring.match(s, "(.-) ?%(.-%)$")
     elseif mw.ustring.match(s, ",") then
     elseif mw.ustring.match(s, ",") then -- If there are no brackets, display only the text before the first comma.
         s = mw.ustring.match(s, "(.-),.*$")
         s = mw.ustring.match(s, "(.-),.*$")
     end
     end
Anonymous user