Module:Autolink

Jump to navigation Jump to search

Implements {{Autolink}} and {{Unlink}}.


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

local p = {}
local text = mw.text

-- removes all special formatting from a string
local function unformat( s )
	return s:gsub( '""', '' )
		:gsub( "'''?", '' )
		:gsub( '##', ' ' )
		:gsub( '  ', ' ' )
end

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

	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 ''

	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

			-- check for piped link in line or or multiple links in line
			if el:find( '%[%[.-|.-%]%]' ) 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 = unformat( link )

					-- 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' )

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

-- returns the target of the first link in text
-- to return the entire text without any links instead, see {{Delink}}
function p.unlink( frame )
	local txt = frame:getParent().args[1]
	return txt and ( txt:match( '%[%[:?(.-)[|%]]' ) or text.trim( txt ) )
end

return p