Module:Annotated link

This is an old revision of this page, as edited by Fred Gandt (talk | contribs) at 22:56, 24 January 2023 (Module:Arguments?). 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 getArgs = require( 'Module:Arguments' ).getArgs
local mLang = require( 'Module:Lang' )

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

local function langify( args )
	local lang = args.lang
	local text = args.text
	if not lang or lang == 'en' then
		return text
	end
	return mLang._lang {
		lang,
		text,
		italic = args.italic or '',
		nocat = args.nocat or '',
		size = args.size or '',
		cat = args.cat or '',
		rtl = args.rtl or ''
	}
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 case = args.desc_first_letter_case
	local dash = args.dash or ' –'
	local display = args.display
	local wedge = args.wedge
	local quote = args.quote
	local abbr = args.abbr
	local aka = args.aka
	
	local GSD_args = {
		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 = langify( {
		lang = args.link_lang,
		text = '[[:' .. name .. '|' .. ( display or name ) .. ']]',
		italic = args.link_lang_italic,
		nocat = args.link_lang_nocat,
		size = args.link_lang_size,
		cat = args.link_lang_cat,
		rtl = args.link_lang_rtl
	} )
	
	if quote then
		result = '"' .. result .. '"'
	end
	
	if abbr then
		result = result .. ' (' .. abbr .. ')'
	end
	
	if aka then
		result = result .. ', also known as ' .. langify( {
			lang = args.aka_lang,
			text = aka,
			italic = args.aka_lang_italic,
			nocat = args.aka_lang_nocat,
			size = args.aka_lang_size,
			cat = args.aka_lang_cat,
			rtl = args.aka_lang_rtl
		} )
	end
	
	if wedge then
		result = result .. ', ' .. langify( {
			lang = args.wedge_lang,
			text = wedge,
			italic = args.wedge_lang_italic,
			nocat = args.wedge_lang_nocat,
			size = args.wedge_lang_size,
			cat = args.wedge_lang_cat,
			rtl = args.wedge_lang_rtl
		} )
	end
	
	local short_description = handleFirstLetterCase( getShortDescription( GSD_args ), case )
	if short_description and short_description ~= '' then
		result = result .. dash .. ' ' .. short_description
	end
	
	return result
	
end

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

return p

--[[
	
	{{#invoke:AnnotatedLink|main
	
		|name= |display= |wedge= |quote= |dash= |abbr= |desc_first_letter_case= |aka=
		
		|link_lang= |link_lang_italic= |link_lang_nocat= |link_lang_size= |link_lang_cat= |link_lang_rtl=
		
		|wedge_lang= |wedge_lang_italic= |wedge_lang_nocat= |wedge_lang_size= |wedge_lang_cat= |wedge_lang_rtl=
		
		|aka_lang= |aka_lang_italic= |aka_lang_nocat= |aka_lang_size= |aka_lang_cat= |aka_lang_rtl=
	
		|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 = "", desc_first_letter_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", desc_first_letter_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''", desc_first_letter_case = "lower" } }

--]]

-- p.main { args = { name = "The Partisan", quote = "yes", only = "wikidata", desc_first_letter_case = "upper", wedge = "from the album ''[[Songs from a Room]]''", aka = "La Complainte du partisan", aka_lang = "fr" } }