feat(lua): use translator/keep_comments: true to prevent lua from erasing pinyin comments

This commit is contained in:
mirtlecn 2024-05-15 11:15:24 +08:00
parent 7d5952d97a
commit 98d18b74df

View File

@ -13,6 +13,7 @@ local M = {}
function M.init(env)
local config = env.engine.schema.config
env.keep_comment = config:get_bool('translator/keep_comments')
local delimiter = config:get_string('speller/delimiter')
if delimiter and #delimiter > 0 and delimiter:sub(1,1) ~= ' ' then
env.delimiter = delimiter:sub(1,1)
@ -122,14 +123,19 @@ function M.func(input, env)
-- cand.comment 是目前输入的词汇的完整拼音
local pinyin = cand.comment:match("^(.-)$")
if pinyin and #pinyin > 0 then
local correction_pinyin = pinyin
if env.delimiter then
pinyin = pinyin:gsub(env.delimiter,' ')
correction_pinyin = correction_pinyin:gsub(env.delimiter,' ')
end
local c = M.corrections[pinyin]
local c = M.corrections[correction_pinyin]
if c and cand.text == c.text then
cand:get_genuine().comment = string.gsub(M.style, "{comment}", c.comment)
else
cand:get_genuine().comment = ""
if env.keep_comment then
cand:get_genuine().comment = string.gsub(M.style, "{comment}", pinyin)
else
cand:get_genuine().comment = ""
end
end
end
yield(cand)