Permanently protected module

Difference between revisions of "Module:Autolink"

From Yugipedia
Jump to: navigation, search
(Created page with "-- -- {{Autolink}} -- local p = {} -- -- Hack to give expr functionality -- @todo change to mw.ext.ParserFunctions.expr when ParserFunction changes are backported to wikia -...")
m
Line 4: Line 4:
  
 
local p = {}
 
local p = {}
 
--
 
-- Hack to give expr functionality
 
-- @todo change to mw.ext.ParserFunctions.expr when ParserFunction changes are backported to wikia
 
--
 
local function expr( frame, str )
 
    ret = frame:preprocess( '{{#expr:' .. str .. '}}' )
 
    return ret
 
end
 
  
 
function p.link( frame )
 
function p.link( frame )
 
     local args = frame:getParent().args
 
     local args = frame:getParent().args
     -- zero-width non-joiner
+
     -- zero-width non-joiner, used for {{nolink}} support
 
     local zwnj = '‌'
 
     local zwnj = '‌'
     local repl
+
     local links, nolink, el, link, text
  
     if mw.ustring.find( args[1], zwnj ) == 0 then
+
     if args[2] then
         ret = '0'
+
         -- {{autolink|link|text}}
 +
        args[1] = args[1] .. '|' .. args[2]
 +
        links = { args[1] }
 
     else
 
     else
        -- split args[1] with * as the delimiter
 
 
         links = mw.text.split( args[1], '*' )
 
         links = mw.text.split( args[1], '*' )
 +
    end
 +
 +
    for i=1, #links do
 +
        nolink = false
  
         -- rejoin with or comma as the delimiter
+
         -- catch empty string at the start of lists
         -- no idea what $x$ does (pattern)
+
         if links[i] ~= '' then
        -- no idea what the subject is all about
+
            el = mw.text.trim( links[i] )
    end
+
 
 +
            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, '"', '' )
{{ #replace: {{ #ifeq: {{ #pos: {{{1}}} | ‌ }} | 0
+
            link = mw.ustring.gsub( link, "'", '' )
  | {{{1}}}
 
  | {{ #arraydefine: @template:autolink-links | {{{1<noinclude>|{{int:Mainpage}}</noinclude>}}} | * | unique }}{{ #arrayprint: @template:autolink-links || $x$ | {{ #if: {{ #pos: {{{1}}} | * }} | <nowiki/>
 
* }}{{
 
    #if: {{ #pos: $x$ | {{!((}} }}
 
    | $x$
 
    | {{ #ifeq: {{ #pos: $x$ | &zwnj; }} | {{ #expr: {{ #len: $x$ }} - 6 }}
 
      | $x$
 
      | {{ #vardefineecho: $template:autolink-quote | {{ #ifexpr: {{ #ifeq: {{ #pos: $x$ | "" }} | 0 | 1 | 0 }} + {{ #ifexpr: {{ #len: $x$ }} = {{ #pos: $x$ | "" | 1 }} + 2 | 1 | 0 }} = 2 | " }} }}[[:{{ #replace: {{ #replace: {{ #replace: $x$ | ''' }} | '' }} | "" }}|{{ #if: {{{2|}}} | {{{2}}} | {{ #if: {{ #var: $template:autolink-quote }} | {{ #replace: $x$ | "" }} | {{ #replace: $x$ | "" | " }} }} }}]]{{ #var: $template:autolink-quote }}
 
    }}
 
  }} }}
 
}} | &zwnj; }}
 
  
]=]
+
            if nolink then
 +
                links[i] = text
 +
            else
 +
                links[i] = table.concat( {'[[', link, '|', text, ']]'}, '' )
 +
            end
 +
        end
 +
    end
  
 +
    links = table.concat( links, '\n*' )
 +
    return links
 
end
 
end
  
 
return p
 
return p

Revision as of 10:48, 17 February 2015

--
-- {{Autolink}}
--

local p = {}

function p.link( frame )
    local args = frame:getParent().args
    -- zero-width non-joiner, used for {{nolink}} support
    local zwnj = '&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