Permanently protected module

Difference between revisions of "Module:Autolink"

From Yugipedia
Jump to: navigation, search
(slightly simpler from sandbox; don't overwrite "links" quite so much (still would like to figure out how to make it never happen))
(from sandbox)
Line 31: Line 31:
 
             if not ustring.match( el, '^[*#;:]?$' ) then
 
             if not ustring.match( el, '^[*#;:]?$' ) then
 
                 if listmarkup ~= '' then
 
                 if listmarkup ~= '' then
                     listmarkup = ustring.match( el, '^([*#;:])' ) or '*'
+
                     listmarkup = ( ustring.match( el, '^([*#;:])' ) or '*' ) .. ' '
 
                     el = ustring.gsub( el, '^[*#;:]%s*', '' )
 
                     el = ustring.gsub( el, '^[*#;:]%s*', '' )
 
                 end
 
                 end
Line 43: Line 43:
 
                     formatr = ''
 
                     formatr = ''
  
                     link = ustring.gsub( link, '""', '' )
+
                     link = ustring.gsub( 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
Line 57: Line 56:
 
                         formatl = formatl .. "'''"
 
                         formatl = formatl .. "'''"
 
                         formatr = "'''" .. formatr
 
                         formatr = "'''" .. formatr
                         txt = ustring.gsub( txt, "^'''", '' )
+
                         txt = ustring.gsub( ustring.gsub( txt, "^'''", '' ), "'''$", '' )
                        txt = ustring.gsub( txt, "'''$", '' )
 
 
                     end
 
                     end
 
                     if ustring.find( txt, "^''" ) and ustring.find( txt, "''$" ) then
 
                     if ustring.find( txt, "^''" ) and ustring.find( txt, "''$" ) then
 
                         formatl = formatl .. "''"
 
                         formatl = formatl .. "''"
 
                         formatr = "''" .. formatr
 
                         formatr = "''" .. formatr
                         txt = ustring.gsub( txt, "^''", '' )
+
                         txt = ustring.gsub( ustring.gsub( txt, "^''", '' ), "''$", '' )
                        txt = ustring.gsub( txt, "''$", '' )
 
 
                     end
 
                     end
  
Line 79: Line 76:
 
     end
 
     end
  
     links = ustring.gsub( links, zwnj, '' )
+
     links = text.trim( ustring.gsub( ustring.gsub( ustring.gsub( ustring.gsub( links, zwnj, '' ), '%[%[[Cc]ategory:', '[[:Category:' ), '%[%[[Ff]ile:', '[[:File:' ), '%[%[[Ii]mage:', '[[:File:' ) )
    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
 
     return links
 
end
 
end

Revision as of 12:36, 1 July 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 linkarr, links, listmarkup, el, link, txt, formatl, formatr

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

    if ustring.find( links, zwnj ) ~= 1 then
        linkarr = text.split( links, '\n' )

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

        listmarkup = #linkarr == 1 and ''

        for i=1, #linkarr do
            el = text.trim( linkarr[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
                    linkarr[i] = table.concat( { listmarkup, el }, '' )
                else
                    link = el
                    txt = args[2] or el
                    formatl = ''
                    formatr = ''

                    link = ustring.gsub( 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( ustring.gsub( txt, "^'''", '' ), "'''$", '' )
                    end
                    if ustring.find( txt, "^''" ) and ustring.find( txt, "''$" ) then
                        formatl = formatl .. "''"
                        formatr = "''" .. formatr
                        txt = ustring.gsub( ustring.gsub( txt, "^''", '' ), "''$", '' )
                    end

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

        links = table.concat( linkarr, '\n' )
    end

    links = text.trim( ustring.gsub( ustring.gsub( ustring.gsub( ustring.gsub( links, zwnj, '' ), '%[%[[Cc]ategory:', '[[:Category:' ), '%[%[[Ff]ile:', '[[:File:' ), '%[%[[Ii]mage:', '[[:File:' ) )
    return links
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