feat: try to use panvimdoc

This commit is contained in:
JuanZoran
2023-03-09 19:42:41 +08:00
parent 69ac7653bf
commit 0be7ff07b5
15 changed files with 434 additions and 337 deletions

View File

@@ -1,63 +1,74 @@
local baidu = require('Trans').conf.engine.baidu
local appid = baidu.appid
local appPasswd = baidu.appPasswd
local salt = tostring(math.random(bit.lshift(1, 15)))
local uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
local M = {}
-- error('请查看README, 实现在线翻译或者设置将在线翻译设置为false')
local baidu = require('Trans').conf.engine.baidu
local appid = baidu.appid
local app_passwd = baidu.app_passwd
local salt = tostring(math.random(bit.lshift(1, 15)))
local uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
local post = require('Trans.util.curl').POST
local function get_field(word, isEn)
local to = isEn and 'zh' or 'en'
local tmp = appid .. word .. salt .. appPasswd
M.request_headers = function(data)
local tmp = appid .. data.str .. salt .. app_passwd
local sign = require('Trans.util.md5').sumhexa(tmp)
return {
q = word,
from = 'auto',
to = to,
q = data.str,
from = data.from,
to = data.to,
appid = appid,
salt = salt,
sign = sign,
}
end
---返回一个channel
---@param word string
---@return table
return function(word)
local isEn = word:isEn()
local query = get_field(word, isEn)
local result = {}
post(uri, {
data = query,
headers = {
content_type = "application/x-www-form-urlencoded",
},
callback = function(str)
local ok, res = pcall(vim.json.decode, str)
if ok and res and res.trans_result then
result[1] = {
title = { word = word },
[isEn and 'translation' or 'definition'] = res.trans_result[1].dst,
}
M.query = function(data)
data.engine = 'baidu'
if result.callback then
result.callback(result[1])
end
else
result[1] = false
end
end,
})
require('Trans.wrapper.curl').POST {
return result
}
end
return M
-- NOTE :free tts:
-- https://zj.v.api.aa1.cn/api/baidu-01/?msg=我爱你&choose=0&su=100&yd=5
-- 选择转音频的人物女生1 输入0 女生2输入5男生1 输入1男生2 输入2男生3 输入3
-- local post = require('Trans.util.curl').POST
-- ---返回一个channel
-- ---@param word string
-- ---@return table
-- return function(word)
-- local isEn = word:isEn()
-- local query = get_field(word, isEn)
-- local result = {}
-- post(uri, {
-- data = query,
-- headers = {
-- content_type = "application/x-www-form-urlencoded",
-- },
-- callback = function(str)
-- local ok, res = pcall(vim.json.decode, str)
-- if ok and res and res.trans_result then
-- result[1] = {
-- title = { word = word },
-- [isEn and 'translation' or 'definition'] = res.trans_result[1].dst,
-- }
-- if result.callback then
-- result.callback(result[1])
-- end
-- else
-- result[1] = false
-- end
-- end,
-- })
-- return result
-- end
-- -- NOTE :free tts:
-- -- https://zj.v.api.aa1.cn/api/baidu-01/?msg=我爱你&choose=0&su=100&yd=5
-- -- 选择转音频的人物女生1 输入0 女生2输入5男生1 输入1男生2 输入2男生3 输入3

View File

@@ -7,4 +7,7 @@ M.__index = function(t, k)
return engine
end
return setmetatable(M, M)

View File

@@ -1,7 +1,6 @@
local M = {}
local M = { no_wait = true }
local db = require 'sqlite.db'
local util = require("Trans.backend.util")
vim.api.nvim_create_autocmd('VimLeavePre', {
once = true,
callback = function()
@@ -11,45 +10,32 @@ vim.api.nvim_create_autocmd('VimLeavePre', {
end
})
M.query = function(opts)
opts = type(opts) == 'string' and { str = opts } or opts
if opts.is_word == false then return end
M.query = function(data)
if data.is_word == false or data.from == 'zh' then return end
opts.engine = 'offline'
opts.field = opts.field or M.field
opts.path = vim.fn.expand(opts.path or require('Trans').conf.db_path)
opts.formatter = opts.formatter or M.formatter
data.path = vim.fn.expand(data.path or require('Trans').conf.db_path)
data.engine = 'offline'
data.formatter = data.formatter or M.formatter
data.query_field = data.query_field or M.query_field
local dict = db:open(opts.path)
local db_name = opts.db_name or 'stardict'
local dict = db:open(data.path)
local db_name = data.db_name or 'stardict'
local res = dict:select(db_name, {
where = { word = opts.str, },
keys = opts.field,
where = { word = data.str, },
keys = data.query_field,
limit = 1,
})[1]
if util.is_English(opts.str) then
opts.from = 'en'
opts.to = 'zh'
else
opts.from = 'zh'
opts.to = 'en'
end
if res then
opts.result = opts.formatter(res)
data.result = data.formatter(res)
end
return opts
return data
end
M.nowait = true
M.field = {
M.query_field = {
'word',
'phonetic',
'definition',
@@ -61,10 +47,13 @@ M.field = {
'exchange',
}
local exist = function(str)
return str and str ~= ''
end
local formatter = {
title = function(res)
res.title = {
local title = {
word = res.word,
oxford = res.oxford,
collins = res.collins,
@@ -75,8 +64,10 @@ local formatter = {
res.oxford = nil
res.collins = nil
res.phonetic = nil
return title
end,
tag = function(res)
if not exist(res.tag) then return end
local tag_map = {
zk = '中考',
gk = '高考',
@@ -96,6 +87,7 @@ local formatter = {
return tag
end,
exchange = function(res)
if not exist(res.exchange) then return end
local exchange_map = {
['p'] = '过去式 ',
['d'] = '过去分词 ',
@@ -110,13 +102,14 @@ local formatter = {
}
local exchange = {}
for i, _exchange in ipairs(vim.split(res.exchange, ' ', { plain = true })) do
exchange[i] = exchange_map[_exchange]
for _, _exchange in ipairs(vim.split(res.exchange, '/', { plain = true })) do
exchange[exchange_map[_exchange:sub(1, 1)]] = _exchange:sub(3)
end
return exchange
end,
pos = function(res)
if not exist(res.pos) then return end
local pos_map = {
a = '代词pron ',
c = '连接词conj ',
@@ -134,13 +127,14 @@ local formatter = {
}
local pos = {}
for i, _pos in ipairs(vim.split(res.pos, '/', { plain = true })) do
pos[i] = pos_map[_pos]
for _, _pos in ipairs(vim.split(res.pos, '/', { plain = true })) do
pos[pos_map[_pos:sub(1, 1)]] = _pos:sub(3)
end
return pos
end,
translation = function(res)
if not exist(res.translation) then return end
local translation = {}
for i, _translation in ipairs(vim.split(res.translation, '\n', { plain = true })) do
translation[i] = _translation
@@ -149,6 +143,7 @@ local formatter = {
return translation
end,
definition = function(res)
if not exist(res.definition) then return end
local definition = {}
for i, _definition in ipairs(vim.split(res.definition, '\n', { plain = true })) do
-- -- TODO :判断是否需要分割空格

View File

@@ -1,14 +0,0 @@
local M = {}
M.is_English = function(str)
local char = { str:byte(1, -1) }
for i = 1, #str do
if char[i] > 128 then
return false
end
end
return true
end
return M