Difference between revisions of "Module:Autolink/sandbox"

From Yugipedia
Jump to: navigation, search
(fix single-item lists)
(stop caring so much about moving italics/bolding out of the link)
Line 21: Line 21:
 
end
 
end
  
local linkarr, numlinks, links, listmarkup, el, link, txt
+
local linkarr, numlinks, links, listmarkup, el, link, quotes, txt
local format = { '', '', '' }
 
  
 
-- set default to stop errors
 
-- set default to stop errors
Line 46: Line 45:
 
linkarr[i] = table.concat( { listmarkup, el }, '' )
 
linkarr[i] = table.concat( { listmarkup, el }, '' )
 
else
 
else
format[1] = el:find( '^"%[%[.-%]%]"$' ) and '"' or ''
+
quotes = el:find( '^"%[%[.-%]%]"$' ) and '"' or ''
 
el = el:match( '%[%[(.-)%]%]' ) or el
 
el = el:match( '%[%[(.-)%]%]' ) or el
 
link = el
 
link = el
Line 54: Line 53:
 
link = unformat( link )
 
link = unformat( link )
  
-- check for formatting that can be moved out of the link entirely
+
-- move beginning/ending double quotes out of the link entirely
 
if txt:find( '^""' ) and txt:find( '""$' ) then
 
if txt:find( '^""' ) and txt:find( '""$' ) then
format[1] = '"'
+
quotes = '"'
 
txt = txt:gsub( '^""', '' ):gsub( '""$', '' )
 
txt = txt:gsub( '^""', '' ):gsub( '""$', '' )
 
end
 
end
 
txt = txt:gsub( '""', '"' )
 
txt = txt:gsub( '""', '"' )
if txt:find( "^'''" ) and txt:find( "'''$" ) then
 
format[2] = "'''"
 
txt = txt:gsub( "'''$", '' ):gsub( "^'''", '' )
 
end
 
if txt:find( "^''" ) and txt:find( "''$" ) then
 
format[3] = "''"
 
txt = txt:gsub( "''$", '' ):gsub( "^''", '' )
 
end
 
 
else
 
else
 
txt = txt:gsub( "'", ''' )
 
txt = txt:gsub( "'", ''' )
Line 82: Line 73:
  
 
if link == txt then
 
if link == txt then
linkarr[i] = table.concat( { listmarkup, table.concat( format ), '[[', link, ']]', format[3], format[2], format[1] }, '' )
+
linkarr[i] = table.concat( { listmarkup, quotes, '[[', link, ']]', quotes }, '' )
 
else
 
else
linkarr[i] = table.concat( { listmarkup, table.concat( format ), '[[', link, '|', txt, ']]', format[3], format[2], format[1] }, '' )
+
linkarr[i] = table.concat( { listmarkup, quotes, '[[', link, '|', txt, ']]', quotes }, '' )
 
end
 
end
 
end
 
end

Revision as of 16:07, 25 April 2023

--
-- 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, numlinks, links, listmarkup, el, link, quotes, txt

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

	linkarr = text.split( links, '\n' )
	numlinks = #linkarr

	args[2] = numlinks == 1 and args[2]

	for i = 1, numlinks do
		el = text.trim( linkarr[i] )

		-- skip empty lines
		if not el:find( '^[*#;:]?%s*$' ) then
			if listmarkup ~= '' then
				listmarkup = ( el:match( '^([*#;:])' ) or '*' ) .. ' '
				el = el:gsub( '^[*#;:]%s*', '' )
			end

			-- check for piped link in line or multiple links in line
			if el:find( '%[%[.-|.-%]%]' ) or el:find( '%]%].-%[%[' ) then
				linkarr[i] = table.concat( { listmarkup, el }, '' )
			else
				quotes = el:find( '^"%[%[.-%]%]"$' ) and '"' or ''
				el = el:match( '%[%[(.-)%]%]' ) or el
				link = el
				txt = args[2] or el

				if el:find( '%[%[' ) ~= 1 then
					link = unformat( link )

					-- move beginning/ending double quotes out of the link entirely
					if txt:find( '^""' ) and txt:find( '""$' ) then
						quotes = '"'
						txt = txt:gsub( '^""', '' ):gsub( '""$', '' )
					end
					txt = txt:gsub( '""', '"' )
				else
					txt = txt:gsub( "'", ''' )
				end
				if txt:find( '##' ) then
					txt = txt:gsub( '##', '#' )
				elseif txt:find( '[^&]#' ) then
					txt = txt:gsub( '([^&])#', '%1 § ')
				end
				
				if numlinks == 1 then
					listmarkup = ''
				end

				if link == txt then
					linkarr[i] = table.concat( { listmarkup, quotes, '[[', link, ']]', quotes }, '' )
				else
					linkarr[i] = table.concat( { listmarkup, quotes, '[[', link, '|', txt, ']]', quotes }, '' )
				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