Permanently protected module

Difference between revisions of "Module:Autolink"

From Yugipedia
Jump to: navigation, search
(if nolink then links[i] = el (instead of text))
(don't do any processing if nolink is true)
Line 39: Line 39:
 
             end
 
             end
  
             if mw.ustring.find( el, '|' ) ~= nil  then
+
             if not nolink then
                el = mw.text.split( el, '|' )
+
                if mw.ustring.find( el, '|' ) ~= nil  then
                link = el[1]
+
                    el = mw.text.split( el, '|' )
                text = el[2]
+
                    link = el[1]
            else
+
                    text = el[2]
                 link = el
+
                else
                 text = el
+
                    link = el
 +
                    text = el
 +
                end
 +
 
 +
                 link = mw.ustring.gsub( link, '""', '' )
 +
                link = mw.ustring.gsub( link, "'''", '' )
 +
                link = mw.ustring.gsub( link, "''", '' )
 +
                 text = mw.ustring.gsub( text, '""', '"' )
 
             end
 
             end
 
            link = mw.ustring.gsub( link, '""', '' )
 
            link = mw.ustring.gsub( link, "'''", '' )
 
            link = mw.ustring.gsub( link, "''", '' )
 
            text = mw.ustring.gsub( text, '""', '"' )
 
  
 
             if nolink then
 
             if nolink then

Revision as of 12:14, 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, nolink, 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

    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
                nolink = true
            elseif mw.ustring.find( el, '%[%[' ) then
                if mw.ustring.find( el, '%[%[([Ii]mage|[Ff]ile|[Cc]ategory|[Mm]edia)%:' ) then
                    el = mw.ustring.gsub( el, '%[%[([Ii]mage|[Ff]ile|[Cc]ategory|[Mm]edia)%:', '[[:%1:' )
                end
                nolink = true
            end

            if not nolink then
                if mw.ustring.find( el, '|' ) ~= nil  then
                    el = mw.text.split( el, '|' )
                    link = el[1]
                    text = el[2]
                else
                    link = el
                    text = el
                end

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

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

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

return p