Module:Ruby

From EarthMC
Revision as of 15:07, 4 January 2023 by 181cm (talk | contribs) (fix bug)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

local module = {}
local g_frame
 
function get_arg ( name, plain )
	if g_frame.args[name] and (mw.text.trim(g_frame.args[name]) ~= '' or plain) then
		return mw.text.trim(g_frame.args[name])
	end
end
 
function module.ruby ( frame )
	local parent = frame:getParent()
	if parent and (parent:getTitle() == "Template:Ruby" or parent:getTitle() == "Template:Ruby/Sandbox" or parent:getTitle() == "Template:沙盒") then
		frame = parent
	end
	g_frame = frame
	local args = {
		class = get_arg('class'),
		id = get_arg('id'),
		style = ( get_arg('style') or get_arg('css') ),
		rbclass = get_arg('rbclass'),
		rbid = get_arg('rbid'),
		rbsize = get_arg('rbsize'),
		rtclass = get_arg('rtclass'),
		rtid = get_arg('rtid'),
		rtsize = get_arg('rtsize'),
		rbtext = ( get_arg(1, true) or '{{{1}}}' ),
		rttext = ( get_arg(2, true) or '{{{2}}}' ),
		rblang = get_arg(3),
	}
	args.rtlang = get_arg(4) or args.rblang
	return module._ruby ( args, frame )
end
 
function module._ruby ( args, frame )
	local html = mw.html.create():tag('ruby')
	if args.class then html:addClass( args.class) end
	if args.id then html:attr('id', args.id) end
	if args.style then html:cssText(args.style) end
 
	local rb = html:tag('rb'):attr('data-id', 'template-ruby')
	if args.rbclass then rb:addClass(args.rbclass) end
	if args.rbid then rb:attr('id', args.rbid) end
	if args.rbsize then rb:css('font-size', args.rbsize) end
 
	if args.rblang then
		if frame then 
			rb:wikitext( frame:expandTemplate{ title = 'lang', args = { args.rblang, args.rbtext } } )
		else
			rb:tag('span'):attr('lang', args.rblang):attr('xml:lang', args.rblang):wikitext(args.rbtext)
		end
	else
		rb:wikitext(args.rbtext)
	end
 
	local rt = html:tag('rt')
	if args.rtclass then rt:addClass(args.rtclass) end
	if args.rtid then rt:attr('id', args.rtid) end
	if args.rtsize ~= '' then rt:css('font-size', (args.rtsize or '0.75em') ) end
	if args.rtlang then
		if frame then
			rt:wikitext( frame:expandTemplate{ title = 'lang', args = { args.rtlang, args.rttext } } )
		else
			rt:tag('span'):attr('lang', args.rtlang):attr('xml:lang', args.rtlang):wikitext(args.rttext)
		end
	else
		rt:wikitext(args.rttext)
	end
 
	return tostring ( html )
end
 
return module