Permanently protected module

Difference between revisions of "Module:Autolink"

From Yugipedia
Jump to: navigation, search
(Lua regexes are weak)
(refactor to allow {{nolink}} at the beginning preventing any formatting and to remove the need for the nolink variable)
Line 11: Line 11:
 
     -- zero-width non-joiner, used for {{nolink}} support
 
     -- zero-width non-joiner, used for {{nolink}} support
 
     local zwnj = '‌'
 
     local zwnj = '‌'
     local links, nolink, el, link, text
+
     local links, escape, el, link, text
  
 
     -- set default to stop errors
 
     -- set default to stop errors
Line 24: Line 24:
 
     end
 
     end
  
     for i=1, #links do
+
     if not mw.ustring.find( mw.text.trim( args[1] ), zwnj ) == 1 then
        nolink = false
+
        for i=1, #links do
        el = mw.text.trim( links[i] )
+
            nolink = false
 +
            el = mw.text.trim( links[i] )
  
        -- catch empty string at the start of lists
+
            -- catch empty string at the start of lists
        if el ~= '' then
+
            if el ~= '' then
            if mw.ustring.find( el, zwnj ) then
+
                if mw.ustring.find( el, zwnj ) then
                nolink = true
+
                    links[i] = el
            elseif mw.ustring.find( el, '%[%[' ) then
+
                elseif mw.ustring.find( el, '%[%[' ) then
                el = mw.ustring.gsub( el, '%[%[[Cc]ategory:', '[[:Category:' )
+
                    el = mw.ustring.gsub( el, '%[%[[Cc]ategory:', '[[:Category:' )
                el = mw.ustring.gsub( el, '%[%[[Ff]ile:', '[[:File:' )
+
                    el = mw.ustring.gsub( el, '%[%[[Ff]ile:', '[[:File:' )
                el = mw.ustring.gsub( el, '%[%[[Ii]mage:', '[[:Image:' )
+
                    el = mw.ustring.gsub( el, '%[%[[Ii]mage:', '[[:Image:' )
                el = mw.ustring.gsub( el, '%[%[[Mm]edia:', '[[:Media:' )
+
                    el = mw.ustring.gsub( el, '%[%[[Mm]edia:', '[[:Media:' )
                nolink = true
+
                     links[i] = el
            end
 
 
 
            if not nolink then
 
                if mw.ustring.find( el, '|' ) ~= nil  then
 
                    el = mw.text.split( el, '|' )
 
                     link = el[1]
 
                    text = el[2]
 
 
                 else
 
                 else
 
                     link = el
 
                     link = el
 
                     text = el
 
                     text = el
                end
 
  
                link = mw.ustring.gsub( link, '""', '' )
+
                    link = mw.ustring.gsub( link, '""', '' )
                link = mw.ustring.gsub( link, "'''?", '' )
+
                    link = mw.ustring.gsub( link, "'''?", '' )
                text = mw.ustring.gsub( text, '""', '"' )
+
                    text = mw.ustring.gsub( text, '""', '"' )
            end
+
 
 +
                    if mw.ustring.find( link, '^[Cc]ategory:' ) or
 +
                            mw.ustring.find( link, '^[Ff]ile:' ) or
 +
                            mw.ustring.find( link, '^[Ii]mage:' ) or
 +
                            mw.ustring.find( link, '^[Mm]edia:' ) then
 +
                        escape = ':'
 +
                    else
 +
                        escape = ''
 +
                    end
  
            if nolink then
+
                    if link == text then
                links[i] = el
+
                        links[i] = table.concat( {'[[', escape, link, ']]'}, '' )
            elseif link == text then
+
                    else
                links[i] = table.concat( {'[[:', link, ']]'}, '' )
+
                        links[i] = table.concat( {'[[', escape, link, '|', text, ']]'}, '' )
            else
+
                    end
                links[i] = table.concat( {'[[:', link, '|', text, ']]'}, '' )
+
                end
 
             end
 
             end
 
         end
 
         end

Revision as of 13:02, 17 February 2015

--
-- implements {{Autolink}}
--

local p = {}

function p.link( frame )
    local args = frame:getParent().args
    -- for testing from console
    -- local args = frame
    -- zero-width non-joiner, used for {{nolink}} support
    local zwnj = '‌'
    local links, escape, el, link, text

    -- set default to stop errors
    args[1] = args[1] or ''

    if args[2] then
        -- {{autolink|link|text}}
        args[1] = args[1] .. '|' .. args[2]
        links = { args[1] }
    else
        links = mw.text.split( args[1], '*' )
    end

    if not mw.ustring.find( mw.text.trim( args[1] ), zwnj ) == 1 then
        for i=1, #links do
            nolink = false
            el = mw.text.trim( links[i] )

            -- catch empty string at the start of lists
            if el ~= '' then
                if mw.ustring.find( el, zwnj ) then
                    links[i] = el
                elseif mw.ustring.find( el, '%[%[' ) then
                    el = mw.ustring.gsub( el, '%[%[[Cc]ategory:', '[[:Category:' )
                    el = mw.ustring.gsub( el, '%[%[[Ff]ile:', '[[:File:' )
                    el = mw.ustring.gsub( el, '%[%[[Ii]mage:', '[[:Image:' )
                    el = mw.ustring.gsub( el, '%[%[[Mm]edia:', '[[:Media:' )
                    links[i] = el
                else
                    link = el
                    text = el

                    link = mw.ustring.gsub( link, '""', '' )
                    link = mw.ustring.gsub( link, "'''?", '' )
                    text = mw.ustring.gsub( text, '""', '"' )

                    if mw.ustring.find( link, '^[Cc]ategory:' ) or
                            mw.ustring.find( link, '^[Ff]ile:' ) or
                            mw.ustring.find( link, '^[Ii]mage:' ) or
                            mw.ustring.find( link, '^[Mm]edia:' ) then
                        escape = ':'
                    else
                        escape = ''
                    end

                    if link == text then
                        links[i] = table.concat( {'[[', escape, link, ']]'}, '' )
                    else
                        links[i] = table.concat( {'[[', escape, link, '|', text, ']]'}, '' )
                    end
                end
            end
        end
    end

    links = table.concat( links, '\n*' )
    links = mw.ustring.gsub( links, zwnj, '' )
    links = mw.text.trim( links )
    return links
end

return p