2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/frontend/apps/reader/modules/readerdictionary.lua

83 lines
2.7 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local EventListener = require("ui/widget/eventlistener")
local UIManager = require("ui/uimanager")
local DictQuickLookup = require("ui/widget/dictquicklookup")
local Geom = require("ui/geometry")
local Screen = require("ui/screen")
2013-10-18 20:38:07 +00:00
local JSON = require("JSON")
2013-10-22 18:51:29 +00:00
local DEBUG = require("dbg")
local _ = require("gettext")
2013-10-18 20:38:07 +00:00
local ReaderDictionary = EventListener:new{}
function ReaderDictionary:onLookupWord(word, box, highlight)
2014-03-13 13:52:43 +00:00
self.highlight = highlight
self:stardictLookup(word, box)
return true
2013-04-30 10:45:12 +00:00
end
function ReaderDictionary:stardictLookup(word, box)
2014-03-13 13:52:43 +00:00
DEBUG("lookup word:", word, box)
if word then
-- strip punctuation characters around selected word
word = string.gsub(word, "^%p+", '')
word = string.gsub(word, "%p+$", '')
DEBUG("stripped word:", word)
-- escape quotes and other funny characters in word
local std_out = io.popen("./sdcv --utf8-input --utf8-output -nj "..("%q"):format(word), "r")
local results_str = nil
if std_out then results_str = std_out:read("*all") end
--DEBUG("result str:", word, results_str)
local ok, results = pcall(JSON.decode, JSON, results_str)
if ok and results then
DEBUG("lookup result table:", word, results)
self:showDict(word, results, box)
else
-- dummy results
results = {
{
dict = "",
word = word,
definition = _("No definition found."),
}
}
DEBUG("dummy result table:", word, results)
self:showDict(word, results, box)
2014-03-13 13:52:43 +00:00
end
end
end
2013-04-24 14:57:03 +00:00
function ReaderDictionary:showDict(word, results, box)
if results and results[1] then
DEBUG("showing quick lookup window")
2014-03-13 13:52:43 +00:00
UIManager:show(DictQuickLookup:new{
ui = self.ui,
highlight = self.highlight,
dialog = self.dialog,
-- original lookup word
word = word,
2014-03-13 13:52:43 +00:00
results = results,
dictionary = self.default_dictionary,
width = Screen:getWidth() - Screen:scaleByDPI(80),
word_box = box,
-- differentiate between dict and wiki
wiki = self.wiki,
2014-03-13 13:52:43 +00:00
})
end
2013-04-24 14:57:03 +00:00
end
2013-07-21 06:23:54 +00:00
function ReaderDictionary:onUpdateDefaultDict(dict)
2014-03-13 13:52:43 +00:00
DEBUG("make default dictionary:", dict)
self.default_dictionary = dict
return true
2013-07-21 06:23:54 +00:00
end
function ReaderDictionary:onReadSettings(config)
2014-03-13 13:52:43 +00:00
self.default_dictionary = config:readSetting("default_dictionary")
2013-07-21 06:23:54 +00:00
end
2013-12-27 15:18:16 +00:00
function ReaderDictionary:onSaveSettings()
2014-03-13 13:52:43 +00:00
self.ui.doc_settings:saveSetting("default_dictionary", self.default_dictionary)
2013-07-21 06:23:54 +00:00
end
2013-10-18 20:38:07 +00:00
return ReaderDictionary