Module:Tt: Difference between revisions
ReisuDesign (talk | contribs) Created page with "local p = {} local lib = require('Module:Feature') 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]) -- Return plain text if tooltip return given elseif lib.isEmpty(args[2]) then return args[1] -- Return plain text if no tooltip given else return p._main(args) end end function p._m..." |
ReisuDesign (talk | contribs) No edit summary |
||
| (5 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p.main(frame) | function p.main(frame) | ||
local args = require('Module:Arguments').getArgs(frame, { | local args = require('Module:Arguments').getArgs(frame, { | ||
parentFirst = true, | parentFirst = true, | ||
wrappers = { 'Template:Tt' } | wrappers = {'Template:Tt'} | ||
}) | }) | ||
args[1] = args[1] or '<i>Missing text</i>' | args[1] = args[1] or '<i>Missing text</i>' | ||
if args.clean then | if args.clean then | ||
return p.CleanTT(args[1]) | return p.CleanTT(args[1]) | ||
end | |||
return args[1] | |||
if not args[2] or mw.text.trim(args[2]) == '' then | |||
return args[1] | |||
end | end | ||
return p._main(args) | |||
end | end | ||
function p._main(args | function p._main(args) | ||
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('span') | |||
:addClass('custom-tt-wrapper') | |||
:addClass(args.class or '') | |||
local visibleText = text | |||
-- | local cleanText = text | ||
if | |||
-- Handle explicit link parameter | |||
if args.link and mw.text.trim(tostring(args.link)) ~= '' then | |||
local link = tostring(args.link) | 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 | 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 | return out | ||
end | end | ||
function p.CleanTT(tx) | function p.CleanTT(tx) | ||
return tx:gsub( | |||
'<span class="custom%-tt%-wrapper[^>]*>.-</span>', | |||
'' | |||
) | |||
end | end | ||
return p | return p | ||