Module:Tt: Difference between revisions
Appearance
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 | -- Main tooltip wrapper | ||
:addClass('custom-tt-wrapper | local toggle = out:tag('span') | ||
:addClass( | |||
'custom-tt-wrapper mw-collapsible mw-made-collapsible mw-collapsed giw-collapsible ' .. | |||
(args.class or '') | |||
) | |||
if args. | -- 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, | |||
']]', | |||
' ' | |||
) | |||
local cleanText = text | |||
local | :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, ' ') | |||
local cleanText = text | local cleanText = text | ||
:gsub('%[%[ | :gsub('%[%[[^%]|]+|', '') | ||
:gsub('%[%[', '') | :gsub('%[%[', '') | ||
:gsub('%]%]', '') | :gsub('%]%]', '') | ||
-- | text = | ||
'<span class="screen-reader-text">Tooltip for ' .. | |||
cleanText .. | |||
'</span>ⓘ' | |||
end | |||
toggle:attr('data-tt-text', text) | |||
-- Tooltip toggle | |||
toggle | |||
:addClass('mw-collapsible-toggle | :tag('span') | ||
:addClass( | |||
'mw-collapsible-toggle mw-collapsible-toggle-collapsed custom-tt toggle-tooltip' | |||
) | ) | ||
:wikitext(text) | :wikitext(text) | ||
:done() | :done() | ||
-- Tooltip content | -- Tooltip content | ||
toggle | |||
:tag('span') | |||
:addClass('mw-collapsible-content custom-tt') | |||
:css('display', 'none') | |||
:wikitext(tooltip) | |||
:done() | |||
out:wikitext('‍') | out:wikitext('‍') | ||
| 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
| 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 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('‍')
-- 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,
']]',
' '
)
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, ' ')
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('‍')
return out
end
-- Return only the visible text.
function p.CleanTT(tx)
local clean = tx:gsub(
'‍<span class="custom%-tt%-wrapper[^>]-data%-tt%-text="(.-)">.-‍',
'%1'
)
return clean
end
return p