Skip to content

Commit

Permalink
feat: add escape filter
Browse files Browse the repository at this point in the history
support escape chars `\n`, `\t`, and `\\`
  • Loading branch information
amorphobia committed Sep 6, 2024
1 parent 0b458a7 commit 717c2a6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
4 changes: 2 additions & 2 deletions dicts/cizu_raw.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54589,7 +54589,7 @@
迥然 jyrf iu 1000
春雨如油 jyry vv 1000
超音速 jys vov 1000
床前明月光,疑是地上霜。举头望明月,低头思故乡。 jys voi 899 1 《静夜思》
床前明月光,疑是地上霜。\n举头望明月,低头思故乡。 jys voi 899 1 《静夜思》
交易所 jys oou 899
解压缩 jys uva 898
窘色 jyse ou 1000
Expand Down Expand Up @@ -117540,7 +117540,7 @@
源自于 yzy auv 1000
又怎样 yzy auv 899
要怎样 yzy vuv 898
慈母手中线,游子身上衣。临行密密缝,意恐迟迟归。谁言寸草心,报得三春晖。 yzy aa 798 1 《游子吟》
慈母手中线,游子身上衣。\n临行密密缝,意恐迟迟归。\n谁言寸草心,报得三春晖。 yzy aa 798 1 《游子吟》
越做越 yzy viv 797
药引 yzyb ia 1000
要有 yzyd vv 1000
Expand Down
1 change: 1 addition & 0 deletions schema/jiandao.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ engine:
- simplifier@emoji_suggestion
- uniquifier
- lua_filter@*jiandao/hint_filter
- lua_filter@*jiandao/escape_filter
- simplifier@traditionalization
- reverse_lookup_filter@danzi_lookup

Expand Down
29 changes: 29 additions & 0 deletions schema/lua/jiandao/escape_filter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--[[
Unicode Translator
Copyright (C) 2024 Xuesong Peng <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
--]]

local function filter(input)
for cand in input:iter() do
local text = string.gsub(cand.text, "\\n", "\n")
text = string.gsub(text, "\\t", "\t")
text = string.gsub(text, "\\\\", "\\")
local comment = cand:get_genuine().comment
yield(Candidate(cand.type, cand._start, cand._end, text, comment))
end
end

return filter
2 changes: 1 addition & 1 deletion schema/lua/jiandao/hint_filter.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--[[
Hint Filter
Copyright (C) 2020 Rea <[email protected]>
Copyright (C) 2021, 2023 Xuesong Peng <[email protected]>
Copyright (C) 2021, 2023 - 2024 Xuesong Peng <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
Expand Down
7 changes: 1 addition & 6 deletions schema/lua/jiandao/unicode_translator.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--[[
Unicode Translator
Copyright (C) 2023 Xuesong Peng <[email protected]>
Copyright (C) 2023 - 2024 Xuesong Peng <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
Expand All @@ -17,17 +17,12 @@
--]]

local function translator(input, seg)
local is_win = package.config:sub(1,1) == "\\"
local delimiter = string.find(input, "u`")
if delimiter ~= nil then
local input_code = string.sub(input, delimiter + 2)
local codepoint = tonumber(input_code, 16)
if codepoint ~= nil then
local ch = utf8.char(codepoint)
-- to prevent software crashing on Windows
if is_win and codepoint == 10 then
ch = "LF"
end
local cand = Candidate("unicode", seg.start, seg._end, ch, " Unicode")
-- input_code = string.format("%04s", input_code)
-- string.format not working in Hamster
Expand Down

0 comments on commit 717c2a6

Please sign in to comment.