置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:Photrans

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
local p = {}
local getArgs = require('module:arguments').getArgs

function main(args)
	local rb = args[1]
	local rt = args[2]
	local rt2 = args[3]
	local newrb = ''
	local newrt = ''
	if rt then
		local lrb = mw.ustring.len(rb)
		local lrt = mw.ustring.len(rt)
		local diff = lrt * 0.75 - lrb
		local space = ''
		local important = rt2 and '!important' or ''
		if diff > 0 then
			space = tostring(math.floor(diff / (lrb + 1) * 1000) / 1000)..'em'
			newrb = '<span style="margin-left:'..space..important..';letter-spacing:'..space..important..'">'..rb..'</span>'
			newrt = rt
		else
			space = tostring(math.floor(-diff / (lrt + 1) / 0.75 * 1000) / 1000)..'em'
			newrb = rb
			newrt = '<span style="margin-left:'..space..';letter-spacing:'..space..'">'..rt..'</span>'
		end
	else
		newrb = rb
		newrt = '<span class="photrans-s">'..rb..'</span>'
	end
	return newrb, newrt, rt2
end

function p.one(frame)
	local args = getArgs(frame)
	local rb = args[1] or ''
	local rt = args[2] or '&nbsp;'
	return '<ruby><rb>'..rb..'</rb><rt'..(args[2] and '>' or ' class="photrans-s">')..
		(args.on and rt or '<span>'..rt..'</span>')..'</rt></ruby>'
end

function p.multi(frame)
	local args = getArgs(frame)
	local fullrb = ''
	local fullrt = ''
	local flag = false
	local fullrt2 = '<span class="photrans-off">'
	for k, v in ipairs(args) do
		local eachArg = mw.text.split(v, '_')
		if args.on then
			eachArg[3] = true
		end
		local newrb, newrt, rt2 = main(eachArg)
		fullrb = fullrb..newrb
		fullrt = fullrt..newrt
		flag = flag or rt2
		fullrt2 = fullrt2..(rt2 and newrt or '<span class="photrans-s">'..eachArg[1]..'</span>')
	end
	fullrt2 = fullrt2..'</span>'
	return '<ruby><rb>'..fullrb..'</rb><rt><span>'..fullrt..'</span>'..(flag and fullrt2 or '')..'</rt></ruby>'
end

return p