Module:Tt: Difference between revisions
Appearance
ReisuDesign (talk | contribs) No edit summary |
ReisuDesign (talk | contribs) No edit summary |
||
| Line 25: | Line 25: | ||
out:wikitext('‍') | out:wikitext('‍') | ||
local | local wrapper = out:tag('span') | ||
:addClass('custom-tt-wrapper') | :addClass('custom-tt-wrapper') | ||
:addClass('toggle-tooltip') | |||
:addClass('mw-collapsible') | :addClass('mw-collapsible') | ||
:addClass('mw-collapsed') | :addClass('mw-collapsed') | ||
| Line 32: | Line 33: | ||
if args.class and args.class ~= '' then | if args.class and args.class ~= '' then | ||
wrapper:addClass(args.class) | |||
end | end | ||
local isLinked = ( | |||
(args.link and args.link ~= '') | |||
or (text:find('%[%[') and text:find('%]%]')) | |||
) | |||
if isLinked then | |||
local link = tostring(args.link or '') | local link = tostring(args.link or '') | ||
-- Visible linked text | |||
if link == '1' then | if link == '1' then | ||
wrapper:wikitext('[[' .. text .. ']] ') | |||
elseif link ~= '' then | elseif link ~= '' then | ||
wrapper:wikitext('[[' .. link .. '|' .. text .. ']] ') | |||
else | else | ||
wrapper:wikitext(text .. ' ') | |||
end | end | ||
-- Clean text for screen readers | |||
local cleanText = text | local cleanText = text | ||
:gsub('%[%[?[^|%]]+|', '') | :gsub('%[%[?[^|%]]+|', '') | ||
| Line 52: | Line 59: | ||
:gsub('%]%]', '') | :gsub('%]%]', '') | ||
text | -- Store the tooltip label | ||
'<span class="screen-reader-text">Tooltip for ' | wrapper:attr( | ||
'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') | |||
:addClass('mw-collapsible-toggle') | |||
:addClass('mw-collapsible-toggle-collapsed') | |||
:addClass('custom-tt') | |||
:wikitext(text) | |||
:done() | |||
end | end | ||
-- Tooltip content | |||
wrapper:tag('span') | |||
:addClass('mw-collapsible-content') | :addClass('mw-collapsible-content') | ||
:addClass('custom-tt') | :addClass('custom-tt') | ||
Revision as of 22:26, 1 September 2026
| Main | Documentation | Usage |
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('‍')
local wrapper = 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
wrapper:addClass(args.class)
end
local isLinked = (
(args.link and args.link ~= '')
or (text:find('%[%[') and text:find('%]%]'))
)
if isLinked then
local link = tostring(args.link or '')
-- Visible linked text
if link == '1' then
wrapper:wikitext('[[' .. text .. ']] ')
elseif link ~= '' then
wrapper:wikitext('[[' .. link .. '|' .. text .. ']] ')
else
wrapper:wikitext(text .. ' ')
end
-- Clean text for screen readers
local cleanText = text
:gsub('%[%[?[^|%]]+|', '')
:gsub('%[%[', '')
:gsub('%]%]', '')
-- Store the tooltip label
wrapper:attr(
'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')
:addClass('mw-collapsible-toggle')
:addClass('mw-collapsible-toggle-collapsed')
:addClass('custom-tt')
:wikitext(text)
:done()
end
-- Tooltip content
wrapper:tag('span')
:addClass('mw-collapsible-content')
:addClass('custom-tt')
:wikitext(tooltip)
:done()
out:wikitext('‍')
return out
end
function p.CleanTT(tx)
local clean = tx:gsub(
'‍<span class="custom%-tt%-wrapper[^>]-data%-tt%-text="(.-)">.-‍',
'%1'
)
return clean
end
return p