Module:Delink: Difference between revisions

en>Mr. Stradivarius
(move cat/interwiki/file check earlier to the start of the processing chain)
en>Mr. Stradivarius
(move the colon trick processing to earlier in the chain)
Line 14: Line 14:
     -- 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]].
      
      
     -- First, we check whether or not there are colons in the link, and trim it accordingly.
     -- First, remove the text before the first colon, if any.
     if mw.ustring.match(s, ":") then
     if mw.ustring.match(s, ":") then
           
         s = mw.ustring.match(s, "%[%[.-:(.*)|%]%]")
         -- Check for the [[Help:Colon trick]].
        if 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.
     -- If there are no colons, grab all of the text apart from the square brackets and the pipe.
     else
     else
Line 40: Line 31:
end
end


local function delinkOne(text)
local function delinkOne(s)
     -- First, check for categories, interwikis, and files.
     -- First, check for categories, interwikis, and files.
     local colonprefix = mw.ustring.match(text, "%[%[(.-):.*%]%]") 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)
     or mw.ustring.match(colonprefix, "^[Cc]ategory$")
     or mw.ustring.match(colonprefix, "^[Cc]ategory$")
Line 49: Line 40:
         return ""
         return ""
     end
     end
     if mw.ustring.match(text, "[^|].*|%]%]") or mw.ustring.match(text, "%[%[|") then -- Weed out the pipe tricks first.
   
         return delinkPipeTrick(text)
    -- Remove the colon if the link is using the [[Help:Colon trick]].
    if mw.ustring.match(s, "%[%[:") then
        s = "[[" .. mw.ustring.match(s, "%[%[:(.*%]%])")
    end
   
     if mw.ustring.match(s, "[^|].*|%]%]") or mw.ustring.match(s, "%[%[|") then -- Weed out the pipe tricks first.
         return delinkPipeTrick(s)
     end
     end
     -- Find the link area and display area of the wikilink
     -- Find the link area and display area of the wikilink
     local linkarea, display
     local linkarea, display
     if mw.ustring.match(text, "|") 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(text, "^%[%[(.-)|(.+)%]%]")
         linkarea, display = mw.ustring.match(s, "^%[%[(.-)|(.+)%]%]")
     else
     else
         -- If the link isn't piped, the display area and the link area are the same.
         -- If the link isn't piped, the display area and the link area are the same.
         linkarea = mw.ustring.match(text, "^%[%[(.-)%]%]")
         linkarea = mw.ustring.match(s, "^%[%[(.-)%]%]")
         display = linkarea
         display = linkarea
     end
     end