Jump to content

Module:Tt: Difference between revisions

From The Petit Planet Wiki
ReisuDesign (talk | contribs)
No edit summary
ReisuDesign (talk | contribs)
No edit summary
Line 11: Line 11:
if args.clean then
if args.clean then
return p.CleanTT(args[1])
return p.CleanTT(args[1])
elseif not args[2] or args[2] == '' then
elseif not args[2] or mw.text.trim(args[2]) == '' then
return args[1]
return args[1]
else
else
Line 21: Line 21:
local text = args[1]
local text = args[1]
local tooltip = mw.text.decode(args[2], true)
local tooltip = mw.text.decode(args[2], true)
local out = mw.html.create()
local out = mw.html.create()


-- Prevent whitespace/formatting issues around the wrapper
out:wikitext('‍')
out:wikitext('‍')


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


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


local isLinked = (
toggle:wikitext(
(args.link and args.link ~= '')
'[[',
or (text:find('%[%[') and text:find('%]%]'))
link == '1' and text or link,
)
'|',
text,
']]',
' '
)


if isLinked then
local cleanText = text
local link = tostring(args.link or '')
:gsub('%[%[[^%]|]+|', '')
:gsub('%[%[', '')
:gsub('%]%]', '')
 
text =
'<span class="screen-reader-text">Tooltip for ' ..
cleanText ..
'</span>ⓘ'


-- Visible linked text
-- If the supplied text is already an internal link,
if link == '1' then
-- keep it as visible text and put the toggle on the icon.
wrapper:wikitext('[[' .. text .. ']]&thinsp;')
elseif text:find('%[%[') and text:find('%]%]') then
elseif link ~= '' then
toggle:wikitext(text, '&thinsp;')
wrapper:wikitext('[[' .. link .. '|' .. text .. ']]&thinsp;')
else
wrapper:wikitext(text .. '&thinsp;')
end


-- Clean text for screen readers
local cleanText = text
local cleanText = text
:gsub('%[%[?[^|%]]+|', '')
:gsub('%[%[[^%]|]+|', '')
:gsub('%[%[', '')
:gsub('%[%[', '')
:gsub('%]%]', '')
:gsub('%]%]', '')


-- Store the tooltip label
text =
wrapper:attr(
'<span class="screen-reader-text">Tooltip for ' ..
'data-tt-text',
cleanText ..
'ⓘ'
'</span>ⓘ'
)
end
 
toggle:attr('data-tt-text', text)


-- Info icon / actual collapsible toggle
-- Tooltip toggle
wrapper:tag('span')
toggle
:addClass('mw-collapsible-toggle')
:tag('span')
:addClass('mw-collapsible-toggle-collapsed')
:addClass(
:addClass('custom-tt')
'mw-collapsible-toggle mw-collapsible-toggle-collapsed custom-tt toggle-tooltip'
:attr('aria-label', 'Tooltip for ' .. cleanText)
:wikitext(
'<span class="screen-reader-text">Tooltip for '
.. cleanText
.. '</span>ⓘ'
)
)
:done()
else
-- Plain text itself is the toggle
wrapper:attr('data-tt-text', text)
wrapper:tag('span')
:addClass('mw-collapsible-toggle')
:addClass('mw-collapsible-toggle-collapsed')
:addClass('custom-tt')
:wikitext(text)
:wikitext(text)
:done()
:done()
end


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


out:wikitext('&zwj;')
out:wikitext('&zwj;')
Line 102: Line 98:
end
end


-- Return only the visible text.
function p.CleanTT(tx)
function p.CleanTT(tx)
local clean = tx:gsub(
local clean = tx:gsub(

Revision as of 22:35, 1 September 2026

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