Jump to content

Module:Annotated link

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Fred Gandt (talk | contribs) at 20:10, 21 January 2023 (first draft to test in the wild). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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, '^(.)', 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' )
	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 'a page title must be provided'
	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
	
	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 'something is severely wrong with this module; send help'
	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" } }

--]]