Module documentation

This module implements {{Tt}}.

local p = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		wrappers = { 'Template:Tt' }
	})

	args[1] = args[1] or '<i>Missing text</i>'

	if args.clean then
		return p.CleanTT(args[1])
	elseif not args[2] or mw.text.trim(args[2]) == '' then
		return args[1]
	else
		return p._main(args)
	end
end

function p._main(args)
	local text = args[1]
	local tooltip = mw.text.decode(args[2], true)

	local out = mw.html.create()

	-- Prevent whitespace/formatting issues around the wrapper
	out:wikitext('&zwj;')

	-- Main tooltip wrapper
	local toggle = out:tag('span')
		:addClass(
			'custom-tt-wrapper mw-collapsible mw-made-collapsible mw-collapsed giw-collapsible ' ..
			(args.class or '')
		)

	-- If a link is supplied, keep the link outside the tooltip toggle.
	-- The info icon becomes the actual tooltip toggle.
	if args.link and mw.text.trim(tostring(args.link)) ~= '' then
		local link = tostring(args.link)

		toggle:wikitext(
			'[[',
			link == '1' and text or link,
			'|',
			text,
			']]',
			'&thinsp;'
		)

		local cleanText = text
			:gsub('%[%[[^%]|]+|', '')
			:gsub('%[%[', '')
			:gsub('%]%]', '')

		text =
			'<span class="screen-reader-text">Tooltip for ' ..
			cleanText ..
			'</span>ⓘ'

	-- If the supplied text is already an internal link,
	-- keep it as visible text and put the toggle on the icon.
	elseif text:find('%[%[') and text:find('%]%]') then
		toggle:wikitext(text, '&thinsp;')

		local cleanText = text
			:gsub('%[%[[^%]|]+|', '')
			:gsub('%[%[', '')
			:gsub('%]%]', '')

		text =
			'<span class="screen-reader-text">Tooltip for ' ..
			cleanText ..
			'</span>ⓘ'
	end

	toggle:attr('data-tt-text', text)

	-- Tooltip toggle
	toggle
		:tag('span')
			:addClass(
				'mw-collapsible-toggle mw-collapsible-toggle-collapsed custom-tt toggle-tooltip'
			)
			:wikitext(text)
			:done()

	-- Tooltip content
	toggle
		:tag('span')
			:addClass('mw-collapsible-content custom-tt')
			:css('display', 'none')
			:wikitext(tooltip)
			:done()

	out:wikitext('&zwj;')

	return out
end

-- Return only the visible text.
function p.CleanTT(tx)
	local clean = tx:gsub(
		'&zwj;<span class="custom%-tt%-wrapper[^>]-data%-tt%-text="(.-)">.-&zwj;',
		'%1'
	)

	return clean
end

return p