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
 
(One intermediate revision by the same user not shown)
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 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 21: Line 23:
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()


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


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


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


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


if isLinked then
cleanText = text
local link = tostring(args.link or '')


-- Visible linked text
-- Handle text that is already an internal link
if link == '1' then
elseif text:find('%[%[') and text:find('%]%]') then
wrapper:wikitext('[[' .. text .. ']] ')
visibleText = text
elseif link ~= '' then
wrapper:wikitext('[[' .. link .. '|' .. text .. ']] ')
else
wrapper:wikitext(text .. ' ')
end


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


-- Store the tooltip label
-- Visible text
wrapper:attr(
out:wikitext(visibleText)
'data-tt-text',
'ⓘ'
)
 
-- Info icon / actual collapsible toggle
wrapper:tag('span')
:addClass('mw-collapsible-toggle')
:addClass('mw-collapsible-toggle-collapsed')
:addClass('custom-tt')
: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')
-- Tooltip trigger
:addClass('mw-collapsible-toggle')
local trigger = out:tag('span')
:addClass('mw-collapsible-toggle-collapsed')
:addClass('custom-tt-trigger')
:addClass('custom-tt')
:attr('tabindex', '0')
:wikitext(text)
:attr('role', 'button')
:done()
:attr('aria-label', 'Tooltip for ' .. cleanText)
end
:wikitext('ⓘ')


-- Tooltip content
-- Tooltip
wrapper:tag('span')
out:tag('span')
:addClass('mw-collapsible-content')
:addClass('custom-tt-content')
:addClass('custom-tt')
:wikitext(tooltip)
:wikitext(tooltip)
:done()
out:wikitext('&zwj;')


return out
return out
Line 103: Line 73:


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