2014-10-15 10:01:58 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local DictQuickLookup = require("ui/widget/dictquicklookup")
|
2014-11-28 13:20:38 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
2015-10-03 06:18:47 +00:00
|
|
|
local DataStorage = require("datastorage")
|
2014-10-15 10:01:58 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Screen = require("device").screen
|
2015-04-15 12:28:32 +00:00
|
|
|
local Device = require("device")
|
2015-03-21 03:39:25 +00:00
|
|
|
local JSON = require("json")
|
2013-10-22 18:51:29 +00:00
|
|
|
local DEBUG = require("dbg")
|
2014-07-24 14:11:25 +00:00
|
|
|
local _ = require("gettext")
|
2014-11-28 13:53:42 +00:00
|
|
|
local T = require("ffi/util").template
|
2013-04-23 22:59:52 +00:00
|
|
|
|
2015-04-15 12:28:32 +00:00
|
|
|
local ReaderDictionary = InputContainer:new{
|
|
|
|
data_dir = nil,
|
|
|
|
}
|
2014-10-15 10:01:58 +00:00
|
|
|
|
|
|
|
function ReaderDictionary:init()
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
2015-10-03 06:18:47 +00:00
|
|
|
self.data_dir = os.getenv("STARDICT_DATA_DIR") or
|
|
|
|
DataStorage:getDataDir() .. "/data/dict"
|
2014-10-15 10:01:58 +00:00
|
|
|
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"),
|
2014-10-15 10:01:58 +00:00
|
|
|
type = "text",
|
|
|
|
callback = function(input)
|
|
|
|
self:onLookupWord(input)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
2013-04-23 22:59:52 +00:00
|
|
|
|
2014-08-17 16:32:09 +00:00
|
|
|
function ReaderDictionary:onLookupWord(word, box, highlight)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.highlight = highlight
|
|
|
|
self:stardictLookup(word, box)
|
2014-08-20 06:41:45 +00:00
|
|
|
return true
|
2013-04-30 10:45:12 +00:00
|
|
|
end
|
|
|
|
|
2014-10-29 08:42:00 +00:00
|
|
|
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
|
2014-10-29 08:42:00 +00:00
|
|
|
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
|
2014-10-29 08:42:00 +00:00
|
|
|
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
|
2014-10-29 08:42:00 +00:00
|
|
|
-- ignore all markup tags
|
|
|
|
def = def:gsub("%b<>", "")
|
|
|
|
result.definition = def
|
2014-10-28 07:57:01 +00:00
|
|
|
end
|
|
|
|
return results
|
|
|
|
end
|
|
|
|
|
2013-12-26 14:40:40 +00:00
|
|
|
function ReaderDictionary:stardictLookup(word, box)
|
2014-03-13 13:52:43 +00:00
|
|
|
DEBUG("lookup word:", word, box)
|
|
|
|
if word then
|
2015-02-01 09:40:34 +00:00
|
|
|
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
|
2015-04-22 06:27:05 +00:00
|
|
|
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
|
2014-11-26 09:01:34 +00:00
|
|
|
end
|
2014-07-24 14:11:25 +00:00
|
|
|
--DEBUG("result str:", word, results_str)
|
2015-03-21 03:39:25 +00:00
|
|
|
local ok, results = pcall(JSON.decode, results_str)
|
2014-07-24 14:11:25 +00:00
|
|
|
if ok and results then
|
2014-11-21 10:32:43 +00:00
|
|
|
--DEBUG("lookup result table:", word, results)
|
2014-10-29 08:42:00 +00:00
|
|
|
self:showDict(word, tidy_markup(results), box)
|
2014-07-24 14:11:25 +00:00
|
|
|
else
|
2014-12-03 06:26:00 +00:00
|
|
|
DEBUG("JSON data cannot be decoded", results)
|
2014-07-24 14:11:25 +00:00
|
|
|
-- dummy results
|
|
|
|
results = {
|
|
|
|
{
|
|
|
|
dict = "",
|
|
|
|
word = word,
|
|
|
|
definition = _("No definition found."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG("dummy result table:", word, results)
|
2014-08-20 10:25:37 +00:00
|
|
|
self:showDict(word, results, box)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
2013-04-24 14:57:03 +00:00
|
|
|
|
2014-08-20 10:25:37 +00:00
|
|
|
function ReaderDictionary:showDict(word, results, box)
|
2014-08-17 16:32:09 +00:00
|
|
|
if results and results[1] then
|
2014-08-20 06:41:45 +00:00
|
|
|
DEBUG("showing quick lookup window")
|
2014-11-21 10:32:43 +00:00
|
|
|
self.dict_window = DictQuickLookup:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
ui = self.ui,
|
|
|
|
highlight = self.highlight,
|
|
|
|
dialog = self.dialog,
|
2014-08-20 10:25:37 +00:00
|
|
|
-- original lookup word
|
|
|
|
word = word,
|
2014-03-13 13:52:43 +00:00
|
|
|
results = results,
|
|
|
|
dictionary = self.default_dictionary,
|
2014-11-20 22:07:39 +00:00
|
|
|
width = Screen:getWidth() - Screen:scaleBySize(80),
|
2014-08-20 06:41:45 +00:00
|
|
|
word_box = box,
|
|
|
|
-- differentiate between dict and wiki
|
|
|
|
wiki = self.wiki,
|
2014-11-21 10:32:43 +00:00
|
|
|
}
|
2014-12-01 14:39:41 +00:00
|
|
|
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
|
2014-11-28 13:20:38 +00:00
|
|
|
UIManager:show(InfoMessage:new{
|
2014-11-28 13:53:42 +00:00
|
|
|
text = T(_("%1 is now the default dictionary for this document."), dict),
|
2014-11-28 13:20:38 +00:00
|
|
|
timeout = 2,
|
|
|
|
})
|
2014-08-20 06:41:45 +00:00
|
|
|
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
|