Permanently protected module

Difference between revisions of "Module:Autolink"

From Yugipedia
Jump to: navigation, search
(from sandbox: fix for input which is manually linked and enclosed in a single set of double quotes)
(update from sandbox)
(One intermediate revision by the same user not shown)
Line 7: Line 7:
  
 
function p.link( frame )
 
function p.link( frame )
    if frame == mw.getCurrentFrame() then
+
if frame == mw.getCurrentFrame() then
        args = frame:getParent().args
+
args = frame:getParent().args
    else
+
else
        args = frame
+
args = frame
    end
+
end
  
    -- 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 linkarr, links, listmarkup, el, ell, elr, link, raw, txt, formatl, formatr
+
local linkarr, links, listmarkup, el, ell, elr, link, raw, txt, formatl, formatr
  
    -- set default to stop errors
+
-- set default to stop errors
    links = args[1] and text.trim( args[1] ) or ''
+
links = args[1] and text.trim( args[1] ) or ''
  
    -- check for nolink token at the front of the input, which prevents per-link/item processing
+
-- check for nolink token at the front of the input, which prevents per-link/item processing
    if links:find( zwnj ) ~= 1 then
+
if links:find( zwnj ) ~= 1 then
        linkarr = text.split( links, '\n' )
+
linkarr = text.split( links, '\n' )
  
        args[2] = #linkarr == 1 and args[2]
+
args[2] = #linkarr == 1 and args[2]
  
        listmarkup = #linkarr == 1 and ''
+
listmarkup = #linkarr == 1 and ''
  
        for i = 1, #linkarr do
+
for i = 1, #linkarr do
            el = text.trim( linkarr[i] )
+
el = text.trim( linkarr[i] )
  
            -- catch empty string at the start of lists
+
-- catch empty string at the start of lists
            if not el:find( '^[*#;:]?$' ) then
+
if not el:find( '^[*#;:]?$' ) then
                if listmarkup ~= '' then
+
if listmarkup ~= '' then
                    listmarkup = ( el:match( '^([*#;:])' ) or '*' ) .. ' '
+
listmarkup = ( el:match( '^([*#;:])' ) or '*' ) .. ' '
                    el = el:gsub( '^[*#;:]%s*', '' )
+
el = el:gsub( '^[*#;:]%s*', '' )
                end
+
end
  
                if el:find( zwnj ) or el:find( '%[%[.-|.-%]%]' ) then
+
if el:find( zwnj ) or el:find( '%[%[.-|.-%]%]' ) then
                    linkarr[i] = table.concat( { listmarkup, el }, '' )
+
linkarr[i] = table.concat( { listmarkup, el }, '' )
                else
+
else
                    raw = el:find( '%[%[' )
+
raw = el:find( '%[%[' )
                    ell = el:match( '^"%[%[' ) and '"' or ''
+
ell = el:match( '^"%[%[' ) and '"' or ''
                    elr = el:match( '%]%]"$' ) and '"' or ''
+
elr = el:match( '%]%]"$' ) and '"' or ''
                    el = el:match( '%[%[(.-)%]%]' ) or el
+
el = el:match( '%[%[(.-)%]%]' ) or el
                    link = el
+
link = el
                    txt = args[2] or el
+
txt = args[2] or el
                    formatl = ''
+
formatl = ''
                    formatr = ''
+
formatr = ''
  
                    if raw ~= 1 then
+
if raw ~= 1 then
                        link = link:gsub( '""', '' ):gsub( "'''?", '' )
+
link = link:gsub( '""', '' )
 +
:gsub( "'''?", '' )
 +
:gsub( '##', ' ' )
 +
:gsub( '  ', ' ' )
  
                        -- 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 txt:find( '^""' ) and txt:find( '""$' ) then
+
if txt:find( '^""' ) and txt:find( '""$' ) then
                            formatl = '"'
+
formatl = '"'
                            formatr = '"'
+
formatr = '"'
                            txt = txt:gsub( '""', '' )
+
txt = txt:gsub( '""', '' )
                        else
+
else
                            txt = txt:gsub( '""', '"' )
+
txt = txt:gsub( '""', '"' )
                        end
+
end
                        if txt:find( "^'''" ) and txt:find( "'''$" ) then
+
if txt:find( "^'''" ) and txt:find( "'''$" ) then
                            formatl = formatl .. "'''"
+
formatl = formatl .. "'''"
                            formatr = "'''" .. formatr
+
formatr = "'''" .. formatr
                            txt = txt:gsub( "'''$", '' ):gsub( "^'''", '' )
+
txt = txt:gsub( "'''$", '' ):gsub( "^'''", '' )
                        end
+
end
                        if txt:find( "^''" ) and txt:find( "''$" ) then
+
if txt:find( "^''" ) and txt:find( "''$" ) then
                            formatl = formatl .. "''"
+
formatl = formatl .. "''"
                            formatr = "''" .. formatr
+
formatr = "''" .. formatr
                            txt = txt:gsub( "''$", '' ):gsub( "^''", '' )
+
txt = txt:gsub( "''$", '' ):gsub( "^''", '' )
                        end
+
end
                    else
+
else
                        txt = txt:gsub( "'", ''' )
+
txt = txt:gsub( "'", ''' )
                    end
+
end
                    if txt:find( '[^&]#' ) then
+
if txt:find( '##' ) then
                        txt = txt:gsub( '([^&])#', '%1 § ')
+
txt = txt:gsub( '##', '#' )
                    end
+
elseif txt:find( '[^&]#' ) then
 +
txt = txt:gsub( '([^&])#', '%1 § ')
 +
end
  
                    if link == txt then
+
if link == txt then
                        linkarr[i] = table.concat( { listmarkup, ell, formatl, '[[', link, ']]', formatr, elr }, '' )
+
linkarr[i] = table.concat( { listmarkup, ell, formatl, '[[', link, ']]', formatr, elr }, '' )
                    else
+
else
                        linkarr[i] = table.concat( { listmarkup, ell, formatl, '[[', link, '|', txt, ']]', formatr, elr }, '' )
+
linkarr[i] = table.concat( { listmarkup, ell, formatl, '[[', link, '|', txt, ']]', formatr, elr }, '' )
                    end
+
end
                end
+
end
            end
+
end
        end
+
end
  
        links = table.concat( linkarr, '\n' )
+
links = table.concat( linkarr, '\n' )
    end
+
end
  
    links = text.trim( links:gsub( zwnj, '' )
+
links = text.trim( links:gsub( zwnj, '' )
        :gsub( '%[%[[Cc]ategory:', '[[:Category:' )
+
:gsub( '%[%[[Cc]ategory:', '[[:Category:' )
        :gsub( '%[%[[Ff]ile:', '[[:File:' )
+
:gsub( '%[%[[Ff]ile:', '[[:File:' )
        :gsub( '%[%[[Ii]mage:', '[[:File:' ) )
+
:gsub( '%[%[[Ii]mage:', '[[:File:' ) )
    return links
+
return links
 
end
 
end
  
 
function p.unlink( frame )
 
function p.unlink( frame )
    local args = frame:getParent().args
+
local args = frame:getParent().args
    return args[1] and ( args[1]:match( '%[%[:?(.-)[|%]]' ) or text.trim( args[1] ) )
+
return args[1] and ( args[1]:match( '%[%[:?(.-)[|%]]' ) or text.trim( args[1] ) )
 
end
 
end
  
 
return p
 
return p

Revision as of 23:55, 14 December 2018

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

local p = {}
local text = mw.text

function p.link( frame )
	if frame == mw.getCurrentFrame() then
		args = frame:getParent().args
	else
		args = frame
	end

	-- 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, ell, elr, link, raw, txt, formatl, formatr

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

	-- check for nolink token at the front of the input, which prevents per-link/item processing
	if links:find( 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 el:find( '^[*#;:]?$' ) then
				if listmarkup ~= '' then
					listmarkup = ( el:match( '^([*#;:])' ) or '*' ) .. ' '
					el = el:gsub( '^[*#;:]%s*', '' )
				end

				if el:find( zwnj ) or el:find( '%[%[.-|.-%]%]' ) then
					linkarr[i] = table.concat( { listmarkup, el }, '' )
				else
					raw = el:find( '%[%[' )
					ell = el:match( '^"%[%[' ) and '"' or ''
					elr = el:match( '%]%]"$' ) and '"' or ''
					el = el:match( '%[%[(.-)%]%]' ) or el
					link = el
					txt = args[2] or el
					formatl = ''
					formatr = ''

					if raw ~= 1 then
						link = link:gsub( '""', '' )
									:gsub( "'''?", '' )
									:gsub( '##', ' ' )
									:gsub( '  ', ' ' )

						-- check for formatting that can be moved out of the link entirely
						if txt:find( '^""' ) and txt:find( '""$' ) then
							formatl = '"'
							formatr = '"'
							txt = txt:gsub( '""', '' )
						else
							txt = txt:gsub( '""', '"' )
						end
						if txt:find( "^'''" ) and txt:find( "'''$" ) then
							formatl = formatl .. "'''"
							formatr = "'''" .. formatr
							txt = txt:gsub( "'''$", '' ):gsub( "^'''", '' )
						end
						if txt:find( "^''" ) and txt:find( "''$" ) then
							formatl = formatl .. "''"
							formatr = "''" .. formatr
							txt = txt:gsub( "''$", '' ):gsub( "^''", '' )
						end
					else
						txt = txt:gsub( "'", ''' )
					end
					if txt:find( '##' ) then
						txt = txt:gsub( '##', '#' )
					elseif txt:find( '[^&]#' ) then
						txt = txt:gsub( '([^&])#', '%1 § ')
					end

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

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

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

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

return p