Module:Annotated link

This is an old revision of this page, as edited by Fred Gandt (talk | contribs) at 18:42, 24 January 2023 (able to pass all args to Module:GetShortDescription; language handling is gonna be a lot of fun). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

local getShortDescription = require( 'Module:GetShortDescription' ).main
local mLang = require( 'Module:Lang' )

local function errorMessage( message )
	return '<strong class="error">ERROR with invocation of [[Module:AnnotatedLink]]: ' .. message .. '</strong>'
end

local function safePipedLink( name, display )
	return '[[:' .. name .. '|' .. ( display or name ) .. ']]'
end

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 annotatedLink( args )
	local name = args.name
	if not name then
		return errorMessage( 'a page name (including namespace) 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 GSDargs = {
		args = {
			lang_italic = args.desc_lang_italic,
			lang_nocat = args.desc_lang_nocat,
			lang_size = args.desc_lang_size,
			lang_cat = args.desc_lang_cat,
			lang_rtl = args.desc_lang_rtl,
			lang_no = args.desc_lang_no,
			fallback = args.fallback,
			prefer = args.prefer,
			only = args.only,
			name = name
		}
	}
	
	local result = safePipedLink( name, display )
	
	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 = handleFirstLetterCase( getShortDescription( GSDargs ), case )
	if short_description and short_description ~= '' then
		result = result .. dash .. ' ' .. short_description
	end
	
	return result
	
end

function p.main( frame )
	local args = frame.args
	local annotated_link = annotatedLink( args )
	mw.log( annotated_link )
	return annotated_link
end

return p

--[[
	
	{{#invoke:AnnotatedLink|main
	
	|name= |display= |wedge= |quote= |dash= |abbr= |case= |aka= |name= |only= |prefer= |fallback=
	
	|desc_lang_italic= |desc_lang_nocat= |desc_lang_size= |desc_lang_cat= |desc_lang_rtl= |desc_lang_no=
	
	}}
	
	p.main { args = { name = "", display = "", wedge = "", quote = "", dash = "", abbr = "", case = "", aka = "" } }
	
	p.main { args = { name = "The Partisan" } }
	p.main { args = { name = "The Partisan", quote = "yes" } }
	p.main { args = { name = "The Partisan", quote = "yes", only = "wikidata", case = "upper" } }
	
	p.main { args = { name = "Author, Author (Star Trek: Voyager)" } }
	p.main { args = { name = "Author, Author (Star Trek: Voyager)", display = "Author, Author" } }
	p.main { args = { name = "Author, Author (Star Trek: Voyager)", display = "Author, Author", wedge = "''(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" } }

--]]