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 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()

	out:wikitext('&zwj;')

	local toggle = out:tag('span')
		:addClass('custom-tt-wrapper')
		:addClass('toggle-tooltip')
		:addClass('mw-collapsible')
		:addClass('mw-collapsed')
		:addClass('giw-collapsible')

	if args.class and args.class ~= '' then
		toggle:addClass(args.class)
	end

	-- Make the text clickable if a link is provided,
	-- or if the text already contains a wiki link.
	if (args.link and args.link ~= '') or (text:find('%[%[') and text:find('%]%]')) then
		local link = tostring(args.link or '')

		if link == '1' then
			toggle:wikitext(
				'[[', text, ']]', '&thinsp;'
			)
		elseif link ~= '' then
			toggle:wikitext(
				'[[', link, '|', text, ']]', '&thinsp;'
			)
		else
			toggle:wikitext(text, '&thinsp;')
		end

		-- Add accessible text and an info icon.
		local cleanText = text
			:gsub('%[%[?[^|%]]+|', '')
			:gsub('%[%[', '')
			:gsub('%]%]', '')

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

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

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

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

	out:wikitext('&zwj;')

	return out
end

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

	return clean
end

return p