Permanently protected module

Module:Autolink

From Yugipedia
Revision as of 10:48, 17 February 2015 by Cqm (talk)
Jump to: navigation, search

--
-- {{Autolink}}
--

local p = {}

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

    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

        -- catch empty string at the start of lists
        if links[i] ~= '' then
            el = mw.text.trim( links[i] )

            if mw.ustring.find( el, zwnj ) then
                nolink = true
            end

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

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

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

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

return p