Permanently protected module

Difference between revisions of "Module:Autolink"

From Yugipedia
Jump to: navigation, search
(fix and tweak)
(some tweaks: clean up globals; slightly optimize unprettify(); slightly more descriptive variable names in prettify_link(); expand ref comment a bit)
 
(36 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
--
 
--
-- implements {{Autolink}} and {{Unlink}}
+
-- implements {{Autolink}}, {{Unlink}}, and {{Formatted link}}
 
--
 
--
  
 +
require('Module:No globals')
 
local p = {}
 
local p = {}
  
function p.link( frame )
+
-- removes all special formatting from a string
    local args = frame:getParent().args
+
local function unprettify( s )
    -- for testing from console
+
return s:gsub( '""', '' )
    -- local args = frame
+
:gsub( "'''?", '' )
    -- marker used for {{nolink}} support (doesn't have to be a zero-width non-joiner, that's just what I used in the template version)
+
:gsub( '[# ]##?', ' ' ) --lmao
    local zwnj = '‌'
+
:gsub( ' ', ' ' )
    local links, nolink, listmarkup, escape, el, link, text, formatl, formatr
+
end
  
    -- set default to stop errors
+
-- formats a link
    args[1] = args[1] or ''
+
local function prettify_link( target, label )
    args[1] = mw.text.trim( args[1] )
+
if not target or target == '' then
 +
return ''
 +
end
 +
 +
if not label or label == '' then
 +
label = target
 +
end
 +
 +
local plain_target = unprettify( target )
 +
local plain_label = unprettify( label )
 +
local prettified_label = label:gsub( '""', '"' ):gsub( '([^&# ])#([^#])', '%1 § %2' ):gsub( '##', '#' )
 +
 +
local link = ''
 +
if plain_target == prettified_label then
 +
-- no formatting
 +
link = ('[[%s]]'):format( plain_target )
 +
elseif plain_target == plain_label then
 +
local start, _end = prettified_label:find( plain_target, 1, true )
 +
if start then
 +
-- only formatting is on the link boundaries (`''Foo Bar''`)
 +
link = ('%s[[%s]]%s'):format( prettified_label:sub( 1, start - 1 ), plain_target, prettified_label:sub( _end + 1 ) )
 +
else
 +
-- formatting within the link (`''Foo Bar'' (Quux)`)
 +
link = ('[[%s|%s]]'):format( plain_target, prettified_label )
 +
end
 +
else
 +
if plain_label == prettified_label then
 +
-- no formatting
 +
link = ('[[%s|%s]]'):format( plain_target, prettified_label )
 +
else
 +
local start, _end = prettified_label:find( plain_label, 1, true )
 +
if start then
 +
-- only formatting is on the text boundaries (`[[Foo Bar Baz|''Foo Bar'']]`)
 +
link = ('%s[[%s|%s]]%s'):format( prettified_label:sub( 1, start - 1 ), plain_target, prettified_label:sub( start, _end ), prettified_label:sub( _end + 1 ) )
 +
else
 +
-- formatting within the text (`[[Foo Bar Baz|''Foo'' Bar]]`)
 +
link = ('[[%s|%s]]'):format( plain_target, prettified_label )
 +
end
 +
end
 +
end
 +
return link
 +
end
  
    links = mw.text.split( args[1], '\n' )
+
-- there's several things this doesn't do that it probably could/should:
 
+
-- * supporting mixed lists (currently it assumes that whatever list type is used by the first item is the same as all other items)
    args[2] = #links == 1 and args[2]
+
-- ** similarly, supporting sublists
 
+
-- * allowing multiple items to be linked/formatted via multiple parameters, instead of just via a list in the first parameter
    nolink = mw.ustring.find( args[1], zwnj ) == 1
+
-- * supporting multiline link syntax (probably most likely from SMW annotations with multiline values, though file embeds can have multiline descriptions too)
    listmarkup = #links == 1 and ''
+
-- * allowing text to be removed from the link for display (e.g. excluding the dabtag in `Dark Magician (manga)`)
 
+
function p.link( frame )
    for i=1, #links do
+
local args = frame == mw.getCurrentFrame() and frame:getParent().args or frame
        el = mw.text.trim( links[i] )
 
 
 
        -- catch empty string at the start of lists
 
        if el ~= '' then
 
            if listmarkup ~= '' then
 
                listmarkup = mw.ustring.match( el, '^([*#])' ) or '*'
 
                el = mw.ustring.gsub( el, '^[*#]', '' )
 
            end
 
 
 
            if mw.ustring.find( el, zwnj ) or mw.ustring.find( el, '%[%[' ) or nolink then
 
                el = mw.ustring.gsub( el, '%[%[[Cc]ategory:', '[[:Category:' )
 
                el = mw.ustring.gsub( el, '%[%[[Ff]ile:', '[[:File:' )
 
                el = mw.ustring.gsub( el, '%[%[[Ii]mage:', '[[:Image:' )
 
                links[i] = table.concat( { listmarkup, el }, '' )
 
            else
 
                link = el
 
                text = args[2] or el
 
                formatl = ''
 
                formatr = ''
 
 
 
                link = mw.ustring.gsub( link, '""', '' )
 
                link = mw.ustring.gsub( link, "'''?", '' )
 
 
 
                -- check for formatting that can be moved out of the link entirely
 
                if mw.ustring.find( text, '^""' ) and mw.ustring.find( text, '""$' ) then
 
                    formatl = '"'
 
                    formatr = '"'
 
                    text = mw.ustring.gsub( text, '""', '' )
 
                else
 
                    text = mw.ustring.gsub( text, '""', '"' )
 
                end
 
                if mw.ustring.find( text, "^'''" ) and mw.ustring.find( text, "'''$" ) then
 
                    formatl = formatl .. "'''"
 
                    formatr = "'''" .. formatr
 
                    text = mw.ustring.gsub( text, "^'''", '' )
 
                    text = mw.ustring.gsub( text, "'''$", '' )
 
                end
 
                if mw.ustring.find( text, "^''" ) and mw.ustring.find( text, "''$" ) then
 
                    formatl = formatl .. "''"
 
                    formatr = "''" .. formatr
 
                    text = mw.ustring.gsub( text, "^''", '' )
 
                    text = mw.ustring.gsub( text, "''$", '' )
 
                end
 
 
 
                if mw.ustring.find( link, '^[Cc]ategory:' ) or
 
                        mw.ustring.find( link, '^[Ff]ile:' ) or
 
                        mw.ustring.find( link, '^[Ii]mage:' ) then
 
                    escape = ':'
 
                else
 
                    escape = ''
 
                end
 
  
                if link == text then
+
-- the gsub() is a dumb hack for the encoded apostrophe test case(s)
                    links[i] = table.concat( { listmarkup, formatl, '[[', escape, link, ']]', formatr }, '' )
+
local items = args[1] and args[1]:gsub( '&', '&' ) or ''
                else
+
                    links[i] = table.concat( { listmarkup, formatl, '[[', escape, link, '|', text, ']]', formatr }, '' )
+
-- remove any empty lines/list items from the input
                end
+
local lines = {}
            end
+
for item in mw.text.gsplit( items, '\n' ) do
        end
+
item = mw.text.trim( item:match( '^[*#;:]?(.*)$' ) )
    end
+
if item ~= '' then
 +
table.insert( lines, item )
 +
end
 +
end
  
    links = table.concat( links, '\n' )
+
-- if there's more than one line, grab the list character (with a fallback if there is none)
    links = mw.ustring.gsub( links, zwnj, '' )
+
local listtype = ''
    links = mw.text.trim( links )
+
if #lines > 1 then
    return links
+
listtype = (items:match( '^%s*([*#;:])' ) or '*') .. ' '
 +
end
 +
 +
-- format, link, and return
 +
local prettified_lines = {}
 +
local result = ''
 +
for _, line in ipairs( lines ) do
 +
if line:find( '[[', 1, true ) then
 +
local prettified_line = {}
 +
local post = line
 +
while post:find( '[[', 1, true ) do
 +
local pre, annotation, target, label
 +
if post:match( '^[^%[]*%[%[[^|%]]+::' ) then
 +
-- don't format SMW annotations
 +
pre, annotation = post:match( '^([^%[]*)(%[%[[^%]]+%]%])' )
 +
else
 +
pre, target, label = post:match( '^([^%[]*)%[%[ *([^|%]]+) *|? *([^%]]-) *%]%]' )
 +
end
 +
table.insert( prettified_line, ('%s%s'):format( pre and pre:gsub( '""', '"' ) or '', target and prettify_link( target, label ) or annotation ) )
 +
post = post:match( '%]%](.*)$' )
 +
end
 +
result = ('%s%s'):format( table.concat( prettified_line ), post:gsub( '""', '"' ) )
 +
elseif line:find( '\127', 1, true ) then
 +
-- input is unlinked but has a ref tag
 +
-- \127 (DEL) is the start/end character of strip markers
 +
-- technically this will match any stripped content, not just ref tags
 +
local link, ref = line:match( '^([^\127]+)(\127.*)$' )
 +
result = ('%s%s'):format( prettify_link( link ), ref )
 +
else
 +
result = prettify_link( line )
 +
end
 +
 +
table.insert( prettified_lines, ('%s%s'):format( listtype, result ) )
 +
end
 +
 +
prettified_lines = table.concat( prettified_lines, '\n' )
 +
:gsub( '%[%[[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]:', '[[:Category:' )
 +
:gsub( '%[%[[Ff][Ii][Ll][Ee]:', '[[:File:' )
 +
:gsub( '%[%[[Ii][Mm][Aa][Gg][Ee]:', '[[:File:' )
 +
 +
return prettified_lines
 
end
 
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 )
 
function p.unlink( frame )
    local args = frame:getParent().args
+
local txt = frame == mw.getCurrentFrame() and frame:getParent().args[1] or frame[1]
    args[1] = args[1] or ''
+
return txt and ( txt:match( '%[%[:?(.-)[|%]]' ) or mw.text.trim( txt ) )
    return mw.ustring.match( args[1], '%[%[:?(.-)[|%]]' ) or mw.text.trim( args[1] )
 
 
end
 
end
  
 
return p
 
return p

Latest revision as of 19:07, 23 December 2023

--
-- implements {{Autolink}}, {{Unlink}}, and {{Formatted link}}
--

require('Module:No globals')
local p = {}

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

-- formats a link
local function prettify_link( target, label )
	if not target or target == '' then
		return ''
	end
	
	if not label or label == '' then
		label = target
	end
	
	local plain_target = unprettify( target )
	local plain_label = unprettify( label )
	local prettified_label = label:gsub( '""', '"' ):gsub( '([^&# ])#([^#])', '%1 § %2' ):gsub( '##', '#' )
	
	local link = ''
	if plain_target == prettified_label then
		-- no formatting
		link = ('[[%s]]'):format( plain_target )
	elseif plain_target == plain_label then
		local start, _end = prettified_label:find( plain_target, 1, true )
		if start then
			-- only formatting is on the link boundaries (`''Foo Bar''`)
			link = ('%s[[%s]]%s'):format( prettified_label:sub( 1, start - 1 ), plain_target, prettified_label:sub( _end + 1 ) )
		else
			-- formatting within the link (`''Foo Bar'' (Quux)`)
			link = ('[[%s|%s]]'):format( plain_target, prettified_label )
		end
	else
		if plain_label == prettified_label then
			-- no formatting
			link = ('[[%s|%s]]'):format( plain_target, prettified_label )
		else
			local start, _end = prettified_label:find( plain_label, 1, true )
			if start then
				-- only formatting is on the text boundaries (`[[Foo Bar Baz|''Foo Bar'']]`)
				link = ('%s[[%s|%s]]%s'):format( prettified_label:sub( 1, start - 1 ), plain_target, prettified_label:sub( start, _end ), prettified_label:sub( _end + 1 ) )
			else
				-- formatting within the text (`[[Foo Bar Baz|''Foo'' Bar]]`)
				link = ('[[%s|%s]]'):format( plain_target, prettified_label )
			end
		end
	end
	return link
end

-- there's several things this doesn't do that it probably could/should:
-- * supporting mixed lists (currently it assumes that whatever list type is used by the first item is the same as all other items)
-- ** similarly, supporting sublists
-- * allowing multiple items to be linked/formatted via multiple parameters, instead of just via a list in the first parameter
-- * supporting multiline link syntax (probably most likely from SMW annotations with multiline values, though file embeds can have multiline descriptions too)
-- * allowing text to be removed from the link for display (e.g. excluding the dabtag in `Dark Magician (manga)`)
function p.link( frame )
	local args = frame == mw.getCurrentFrame() and frame:getParent().args or frame

	-- the gsub() is a dumb hack for the encoded apostrophe test case(s)
	local items = args[1] and args[1]:gsub( '&', '&' ) or ''
	
	-- remove any empty lines/list items from the input
	local lines = {}
	for item in mw.text.gsplit( items, '\n' ) do
		item = mw.text.trim( item:match( '^[*#;:]?(.*)$' ) )
		if item ~= '' then
			table.insert( lines, item )
		end
	end

	-- if there's more than one line, grab the list character (with a fallback if there is none)
	local listtype = ''
	if #lines > 1 then
		listtype = (items:match( '^%s*([*#;:])' ) or '*') .. ' '
	end
	
	-- format, link, and return
	local prettified_lines = {}
	local result = ''
	for _, line in ipairs( lines ) do
		if line:find( '[[', 1, true ) then
			local prettified_line = {}
			local post = line
			while post:find( '[[', 1, true ) do
				local pre, annotation, target, label
				if post:match( '^[^%[]*%[%[[^|%]]+::' ) then
					-- don't format SMW annotations
					pre, annotation = post:match( '^([^%[]*)(%[%[[^%]]+%]%])' )
				else
					pre, target, label = post:match( '^([^%[]*)%[%[ *([^|%]]+) *|? *([^%]]-) *%]%]' )
				end
				table.insert( prettified_line, ('%s%s'):format( pre and pre:gsub( '""', '"' ) or '', target and prettify_link( target, label ) or annotation ) )
				post = post:match( '%]%](.*)$' )
			end
			result = ('%s%s'):format( table.concat( prettified_line ), post:gsub( '""', '"' ) )
		elseif line:find( '\127', 1, true ) then
			-- input is unlinked but has a ref tag
			-- \127 (DEL) is the start/end character of strip markers
			-- technically this will match any stripped content, not just ref tags
			local link, ref = line:match( '^([^\127]+)(\127.*)$' )
			result = ('%s%s'):format( prettify_link( link ), ref )
		else
			result = prettify_link( line )
		end
		
		table.insert( prettified_lines, ('%s%s'):format( listtype, result ) )
	end
	
	prettified_lines = table.concat( prettified_lines, '\n' )
		:gsub( '%[%[[Cc][Aa][Tt][Ee][Gg][Oo][Rr][Yy]:', '[[:Category:' )
		:gsub( '%[%[[Ff][Ii][Ll][Ee]:', '[[:File:' )
		:gsub( '%[%[[Ii][Mm][Aa][Gg][Ee]:', '[[:File:' )
		
	return prettified_lines
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 == mw.getCurrentFrame() and frame:getParent().args[1] or frame[1]
	return txt and ( txt:match( '%[%[:?(.-)[|%]]' ) or mw.text.trim( txt ) )
end

return p