refactor: remove useless code
This commit is contained in:
@@ -4,11 +4,9 @@ local appPasswd = baidu.appPasswd
|
||||
local salt = tostring(math.random(bit.lshift(1, 15)))
|
||||
local uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate'
|
||||
|
||||
if appid == '' or appPasswd == '' then
|
||||
error('请查看README, 实现在线翻译或者设置将在线翻译设置为false')
|
||||
end
|
||||
-- error('请查看README, 实现在线翻译或者设置将在线翻译设置为false')
|
||||
|
||||
local post = require('Trans.util.curl').POST
|
||||
local post = require('Trans.util.curl').POST
|
||||
|
||||
local function get_field(word, isEn)
|
||||
local to = isEn and 'zh' or 'en'
|
||||
@@ -43,7 +41,7 @@ return function(word)
|
||||
if ok and res and res.trans_result then
|
||||
result[1] = {
|
||||
title = { word = word },
|
||||
[isEn and 'translation' or 'definition'] = res.trans_result[1].dst,
|
||||
[isEn and 'translation' or 'definition'] = res.trans_result[1].dst,
|
||||
}
|
||||
|
||||
if result.callback then
|
||||
|
@@ -1,13 +1,10 @@
|
||||
local M = {}
|
||||
|
||||
|
||||
M.__index = function(tbl, k)
|
||||
local res, engine = pcall(require, 'Trans.backend.' .. k)
|
||||
assert(res, [[Can't Found Engine:]] .. k)
|
||||
|
||||
tbl[k] = engine
|
||||
M.__index = function(t, k)
|
||||
local res, engine = pcall(require, [[Trans.backend.]] .. k)
|
||||
assert(res, [[No such Backend: ]] .. k)
|
||||
t[k] = engine
|
||||
return engine
|
||||
end
|
||||
|
||||
|
||||
return setmetatable(M, M)
|
||||
|
@@ -1,12 +1,6 @@
|
||||
local _, db = pcall(require, 'sqlite.db')
|
||||
if not _ then
|
||||
error('Please check out sqlite.lua')
|
||||
end
|
||||
|
||||
-- INFO : init database
|
||||
local path = require('Trans').conf.db_path
|
||||
local dict = db:open(path)
|
||||
local M = {}
|
||||
|
||||
local db = require 'sqlite.db'
|
||||
vim.api.nvim_create_autocmd('VimLeavePre', {
|
||||
once = true,
|
||||
callback = function()
|
||||
@@ -16,32 +10,52 @@ 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
|
||||
|
||||
return function(word)
|
||||
local res = (dict:select('stardict', {
|
||||
where = { word = word, },
|
||||
keys = {
|
||||
'word',
|
||||
'phonetic',
|
||||
'definition',
|
||||
'translation',
|
||||
'pos',
|
||||
'collins',
|
||||
'oxford',
|
||||
'tag',
|
||||
'exchange',
|
||||
},
|
||||
limit = 1,
|
||||
}))[1]
|
||||
|
||||
if res then
|
||||
res.title = {
|
||||
word = res.word,
|
||||
oxford = res.oxford,
|
||||
collins = res.collins,
|
||||
phonetic = res.phonetic,
|
||||
}
|
||||
end
|
||||
local str = opts.str
|
||||
local path = opts.path or require('Trans').conf.db_path
|
||||
local formatter = opts.formatter or M.formatter
|
||||
local field = M.field or M.field
|
||||
|
||||
|
||||
local dict = db:open(path)
|
||||
local db_name = opts.db_name or 'stardict'
|
||||
local res = dict:select(db_name, {
|
||||
where = { word = str, },
|
||||
keys = field,
|
||||
limit = 1,
|
||||
})[1]
|
||||
|
||||
return res and formatter(res) or nil
|
||||
end
|
||||
|
||||
M.nowait = true
|
||||
|
||||
M.field = {
|
||||
'word',
|
||||
'phonetic',
|
||||
'definition',
|
||||
'translation',
|
||||
'pos',
|
||||
'collins',
|
||||
'oxford',
|
||||
'tag',
|
||||
'exchange',
|
||||
}
|
||||
|
||||
|
||||
M.formatter = function(res)
|
||||
res.title = {
|
||||
word = res.word,
|
||||
oxford = res.oxford,
|
||||
collins = res.collins,
|
||||
phonetic = res.phonetic,
|
||||
}
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
return M
|
||||
|
16
lua/Trans/backend/util.lua
Normal file
16
lua/Trans/backend/util.lua
Normal file
@@ -0,0 +1,16 @@
|
||||
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
|
||||
|
Reference in New Issue
Block a user