2017-06-18 16:08:57 +00:00
local ConfirmBox = require ( " ui/widget/confirmbox " )
2015-10-03 06:18:47 +00:00
local DataStorage = require ( " datastorage " )
2015-04-15 12:28:32 +00:00
local Device = require ( " device " )
2017-04-07 13:20:57 +00:00
local DictQuickLookup = require ( " ui/widget/dictquicklookup " )
local InfoMessage = require ( " ui/widget/infomessage " )
2017-06-18 16:08:57 +00:00
local InputContainer = require ( " ui/widget/container/inputcontainer " )
2015-03-21 03:39:25 +00:00
local JSON = require ( " json " )
2017-04-07 13:20:57 +00:00
local UIManager = require ( " ui/uimanager " )
2016-12-29 08:10:38 +00:00
local logger = require ( " logger " )
2016-12-06 21:15:52 +00:00
local util = require ( " util " )
2014-07-24 14:11:25 +00:00
local _ = require ( " gettext " )
2017-04-07 13:20:57 +00:00
local Screen = Device.screen
2014-11-28 13:53:42 +00:00
local T = require ( " ffi/util " ) . template
2013-04-23 22:59:52 +00:00
2017-08-29 10:52:14 +00:00
-- We'll store the list of available dictionaries as a module local
-- so we only have to look for them on the first :init()
local available_ifos = nil
local function getIfosInDir ( path )
2017-08-29 20:27:43 +00:00
-- Get all the .ifo under directory path.
2017-08-29 10:52:14 +00:00
-- We use the same logic as sdcv to walk directories and ifos files
-- (so we get them in the order sdcv queries them) :
-- - No sorting, entries are processed in the order the dir_read_name() call
-- returns them (inodes linked list)
-- - If entry is a directory, Walk in it first and recurse
local ifos = { }
local ok , iter , dir_obj = pcall ( lfs.dir , path )
if ok then
for name in iter , dir_obj do
if name ~= " . " and name ~= " .. " then
local fullpath = path .. " / " .. name
local attributes = lfs.attributes ( fullpath )
if attributes ~= nil then
if attributes.mode == " directory " then
local dirifos = getIfosInDir ( fullpath ) -- recurse
for _ , ifo in pairs ( dirifos ) do
table.insert ( ifos , ifo )
end
2017-08-29 20:27:43 +00:00
elseif fullpath : match ( " %.ifo$ " ) then
table.insert ( ifos , fullpath )
2017-08-29 10:52:14 +00:00
end
end
end
end
end
return ifos
end
2015-04-15 12:28:32 +00:00
local ReaderDictionary = InputContainer : new {
data_dir = nil ,
2016-06-28 16:35:00 +00:00
dict_window_list = { } ,
2016-12-06 21:15:52 +00:00
lookup_msg = _ ( " Searching dictionary for: \n %1 " )
2015-04-15 12:28:32 +00:00
}
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 "
2017-08-29 10:52:14 +00:00
-- Gather info about available dictionaries
if not available_ifos then
available_ifos = { }
logger.dbg ( " Getting list of dictionaries " )
local ifo_files = getIfosInDir ( self.data_dir )
local dict_ext = self.data_dir .. " _ext "
if lfs.attributes ( dict_ext , " mode " ) == " directory " then
local extifos = getIfosInDir ( dict_ext )
for _ , ifo in pairs ( extifos ) do
table.insert ( ifo_files , ifo )
end
end
for _ , ifo_file in pairs ( ifo_files ) do
2017-08-29 20:27:43 +00:00
local f = io.open ( ifo_file , " r " )
2017-08-29 10:52:14 +00:00
if f then
local content = f : read ( " *all " )
f : close ( )
2017-08-29 20:27:43 +00:00
local dictname = content : match ( " \n bookname=(.-) \n " )
2017-08-29 10:52:14 +00:00
-- sdcv won't use dict that don't have a bookname=
if dictname then
table.insert ( available_ifos , {
file = ifo_file ,
name = dictname ,
} )
end
end
end
logger.dbg ( " found " , # available_ifos , " dictionaries " )
2017-08-29 20:27:43 +00:00
if not G_reader_settings : readSetting ( " dicts_disabled " ) then
-- Create an empty dict for this setting, so that we can
-- access and update it directly thru G_reader_settings
-- and it will automatically be saved.
G_reader_settings : saveSetting ( " dicts_disabled " , { } )
end
2017-08-29 10:52:14 +00:00
end
2017-08-29 20:27:43 +00:00
-- Prepare the -u options to give to sdcv if some dictionaries are disabled
self : updateSdcvDictNamesOptions ( )
end
function ReaderDictionary : updateSdcvDictNamesOptions ( )
-- We cannot tell sdcv which dictionaries to ignore, but we
-- can tell it which dictionaries to use, by using multiple
-- -u <dictname> options.
-- (The order of the -u does not matter, and we can not use
-- them for ordering queries and results)
local dicts_disabled = G_reader_settings : readSetting ( " dicts_disabled " )
if not next ( dicts_disabled ) then
-- no dict disabled, no need to use any -u option
self.sdcv_dictnames_options_raw = nil
self.sdcv_dictnames_options_escaped = nil
return
end
local u_options_raw = { } -- for android call (individual unesscaped elements)
local u_options_escaped = { } -- for other devices call via shell
for _ , ifo in pairs ( available_ifos ) do
if not dicts_disabled [ ifo.file ] then
table.insert ( u_options_raw , " -u " )
table.insert ( u_options_raw , ifo.name )
-- Escape chars in dictname so it's ok for the shell command
-- local u_esc = ("-u %q"):format(ifo.name)
-- This may be safer than using lua's %q:
local u_esc = " -u ' " .. ifo.name : gsub ( " ' " , " ' \\ '' " ) .. " ' "
table.insert ( u_options_escaped , u_esc )
end
-- Note: if all dicts are disabled, we won't get any -u, and so
-- all dicts will be queried.
end
self.sdcv_dictnames_options_raw = u_options_raw
self.sdcv_dictnames_options_escaped = table.concat ( u_options_escaped , " " )
2014-10-15 10:01:58 +00:00
end
2017-03-04 13:46:38 +00:00
function ReaderDictionary : addToMainMenu ( menu_items )
menu_items.dictionary_lookup = {
2014-10-15 10:01:58 +00:00
text = _ ( " Dictionary lookup " ) ,
tap_input = {
2014-11-14 22:04:27 +00:00
title = _ ( " Enter a word to look up " ) ,
2017-04-07 13:20:57 +00:00
ok_text = _ ( " Search dictionary " ) ,
2014-10-15 10:01:58 +00:00
type = " text " ,
callback = function ( input )
self : onLookupWord ( input )
end ,
} ,
2017-02-28 21:46:32 +00:00
}
2017-08-22 15:24:31 +00:00
menu_items.dictionary_settings = {
text = _ ( " Dictionary settings " ) ,
sub_item_table = {
2017-08-29 10:52:14 +00:00
{
text_func = function ( )
2017-09-05 05:56:05 +00:00
local nb_available , nb_enabled , nb_disabled = self : getNumberOfDictionaries ( )
2017-08-29 20:27:43 +00:00
local nb_str = nb_available
if nb_disabled > 0 then
nb_str = nb_enabled .. " / " .. nb_available
2017-08-29 10:52:14 +00:00
end
return T ( _ ( " Installed dictionaries (%1) " ) , nb_str )
end ,
2017-09-05 05:56:05 +00:00
enabled_func = function ( )
return self : getNumberOfDictionaries ( ) > 0
end ,
2017-08-29 10:52:14 +00:00
sub_item_table = self : genDictionariesMenu ( ) ,
} ,
{
2017-09-01 18:42:03 +00:00
text = _ ( " Info on dictionary order " ) ,
2017-08-29 10:52:14 +00:00
callback = function ( )
UIManager : show ( InfoMessage : new {
2017-09-01 18:42:03 +00:00
text = T ( _ ( [ [
If you ' d like to change the order in which dictionaries are queried (and their results displayed), you can:
- move all dictionary directories out of % 1.
- move them back there , one by one , in the order you want them to be used . ] ] ) , self.data_dir )
2017-08-29 10:52:14 +00:00
} )
end
} ,
2017-08-22 15:24:31 +00:00
{
text = _ ( " Disable dictionary fuzzy search " ) ,
checked_func = function ( )
return self.disable_fuzzy_search == true
end ,
callback = function ( )
self.disable_fuzzy_search = not self.disable_fuzzy_search
end ,
hold_callback = function ( )
self : makeDisableFuzzyDefault ( self.disable_fuzzy_search )
end ,
} ,
{ -- setting used by dictquicklookup
text = _ ( " Justify text " ) ,
checked_func = function ( )
return G_reader_settings : nilOrTrue ( " dict_justify " )
end ,
callback = function ( )
G_reader_settings : flipNilOrTrue ( " dict_justify " )
end ,
}
}
2017-06-18 16:08:57 +00:00
}
2014-10-15 10:01:58 +00:00
end
2013-04-23 22:59:52 +00:00
2017-09-09 16:30:00 +00:00
function ReaderDictionary : onLookupWord ( word , box , highlight , link )
2014-03-13 13:52:43 +00:00
self.highlight = highlight
2017-09-09 16:30:00 +00:00
self : stardictLookup ( word , box , link )
2014-08-20 06:41:45 +00:00
return true
2013-04-30 10:45:12 +00:00
end
2017-09-05 05:56:05 +00:00
--- Gets number of available, enabled, and disabled dictionaries
-- @treturn int nb_available
-- @treturn int nb_enabled
-- @treturn int nb_disabled
function ReaderDictionary : getNumberOfDictionaries ( )
local nb_available = # available_ifos
local nb_disabled = 0
for _ in pairs ( G_reader_settings : readSetting ( " dicts_disabled " ) ) do
nb_disabled = nb_disabled + 1
end
local nb_enabled = nb_available - nb_disabled
return nb_available , nb_enabled , nb_disabled
end
2017-08-29 10:52:14 +00:00
function ReaderDictionary : genDictionariesMenu ( )
local items = { }
for _ , ifo in pairs ( available_ifos ) do
table.insert ( items , {
text = ifo.name ,
callback = function ( )
2017-08-29 20:27:43 +00:00
local dicts_disabled = G_reader_settings : readSetting ( " dicts_disabled " )
if dicts_disabled [ ifo.file ] then
dicts_disabled [ ifo.file ] = nil
else
dicts_disabled [ ifo.file ] = true
2017-08-29 10:52:14 +00:00
end
2017-08-29 20:27:43 +00:00
-- Update the -u options to give to sdcv
self : updateSdcvDictNamesOptions ( )
2017-08-29 10:52:14 +00:00
end ,
checked_func = function ( )
2017-08-29 20:27:43 +00:00
local dicts_disabled = G_reader_settings : readSetting ( " dicts_disabled " )
return not dicts_disabled [ ifo.file ]
2017-08-29 10:52:14 +00:00
end
} )
end
return items
end
2017-04-26 06:12:25 +00:00
local function dictDirsEmpty ( dict_dirs )
for _ , dict_dir in ipairs ( dict_dirs ) do
if not util.isEmptyDir ( dict_dir ) then
return false
end
end
return true
end
2016-12-06 21:15:52 +00:00
local function tidyMarkup ( 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<> " , " " )
2016-12-01 12:34:40 +00:00
-- strip all leading empty lines/spaces
def = def : gsub ( " ^%s+ " , " " )
2014-10-29 08:42:00 +00:00
result.definition = def
2014-10-28 07:57:01 +00:00
end
return results
end
2016-12-06 21:15:52 +00:00
function ReaderDictionary : cleanSelection ( text )
-- Will be used by ReaderWikipedia too
if not text then
return " "
end
2017-04-09 14:58:41 +00:00
-- crengine does now a much better job at finding word boundaries, but
-- some cleanup is still needed for selection we get from other engines
-- (example: pdf selection "qu’ autrefois," will be cleaned to "autrefois")
2016-12-06 21:15:52 +00:00
--
2017-04-09 14:58:41 +00:00
-- Replace extended quote (included in the general puncturation range)
-- with plain ascii quote (for french words like "aujourd’ hui")
2016-12-06 21:15:52 +00:00
text = string.gsub ( text , " \xE2 \x80 \x99 " , " ' " ) -- U+2019 (right single quotation mark)
-- Strip punctuation characters around selection
text = util.stripePunctuations ( text )
2017-04-09 14:58:41 +00:00
-- Strip some common english grammatical construct
text = string.gsub ( text , " 's$ " , ' ' ) -- english possessive
-- Strip some common french grammatical constructs
text = string.gsub ( text , " ^[LSDMNTlsdmnt]' " , ' ' ) -- french l' s' t'...
2016-12-06 21:15:52 +00:00
text = string.gsub ( text , " ^[Qq][Uu]' " , ' ' ) -- french qu'
2017-04-09 14:58:41 +00:00
-- Replace no-break space with regular space
text = string.gsub ( text , " \xC2 \xA0 " , ' ' ) -- U+00A0 no-break space
-- There may be a need to remove some (all?) diacritical marks
-- https://en.wikipedia.org/wiki/Combining_character#Unicode_ranges
-- see discussion at https://github.com/koreader/koreader/issues/1649
-- Commented for now, will have to be checked by people who read
-- languages and texts that use them.
-- text = string.gsub(text, "\204[\128-\191]", '') -- U+0300 to U+033F
-- text = string.gsub(text, "\205[\128-\175]", '') -- U+0340 to U+036F
2016-12-06 21:15:52 +00:00
return text
end
2017-06-18 16:08:57 +00:00
function ReaderDictionary : showLookupInfo ( word )
2016-12-06 21:15:52 +00:00
local text = T ( self.lookup_msg , word )
self.lookup_progress_msg = InfoMessage : new { text = text }
UIManager : show ( self.lookup_progress_msg )
UIManager : forceRePaint ( )
end
2017-06-18 16:08:57 +00:00
function ReaderDictionary : dismissLookupInfo ( )
2016-12-06 21:15:52 +00:00
if self.lookup_progress_msg then
UIManager : close ( self.lookup_progress_msg )
UIManager : forceRePaint ( )
end
self.lookup_progress_msg = nil
end
2017-09-09 16:30:00 +00:00
function ReaderDictionary : stardictLookup ( word , box , link )
2016-12-29 08:10:38 +00:00
logger.dbg ( " lookup word: " , word , box )
2016-12-06 21:15:52 +00:00
-- escape quotes and other funny characters in word
word = self : cleanSelection ( word )
2016-12-29 08:10:38 +00:00
logger.dbg ( " stripped word: " , word )
2016-12-06 21:15:52 +00:00
if word == " " then
return
end
2017-06-18 16:08:57 +00:00
if not self.disable_fuzzy_search then
self : showLookupInfo ( word )
end
2016-12-06 21:15:52 +00:00
local final_results = { }
local seen_results = { }
-- Allow for two sdcv calls : one in the classic data/dict, and
-- another one in data/dict_ext if it exists
-- We could put in data/dict_ext dictionaries with a great number of words
-- but poor definitions as a fall back. If these were in data/dict,
-- they would prevent fuzzy searches in other dictories with better
-- definitions, and masks such results. This way, we can get both.
local dict_dirs = { self.data_dir }
local dict_ext = self.data_dir .. " _ext "
if lfs.attributes ( dict_ext , " mode " ) == " directory " then
table.insert ( dict_dirs , dict_ext )
end
2017-04-26 06:12:25 +00:00
-- early exit if no dictionaries
if dictDirsEmpty ( dict_dirs ) then
final_results = {
{
dict = " " ,
word = word ,
definition = _ ( [[No dictionaries installed. Please search for "Dictionary support" in the KOReader Wiki to get more information about installing new dictionaries.]] ) ,
}
}
self : showDict ( word , final_results , box )
return
end
2017-08-29 20:27:43 +00:00
local common_options = self.disable_fuzzy_search and " -njf " or " -nj "
2016-12-06 21:15:52 +00:00
for _ , dict_dir in ipairs ( dict_dirs ) do
2014-03-13 13:52:43 +00:00
local results_str = nil
2015-04-22 06:27:05 +00:00
if Device : isAndroid ( ) then
local A = require ( " android " )
2017-08-29 20:27:43 +00:00
local args = { " ./sdcv " , " --utf8-input " , " --utf8-output " , common_options , word , " --data-dir " , dict_dir }
if self.sdcv_dictnames_options_raw then
for _ , opt in pairs ( self.sdcv_dictnames_options_raw ) do
table.insert ( args , opt )
end
end
results_str = A.stdout ( unpack ( args ) )
2015-04-22 06:27:05 +00:00
else
2017-08-29 20:27:43 +00:00
local cmd = ( " ./sdcv --utf8-input --utf8-output %q %q --data-dir %q " ) : format ( common_options , word , dict_dir )
if self.sdcv_dictnames_options_escaped then
cmd = cmd .. " " .. self.sdcv_dictnames_options_escaped
end
local std_out = io.popen ( cmd , " r " )
2015-04-22 06:27:05 +00:00
if std_out then
results_str = std_out : read ( " *all " )
std_out : close ( )
end
2014-11-26 09:01:34 +00:00
end
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
2016-12-06 21:15:52 +00:00
-- we may get duplicates (sdcv may do multiple queries,
-- in fixed mode then in fuzzy mode), we have to remove them
local h
for _ , r in ipairs ( results ) do
h = r.dict .. r.word .. r.definition
if seen_results [ h ] == nil then
table.insert ( final_results , r )
seen_results [ h ] = true
end
end
2014-07-24 14:11:25 +00:00
else
2016-12-29 08:10:38 +00:00
logger.warn ( " JSON data cannot be decoded " , results )
2014-03-13 13:52:43 +00:00
end
end
2016-12-06 21:15:52 +00:00
if # final_results == 0 then
-- dummy results
final_results = {
{
dict = " " ,
word = word ,
definition = _ ( " No definition found. " ) ,
}
}
end
2017-09-09 16:30:00 +00:00
self : showDict ( word , tidyMarkup ( final_results ) , box , link )
2013-04-23 22:59:52 +00:00
end
2013-04-24 14:57:03 +00:00
2017-09-09 16:30:00 +00:00
function ReaderDictionary : showDict ( word , results , box , link )
2017-06-18 16:08:57 +00:00
self : dismissLookupInfo ( )
2014-08-17 16:32:09 +00:00
if results and results [ 1 ] then
2016-12-29 08:10:38 +00:00
logger.dbg ( " showing quick lookup window " , word , results )
2014-11-21 10:32:43 +00:00
self.dict_window = DictQuickLookup : new {
2016-06-28 16:35:00 +00:00
window_list = self.dict_window_list ,
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 ,
2017-09-09 16:30:00 +00:00
-- selected link, if any
selected_link = link ,
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
2016-12-06 21:15:52 +00:00
is_wiki = self.is_wiki ,
wiki_languages = self.wiki_languages ,
2017-03-24 07:20:37 +00:00
refresh_callback = function ( )
2017-05-12 16:28:42 +00:00
if self.view then
-- update info in footer (time, battery, etc)
self.view . footer : updateFooter ( )
end
2017-03-24 07:20:37 +00:00
end ,
2014-11-21 10:32:43 +00:00
}
2016-06-28 16:35:00 +00:00
table.insert ( self.dict_window_list , self.dict_window )
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 )
2016-12-29 08:10:38 +00:00
logger.dbg ( " make default dictionary: " , dict )
2014-03-13 13:52:43 +00:00
self.default_dictionary = dict
2014-11-28 13:20:38 +00:00
UIManager : show ( InfoMessage : new {
2017-05-08 07:26:01 +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 " )
2017-06-18 16:08:57 +00:00
self.disable_fuzzy_search = config : readSetting ( " disable_fuzzy_search " )
if self.disable_fuzzy_search == nil then
self.disable_fuzzy_search = G_reader_settings : isTrue ( " disable_fuzzy_search " )
end
2013-07-21 06:23:54 +00:00
end
2013-12-27 15:18:16 +00:00
function ReaderDictionary : onSaveSettings ( )
2016-12-29 08:10:38 +00:00
logger.dbg ( " save default dictionary " , self.default_dictionary )
2014-03-13 13:52:43 +00:00
self.ui . doc_settings : saveSetting ( " default_dictionary " , self.default_dictionary )
2017-06-18 16:08:57 +00:00
self.ui . doc_settings : saveSetting ( " disable_fuzzy_search " , self.disable_fuzzy_search )
end
function ReaderDictionary : makeDisableFuzzyDefault ( disable_fuzzy_search )
logger.dbg ( " disable fuzzy search " , self.disable_fuzzy_search )
UIManager : show ( ConfirmBox : new {
text = T (
disable_fuzzy_search
and _ ( " Disable fuzzy search by default? " )
or _ ( " Enable fuzzy search by default? " )
) ,
ok_callback = function ( )
G_reader_settings : saveSetting ( " disable_fuzzy_search " , disable_fuzzy_search )
end ,
} )
2013-07-21 06:23:54 +00:00
end
2013-10-18 20:38:07 +00:00
return ReaderDictionary