Module:Tt/Draft: Difference between revisions

From Honkai: Nexus Anima Wiki
Jump to navigation Jump to search
Kuhlau (talk | contribs)
No edit summary
Kuhlau (talk | contribs)
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 19: Line 19:
function p._main(args, options)
function p._main(args, options)
local text = args[1]
local text = args[1]
local tooltip = mw.text.decode(args[2], true)
local tooltip = args[2]
local header = lib.isNotEmpty(args.header) and mw.text.decode(args.header, true) or nil
local header = lib.isNotEmpty(args.header) and args.header or nil
local out = mw.html.create()
local out = mw.html.create()
Line 32: Line 32:
end
end
-- Build hover tooltip (desktop only)
-- Build toggle tooltip
-- Now uses a css box, not a title
local toggle = out:tag('span'):addClass('custom-tt-wrapper toggle-tooltip')
local hover = out:tag('span')
hover
:addClass('text-tooltip hover-tooltip')
:wikitext(text)
-- Optional hover text but plain text must be there for gadget
if not options or not options.nohover then
local content = tooltip:tag('span'):addClass('tt-content')
if header then
content:tag('span'):addClass('tt-header'):wikitext(header)
:done()
end
content:wikitext(tooltip)
content:done()
end
hover:allDone()
-- Build toggle tooltip (mobile only)
local toggle = out:tag('span'):addClass('custom-tt-wrapper mobile-only toggle-tooltip')
-- Optional toggleable text but plain text must be there for gadget
-- Optional toggleable text but plain text must be there for gadget
Line 93: Line 75:
local full = tx
local full = tx
local result = tx
local result = tx
for display in string.gmatch(full,
for display, tooltip in string.gmatch(full, '<span class="text%-tooltip hover%-tooltip">(.-)<span class="tt%-content">(.-)</span></span>') do
'<span[^>]*class="[^"]*custom%-tt%-wrapper[^"]*"[^>]*>' ..
local escTooltip = tooltip:gsub("([^%w])", "%%%1")
'<span[^>]*class="[^"]*mw%-collapsible%-toggle[^"]*"[^>]*>(.-)</span>' ..
'<span[^>]*class="[^"]*mw%-collapsible%-content[^"]*"[^>]*>.-</span></span>'
) do
local escDisplay = display:gsub("([^%w])", "%%%1")
local escDisplay = display:gsub("([^%w])", "%%%1")
search_str = '<span class="text%-tooltip hover%-tooltip">(' .. escDisplay .. ')<span class="tt%-content">' .. escTooltip .. '</span></span><span[^>]+><span[^>]+>' .. escDisplay .. '</span><span[^>]+>' .. escTooltip .. '</span></span>'
local search_str =
'<span[^>]*class="[^"]*custom%-tt%-wrapper[^"]*"[^>]*>' ..
'<span[^>]*class="[^"]*mw%-collapsible%-toggle[^"]*"[^>]*>' .. escDisplay .. '</span>' ..
'<span[^>]*class="[^"]*mw%-collapsible%-content[^"]*"[^>]*>.-</span></span>'
result = result:gsub(search_str, display)
result = result:gsub(search_str, display)
end
end

Latest revision as of 18:15, 15 July 2026

Documentation for this module may be created at Module:Tt/Draft/doc

local p = {}
local lib = require('Module:Feature')

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		wrappers = { 'Template:Tt', 'Template:Tt/Draft' }
	})
	args[1] = args[1] or '<i>Missing text</i>'

	-- Return plain text if no tooltip given
	if lib.isEmpty(args[2]) then
		return args[1]
	end
	
    return p._main(args)
end

function p._main(args, options)
	local text = args[1]
	local tooltip = args[2]
	local header = lib.isNotEmpty(args.header) and args.header or nil
	local out = mw.html.create()
	
	--check for link format
	if lib.isNotEmpty(args.link) then
		local link = tostring(args.link)
		args.link = nil
		local base = tostring(p._main(args, {notoggle = true})) -- tt on label for hover as otherwise the <a> default title takes priority
		local linkTo = '[[' .. lib.ternary(link == '1', text, link) .. '|' .. base .. ']]'
		return p._main({ linkTo, args[2], header = args.header }, {nohover = true}) -- external tt for collapsible for the separated clickable
	end
	
	-- Build toggle tooltip
	local toggle = out:tag('span'):addClass('custom-tt-wrapper toggle-tooltip')
	
	-- Optional toggleable text but plain text must be there for gadget
	if not options or not options.notoggle then
		toggle:addClass(' mw-collapsible mw-made-collapsible mw-collapsed hnaw-collapsible')
		-- move tooltip to detached icon when text is clickable
		if lib.isNotEmpty(args.link) or (text:find('%[%[') and text:find('%]%]')) then
			toggle:wikitext(text, '&thinsp;')
			-- infoicon with semi-hidden text for screen readers
			text = '<span class="screen-reader-text">Tooltip for ' .. text:gsub('%[%[?[^|%]]+|', ''):gsub('%[', ''):gsub('%]', '') .. '</span>&#9432;'
		end
		
		toggle
			:tag('span')
				:addClass('mw-collapsible-toggle mw-collapsible-toggle-default mw-collapsible-toggle-collapsed custom-tt toggle-tooltip')
				:wikitext(text)
				:done()
		local collapsibleContent = toggle
			:tag('span')
				:addClass('mw-collapsible-content custom-tt')
				:css{ display = 'none' }
		if header then
			collapsibleContent
				:tag('span')
					:addClass('tt-header')
					:wikitext(header)
					:done()
		end
		collapsibleContent:wikitext(tooltip)
		collapsibleContent:done()
	else
		toggle:wikitext(text)
	end
	toggle:allDone()
	
	return out
end

--for modules that need the plain text input
function p.CleanTT(tx)
	local full = tx
	local result = tx
	for display in string.gmatch(full,
		'<span[^>]*class="[^"]*custom%-tt%-wrapper[^"]*"[^>]*>' ..
		'<span[^>]*class="[^"]*mw%-collapsible%-toggle[^"]*"[^>]*>(.-)</span>' ..
		'<span[^>]*class="[^"]*mw%-collapsible%-content[^"]*"[^>]*>.-</span></span>'
	) do
		local escDisplay = display:gsub("([^%w])", "%%%1")
		local search_str =
			'<span[^>]*class="[^"]*custom%-tt%-wrapper[^"]*"[^>]*>' ..
			'<span[^>]*class="[^"]*mw%-collapsible%-toggle[^"]*"[^>]*>' .. escDisplay .. '</span>' ..
			'<span[^>]*class="[^"]*mw%-collapsible%-content[^"]*"[^>]*>.-</span></span>'
		result = result:gsub(search_str, display)
	end
	return result
end

return p