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