Module:Annotated link

This is an old revision of this page, as edited by Fred Gandt (talk | contribs) at 05:30, 22 January 2023 (sleepy now). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

local getShortDesc = require( 'Module:User:Fred Gandt/getShortDescription' ).main
local getArgs = require( 'Module:Arguments' ).getArgs

local function handleFirstLetterCase( short_description, case )
	if not case then
		return short_description
	end
	local case_altered_short_description = mw.ustring.gsub( short_description, '^([^%d])', function( first_char )
		if case == 'lower' then
			return mw.ustring.lower( first_char )
		end
		return mw.ustring.upper( first_char )
	end )
	if case_altered_short_description ~= short_description then
		mw.log( 'tracking categories rule' )
	end
	return case_altered_short_description
end

local function getShortDescription( name, case )
	local short_description = getShortDesc( { args = { name = name } } )
	return handleFirstLetterCase( short_description, case )
end

local function annotatedLink( args )
	local name = args.name
	if not name then
		return '<span style="color:red">ERROR with invocation of [[Module:GetShortDescription]]: a page title MUST be provided</span>'
	end
	
	local dash = args.dash or ' –'
	local display = args.display
	local wedge = args.wedge
	local quote = args.quote
	local abbr = args.abbr
	local case = args.case
	local aka = args.aka
	
	--[[
	
	handling for:
	
	category links
	template links
	modile links
	file links
	etc. etc.
	
	all the namespaces \o/
	
	"but shord escs should only be on..."
	<insert image of batman slapping Robin here>
	
	--]]
	
	local result = '[[' .. name
	
	if display then
		result = result .. '|' .. display
	end
	
	result = result .. ']]'
	
	if quote then
		result = '"' .. result .. '"'
	end
	
	if abbr then
		result = result .. ' (' .. abbr .. ')'
	end
	
	if aka then
		result = result .. ', also known as ' .. aka
	end
	
	if wedge then
		result = result .. ' (' .. wedge .. ')'
	end
	
	local short_description = getShortDescription( name, case )
	if short_description and short_description ~= '' then
		result = result .. dash .. ' ' .. short_description
	end
	
	mw.log( 'result' )
	mw.log( result )
	
	return result
	
end

function p.main( frame )
	local args = getArgs( frame.args )
	if not args then
		return '<span style="color:red">ERROR with invocation of [[Module:AnnotatedLink]]: ALL the things are broken!</span>'
	end
	return annotatedLink( args )
end

return p

--[[

	{{#invoke:User:Fred Gandt/annotatedLink|main |name= |display= |wedge= |quote= |dash= |abbr= |case= |aka= }}
	
	p.main { args = { name = "", display = "", wedge = "", quote = "", dash = "", abbr = "", case = "", aka = "" } }


	{{#invoke:User:Fred Gandt/annotatedLink|main|name=The Partisan}}
	{{#invoke:User:Fred Gandt/annotatedLink|main|name=Author, Author (Star Trek: Voyager)}}
	
	p.main { args = { name = "The Partisan" } }
	p.main { args = { name = "Author, Author (Star Trek: Voyager)" } }
	
	p.main { args = { name = "Confédération Mondiale des Activités Subaquatiques", display = "World Underwater Federation", abbr = "CMAS", aka = "''Confédération Mondiale des Activités Subaquatiques''", case = "lower" } }

--]]