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

Module:Sandbox/渚 花/オシミズ

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
local p = {}
-- [] 代码能看就行,不大追求效率了
-- [] 测试完毕
local escapeTable = { -- []转义
	['@'] = '\0HAT\0',
	['@'] = '\0FAT\0',
	['('] = '\0HPL\0',
	[')'] = '\0HPR\0',
	['('] = '\0FPL\0',
	['('] = '\0FPR\0',
}

local unescapeTable = {} -- []反转义
for k, v in pairs(escapeTable) do
	unescapeTable[v] = k 
end


function p._main(args, ruby)
	local ugsub = mw.ustring.gsub
	local format = string.format

	local sequence = {}

	for i, arg in ipairs(args) do
		-- @转义
		escaped = ugsub(arg, '[@@](.)', escapeTable)
		local word, accent, particle

		-- 分离成分
		word, accent, particle = mw.ustring.match(escaped, '^(.-)[@@](.)(.*)$')
		word = word or escaped
		-- [注释于2023/11/9 15:28] assert(not accent or tonumber(accent), '参数“' .. arg .. '”格式有误')
		-- if (not accent or tonumber(accent)) then
		--if (particle and (not tonumber(accent))) then
		if (accent and (not tonumber(accent))) then
			local temp	= particle
			particle	= accent .. temp
		--		particle = accent .. particle
				accent   = nil
		end
		
		-- ruby
		word = ugsub(word, '%b()', function (s)
				return '(' .. mw.ustring.sub(s, 2, -2) .. ')'
			end)
		word = ugsub(word, '(.-)(%b())', function (rb, rt)
				return rt == '()' and rb or ruby{ rb, string.sub(rt, 2, -2) }
			end)

		-- HTML
		if (not accent) and (not particle) then -- 没有注音和助词
			-- sequence[i] = format('<span class="oshimizu-raw">%s</span>', word)
			sequence[i] = '<span class="oshimizu-word">' .. word .. '</span>'
		elseif (not accent) and particle then  -- 有助词没有注音
			sequence[i] ='<span class="oshimizu-span">'						..	
							'<span class="oshimizu-word>'					.. 
								word										.. 
							'</span>'										..
							'<span class="oshimizu-particle">'      		.. 
								particle									..
							'</span>'										..
						 '</span>'
		elseif accent and (not particle) then   -- 有注音没助词
			sequence[i] = '<span class="oshimizu-span oshimizu-word">'	.. 
							word										..
							'<span class="oshiizu-accent">'		    	.. 
								accent									..
							'</span>'									..
						  '</span>'
		else                                     -- 既有注音也有助词
			sequence[i] = '<span class="oshimizu-span">'				..
							'<span class="oshimizu-word">'				..
								word									..
								'<span class="oshimizu-accent">'		..
									accent								..
								'</span>'								..
							'</span>'									..
							'<span class="oshimizu-particle">'			..
								particle								..
							'</span>'									..
						   '</span>'
		end
		
		-- HTML
		--if not accent then
		--	sequence[i] = '<span class="oshimizu-raw">' .. word .. '</span>'
		--elseif particle == '' then
		--	sequence[i] = format('<span class="oshimizu-span oshimizu-word">%s<span class="oshimizu-accent">%s</span></span>', word, accent)
		--else
		--	sequence[i] = format('<span class="oshimizu-span"><span class="oshimizu-word">%s<span class="oshimizu-accent">%s</span></span><span class="oshimizu-particle">%s</span></span>', word, accent, particle)
		--end
	end

	return '<span lang="ja">-{' .. ugsub(table.concat(sequence), '(%z...%z)', unescapeTable) .. '}-</span>'
end


function p.main(frame)
	local parent = frame:getParent()
	if parent and parent:getTitle() == 'User:渚 花/オシミズ' then
		frame = parent
	end
	local args = {}
	local trim = mw.text.trim
	for k, v in pairs(frame.args) do
		v = trim(v)
		if v ~= '' then args[k] = v end
	end
	return p._main(args, function (t) return frame:expandTemplate{ title = 'Ruby', args = t } end)
end

return p