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-10-15 10:01:58 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2013-12-26 14:40:40 +00:00
|
|
|
local Geom = require("ui/geometry")
|
2013-10-22 15:11:31 +00:00
|
|
|
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")
|
2014-07-24 14:11:25 +00:00
|
|
|
local _ = require("gettext")
|
2013-04-23 22:59:52 +00:00
|
|
|
|
2014-10-15 10:01:58 +00:00
|
|
|
local ReaderDictionary = InputContainer:new{}
|
|
|
|
|
|
|
|
function ReaderDictionary:init()
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderDictionary:addToMainMenu(tab_item_table)
|
|
|
|
table.insert(tab_item_table.plugins, {
|
|
|
|
text = _("Dictionary lookup"),
|
|
|
|
tap_input = {
|
|
|
|
title = _("Input word to lookup"),
|
|
|
|
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-28 07:57:01 +00:00
|
|
|
local function tidyCDATA(results)
|
|
|
|
local cdata_tag = "<!%[CDATA%[(.-)%]%]>"
|
|
|
|
local format_escape = "&[29Ib%+]{(.-)}"
|
|
|
|
for _, result in ipairs(results) do
|
|
|
|
-- parse CDATA text in XML
|
|
|
|
if result.definition:find(cdata_tag) then
|
|
|
|
local def = result.definition:gsub(cdata_tag, "%1")
|
|
|
|
-- ignore all tags
|
|
|
|
def = def:gsub("%b<>", "")
|
|
|
|
-- ignore format strings
|
|
|
|
while def:find(format_escape) do
|
|
|
|
def = def:gsub(format_escape, "%1")
|
|
|
|
end
|
|
|
|
result.definition = def
|
|
|
|
end
|
|
|
|
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
|
2014-10-05 06:32:12 +00:00
|
|
|
-- strip ASCII punctuation characters around selected word
|
|
|
|
-- and strip any generic punctuation (U+2000 - U+206F) in the word
|
|
|
|
word = word:gsub("\226[\128-\131][\128-\191]",''):gsub("^%p+",''):gsub("%p+$",'')
|
2014-03-13 13:52:43 +00:00
|
|
|
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
|
2014-07-24 14:11:25 +00:00
|
|
|
--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)
|
2014-10-28 07:57:01 +00:00
|
|
|
self:showDict(word, tidyCDATA(results), box)
|
2014-07-24 14:11:25 +00:00
|
|
|
else
|
|
|
|
-- 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-03-13 13:52:43 +00:00
|
|
|
UIManager:show(DictQuickLookup:new{
|
|
|
|
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,
|
|
|
|
width = Screen:getWidth() - Screen:scaleByDPI(80),
|
2014-08-20 06:41:45 +00:00
|
|
|
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
|
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
|