Permanently protected module

Difference between revisions of "Module:Autolink"

From Yugipedia
Jump to: navigation, search
(update from sandbox)
(update from sandbox)
Line 4: Line 4:
  
 
local p = {}
 
local p = {}
 +
local ustring = mw.ustring
 +
local text = mw.text
  
 
function p.link( frame )
 
function p.link( frame )
Line 11: Line 13:
 
     -- marker used for {{nolink}} support (doesn't have to be a zero-width non-joiner, that's just what was used in the template version)
 
     -- marker used for {{nolink}} support (doesn't have to be a zero-width non-joiner, that's just what was used in the template version)
 
     local zwnj = '‌'
 
     local zwnj = '‌'
     local links, listmarkup, el, link, text, formatl, formatr
+
     local links, listmarkup, el, link, txt, formatl, formatr
  
 
     -- set default to stop errors
 
     -- set default to stop errors
     args[1] = args[1] and mw.text.trim( args[1] ) or ''
+
     args[1] = args[1] and text.trim( args[1] ) or ''
  
     if mw.ustring.find( args[1], zwnj ) == 1 then
+
     if ustring.find( args[1], zwnj ) == 1 then
         links = mw.ustring.gsub( args[1], '%[%[[Cc]ategory:', '[[:Category:' )
+
         links = ustring.gsub( args[1], '%[%[[Cc]ategory:', '[[:Category:' )
         links = mw.ustring.gsub( links, '%[%[[Ff]ile:', '[[:File:' )
+
         links = ustring.gsub( links, '%[%[[Ff]ile:', '[[:File:' )
         links = mw.ustring.gsub( links, '%[%[[Ii]mage:', '[[:Image:' )
+
         links = ustring.gsub( links, '%[%[[Ii]mage:', '[[:File:' )
         return mw.ustring.gsub( links, zwnj, '' )
+
         links = ustring.gsub( links, zwnj, '' )
 +
        return links
 
     else
 
     else
         links = mw.text.split( args[1], '\n' )
+
         links = text.split( args[1], '\n' )
  
 
         args[2] = #links == 1 and args[2]
 
         args[2] = #links == 1 and args[2]
Line 29: Line 32:
  
 
         for i=1, #links do
 
         for i=1, #links do
             el = mw.text.trim( links[i] )
+
             el = text.trim( links[i] )
  
 
             -- catch empty string at the start of lists
 
             -- catch empty string at the start of lists
             if not mw.ustring.match( el, '^[*#;:]?$' ) then
+
             if not ustring.match( el, '^[*#;:]?$' ) then
 
                 if listmarkup ~= '' then
 
                 if listmarkup ~= '' then
                     listmarkup = mw.ustring.match( el, '^([*#;:])' ) or '*'
+
                     listmarkup = ustring.match( el, '^([*#;:])' ) or '*'
                     el = mw.ustring.gsub( el, '^[*#;:]%s*', '' )
+
                     el = ustring.gsub( el, '^[*#;:]%s*', '' )
 
                 end
 
                 end
  
                 if mw.ustring.find( el, zwnj ) or mw.ustring.find( el, '%[%[' ) then
+
                 if ustring.find( el, zwnj ) or ustring.find( el, '%[%[' ) then
 
                     links[i] = table.concat( { listmarkup, el }, '' )
 
                     links[i] = table.concat( { listmarkup, el }, '' )
 
                 else
 
                 else
 
                     link = el
 
                     link = el
                     text = args[2] or el
+
                     txt = args[2] or el
 
                     formatl = ''
 
                     formatl = ''
 
                     formatr = ''
 
                     formatr = ''
  
                     link = mw.ustring.gsub( link, '""', '' )
+
                     link = ustring.gsub( link, '""', '' )
                     link = mw.ustring.gsub( link, "'''?", '' )
+
                     link = ustring.gsub( link, "'''?", '' )
  
 
                     -- check for formatting that can be moved out of the link entirely
 
                     -- check for formatting that can be moved out of the link entirely
                     if mw.ustring.find( text, '^""' ) and mw.ustring.find( text, '""$' ) then
+
                     if ustring.find( txt, '^""' ) and ustring.find( txt, '""$' ) then
 
                         formatl = '"'
 
                         formatl = '"'
 
                         formatr = '"'
 
                         formatr = '"'
                         text = mw.ustring.gsub( text, '""', '' )
+
                         txt = ustring.gsub( txt, '""', '' )
 
                     else
 
                     else
                         text = mw.ustring.gsub( text, '""', '"' )
+
                         txt = ustring.gsub( txt, '""', '"' )
 
                     end
 
                     end
                     if mw.ustring.find( text, "^'''" ) and mw.ustring.find( text, "'''$" ) then
+
                     if ustring.find( txt, "^'''" ) and ustring.find( txt, "'''$" ) then
 
                         formatl = formatl .. "'''"
 
                         formatl = formatl .. "'''"
 
                         formatr = "'''" .. formatr
 
                         formatr = "'''" .. formatr
                         text = mw.ustring.gsub( text, "^'''", '' )
+
                         txt = ustring.gsub( txt, "^'''", '' )
                         text = mw.ustring.gsub( text, "'''$", '' )
+
                         txt = ustring.gsub( txt, "'''$", '' )
 
                     end
 
                     end
                     if mw.ustring.find( text, "^''" ) and mw.ustring.find( text, "''$" ) then
+
                     if ustring.find( txt, "^''" ) and ustring.find( txt, "''$" ) then
 
                         formatl = formatl .. "''"
 
                         formatl = formatl .. "''"
 
                         formatr = "''" .. formatr
 
                         formatr = "''" .. formatr
                         text = mw.ustring.gsub( text, "^''", '' )
+
                         txt = ustring.gsub( txt, "^''", '' )
                         text = mw.ustring.gsub( text, "''$", '' )
+
                         txt = ustring.gsub( txt, "''$", '' )
 
                     end
 
                     end
  
                     if link == text then
+
                     if link == txt then
 
                         links[i] = table.concat( { listmarkup, formatl, '[[', link, ']]', formatr }, '' )
 
                         links[i] = table.concat( { listmarkup, formatl, '[[', link, ']]', formatr }, '' )
 
                     else
 
                     else
                         links[i] = table.concat( { listmarkup, formatl, '[[', link, '|', text, ']]', formatr }, '' )
+
                         links[i] = table.concat( { listmarkup, formatl, '[[', link, '|', txt, ']]', formatr }, '' )
 
                     end
 
                     end
 
                 end
 
                 end
Line 80: Line 83:
  
 
         links = table.concat( links, '\n' )
 
         links = table.concat( links, '\n' )
         links = mw.ustring.gsub( links, zwnj, '' )
+
         links = ustring.gsub( links, zwnj, '' )
         links = mw.ustring.gsub( links, '%[%[[Cc]ategory:', '[[:Category:' )
+
         links = ustring.gsub( links, '%[%[[Cc]ategory:', '[[:Category:' )
         links = mw.ustring.gsub( links, '%[%[[Ff]ile:', '[[:File:' )
+
         links = ustring.gsub( links, '%[%[[Ff]ile:', '[[:File:' )
         links = mw.ustring.gsub( links, '%[%[[Ii]mage:', '[[:Image:' )
+
         links = ustring.gsub( links, '%[%[[Ii]mage:', '[[:File:' )
         links = mw.text.trim( links )
+
         links = text.trim( links )
 
         return links
 
         return links
 
     end
 
     end
Line 91: Line 94:
 
function p.unlink( frame )
 
function p.unlink( frame )
 
     local args = frame:getParent().args
 
     local args = frame:getParent().args
     return args[1] and ( mw.ustring.match( args[1], '%[%[:?(.-)[|%]]' ) or mw.text.trim( args[1] ) )
+
     return args[1] and ( ustring.match( args[1], '%[%[:?(.-)[|%]]' ) or text.trim( args[1] ) )
 
end
 
end
  
 
return p
 
return p

Revision as of 22:15, 13 April 2015

--
-- implements {{Autolink}} and {{Unlink}}
--

local p = {}
local ustring = mw.ustring
local text = mw.text

function p.link( frame )
    local args = frame:getParent().args
    -- for testing from console
    -- local args = frame
    -- marker used for {{nolink}} support (doesn't have to be a zero-width non-joiner, that's just what was used in the template version)
    local zwnj = '‌'
    local links, listmarkup, el, link, txt, formatl, formatr

    -- set default to stop errors
    args[1] = args[1] and text.trim( args[1] ) or ''

    if ustring.find( args[1], zwnj ) == 1 then
        links = ustring.gsub( args[1], '%[%[[Cc]ategory:', '[[:Category:' )
        links = ustring.gsub( links, '%[%[[Ff]ile:', '[[:File:' )
        links = ustring.gsub( links, '%[%[[Ii]mage:', '[[:File:' )
        links = ustring.gsub( links, zwnj, '' )
        return links
    else
        links = text.split( args[1], '\n' )

        args[2] = #links == 1 and args[2]

        listmarkup = #links == 1 and ''

        for i=1, #links do
            el = text.trim( links[i] )

            -- catch empty string at the start of lists
            if not ustring.match( el, '^[*#;:]?$' ) then
                if listmarkup ~= '' then
                    listmarkup = ustring.match( el, '^([*#;:])' ) or '*'
                    el = ustring.gsub( el, '^[*#;:]%s*', '' )
                end

                if ustring.find( el, zwnj ) or ustring.find( el, '%[%[' ) then
                    links[i] = table.concat( { listmarkup, el }, '' )
                else
                    link = el
                    txt = args[2] or el
                    formatl = ''
                    formatr = ''

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

                    -- check for formatting that can be moved out of the link entirely
                    if ustring.find( txt, '^""' ) and ustring.find( txt, '""$' ) then
                        formatl = '"'
                        formatr = '"'
                        txt = ustring.gsub( txt, '""', '' )
                    else
                        txt = ustring.gsub( txt, '""', '"' )
                    end
                    if ustring.find( txt, "^'''" ) and ustring.find( txt, "'''$" ) then
                        formatl = formatl .. "'''"
                        formatr = "'''" .. formatr
                        txt = ustring.gsub( txt, "^'''", '' )
                        txt = ustring.gsub( txt, "'''$", '' )
                    end
                    if ustring.find( txt, "^''" ) and ustring.find( txt, "''$" ) then
                        formatl = formatl .. "''"
                        formatr = "''" .. formatr
                        txt = ustring.gsub( txt, "^''", '' )
                        txt = ustring.gsub( txt, "''$", '' )
                    end

                    if link == txt then
                        links[i] = table.concat( { listmarkup, formatl, '[[', link, ']]', formatr }, '' )
                    else
                        links[i] = table.concat( { listmarkup, formatl, '[[', link, '|', txt, ']]', formatr }, '' )
                    end
                end
            end
        end

        links = table.concat( links, '\n' )
        links = ustring.gsub( links, zwnj, '' )
        links = ustring.gsub( links, '%[%[[Cc]ategory:', '[[:Category:' )
        links = ustring.gsub( links, '%[%[[Ff]ile:', '[[:File:' )
        links = ustring.gsub( links, '%[%[[Ii]mage:', '[[:File:' )
        links = text.trim( links )
        return links
    end
end

function p.unlink( frame )
    local args = frame:getParent().args
    return args[1] and ( ustring.match( args[1], '%[%[:?(.-)[|%]]' ) or text.trim( args[1] ) )
end

return p