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 4: Line 4:
local args = require('Module:Arguments').getArgs(frame, {
local args = require('Module:Arguments').getArgs(frame, {
parentFirst = true,
parentFirst = true,
wrappers = { 'Template:Tt' }
wrappers = {'Template:Tt'}
})
})


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 mw.text.trim(args[2]) == '' then
end
 
if not args[2] or mw.text.trim(args[2]) == '' then
return args[1]
return args[1]
else
return p._main(args)
end
end
return p._main(args)
end
end


Line 22: Line 24:
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('span')
 
:addClass('custom-tt-wrapper')
-- Prevent whitespace/formatting issues around the wrapper
:addClass(args.class or '')
out:wikitext('‍')


-- Main tooltip wrapper
local visibleText = text
local toggle = out:tag('span')
local cleanText = text
: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.
-- Handle explicit link parameter
-- The info icon becomes the actual tooltip toggle.
if args.link and mw.text.trim(tostring(args.link)) ~= '' then
if args.link and mw.text.trim(tostring(args.link)) ~= '' then
local link = tostring(args.link)
local link = tostring(args.link)


toggle:wikitext(
visibleText = '[[' ..
'[[',
(link == '1' and text or link) ..
link == '1' and text or link,
'|' ..
'|',
text ..
text,
']]'
']]',
' '
)


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


text =
-- Handle text that is already an internal link
'<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
elseif text:find('%[%[') and text:find('%]%]') then
toggle:wikitext(text, '&thinsp;')
visibleText = text


local cleanText = text
cleanText = text
:gsub('%[%[[^%]|]+|', '')
:gsub('%[%[[^%]|]+|', '')
:gsub('%[%[', '')
:gsub('%[%[', '')
:gsub('%]%]', '')
:gsub('%]%]', '')
text =
'<span class="screen-reader-text">Tooltip for ' ..
cleanText ..
'</span>ⓘ'
end
end


toggle:attr('data-tt-text', text)
-- Visible text
out:wikitext(visibleText)


-- Tooltip toggle
-- Tooltip trigger
toggle
local trigger = out:tag('span')
:tag('span')
:addClass('custom-tt-trigger')
:addClass(
:attr('tabindex', '0')
'mw-collapsible-toggle mw-collapsible-toggle-collapsed custom-tt toggle-tooltip'
:attr('role', 'button')
)
:attr('aria-label', 'Tooltip for ' .. cleanText)
:wikitext(text)
:wikitext('ⓘ')
:done()


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


return out
return out
end
end


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


return p
return p

Latest revision as of 22:40, 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])
	end

	if not args[2] or mw.text.trim(args[2]) == '' then
		return args[1]
	end

	return p._main(args)
end

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

	local out = mw.html.create('span')
		:addClass('custom-tt-wrapper')
		:addClass(args.class or '')

	local visibleText = text
	local cleanText = text

	-- Handle explicit link parameter
	if args.link and mw.text.trim(tostring(args.link)) ~= '' then
		local link = tostring(args.link)

		visibleText = '[[' ..
			(link == '1' and text or link) ..
			'|' ..
			text ..
			']]'

		cleanText = text

	-- Handle text that is already an internal link
	elseif text:find('%[%[') and text:find('%]%]') then
		visibleText = text

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

	-- Visible text
	out:wikitext(visibleText)

	-- Tooltip trigger
	local trigger = out:tag('span')
		:addClass('custom-tt-trigger')
		:attr('tabindex', '0')
		:attr('role', 'button')
		:attr('aria-label', 'Tooltip for ' .. cleanText)
		:wikitext('ⓘ')

	-- Tooltip
	out:tag('span')
		:addClass('custom-tt-content')
		:wikitext(tooltip)

	return out
end

function p.CleanTT(tx)
	return tx:gsub(
		'<span class="custom%-tt%-wrapper[^>]*>.-</span>',
		''
	)
end

return p