2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/apps/reader/modules/readerdictionary.lua

146 lines
4.7 KiB
Lua
Raw Normal View History

local InputContainer = require("ui/widget/container/inputcontainer")
2013-10-18 20:38:07 +00:00
local DictQuickLookup = require("ui/widget/dictquicklookup")
local InfoMessage = require("ui/widget/infomessage")
local UIManager = require("ui/uimanager")
local Screen = require("device").screen
local Device = require("device")
local JSON = require("json")
2013-10-22 18:51:29 +00:00
local DEBUG = require("dbg")
local _ = require("gettext")
local T = require("ffi/util").template
local ReaderDictionary = InputContainer:new{
data_dir = nil,
}
function ReaderDictionary:init()
self.ui.menu:registerToMainMenu(self)
if Device:isAndroid() then
self.data_dir = "/sdcard/koreader/data/dict"
else
self.data_dir = os.getenv("STARDICT_DATA_DIR") or "data/dict"
end
end
function ReaderDictionary:addToMainMenu(tab_item_table)
table.insert(tab_item_table.plugins, {
text = _("Dictionary lookup"),
tap_input = {
2014-11-14 22:04:27 +00:00
title = _("Enter a word to look up"),
type = "text",
callback = function(input)
self:onLookupWord(input)
end,
},
})
end
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
local function tidy_markup(results)
2014-10-28 07:57:01 +00:00
local cdata_tag = "<!%[CDATA%[(.-)%]%]>"
local format_escape = "&[29Ib%+]{(.-)}"
for _, result in ipairs(results) do
local def = result.definition
-- preserve the <br> tag for line break
def = def:gsub("<[bB][rR] ?/?>", "\n")
2014-10-28 07:57:01 +00:00
-- parse CDATA text in XML
if def:find(cdata_tag) then
def = def:gsub(cdata_tag, "%1")
2014-10-28 07:57:01 +00:00
-- ignore format strings
while def:find(format_escape) do
def = def:gsub(format_escape, "%1")
end
end
-- ignore all markup tags
def = def:gsub("%b<>", "")
result.definition = def
2014-10-28 07:57:01 +00:00
end
return results
end
function ReaderDictionary:stardictLookup(word, box)
2014-03-13 13:52:43 +00:00
DEBUG("lookup word:", word, box)
if word then
word = require("util").stripePunctuations(word)
2014-03-13 13:52:43 +00:00
DEBUG("stripped word:", word)
-- escape quotes and other funny characters in word
local results_str = nil
if Device:isAndroid() then
local A = require("android")
results_str = A.stdout("./sdcv", "--utf8-input", "--utf8-output",
"-nj", word, "--data-dir", self.data_dir)
else
local std_out = io.popen("./sdcv --utf8-input --utf8-output -nj "
.. ("%q"):format(word) .. " --data-dir " .. self.data_dir, "r")
if std_out then
results_str = std_out:read("*all")
std_out:close()
end
end
--DEBUG("result str:", word, results_str)
local ok, results = pcall(JSON.decode, results_str)
if ok and results then
--DEBUG("lookup result table:", word, results)
self:showDict(word, tidy_markup(results), box)
else
DEBUG("JSON data cannot be decoded", results)
-- 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")
self.dict_window = DictQuickLookup:new{
2014-03-13 13:52:43 +00:00
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:scaleBySize(80),
word_box = box,
-- differentiate between dict and wiki
wiki = self.wiki,
}
UIManager:show(self.dict_window)
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
UIManager:show(InfoMessage:new{
text = T(_("%1 is now the default dictionary for this document."), dict),
timeout = 2,
})
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-10-28 07:58:04 +00:00
DEBUG("save default dictionary", self.default_dictionary)
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