Make code to load hyphenation dicts re-useable

This commit is contained in:
Kovid Goyal 2018-05-24 13:08:05 +05:30
parent 0b53f53529
commit 7b63184107
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 37 additions and 31 deletions

View File

@ -7,11 +7,10 @@ __license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2012, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import os, zipfile import os
import calibre import calibre
from calibre.utils.localization import lang_as_iso639_1 from calibre.utils.resources import compiled_coffeescript, load_hyphenator_dicts
from calibre.utils.resources import compiled_coffeescript
class JavaScriptLoader(object): class JavaScriptLoader(object):
@ -67,9 +66,7 @@ class JavaScriptLoader(object):
ans = P(src, data=True, ans = P(src, data=True,
allow_user_override=False).decode('utf-8') allow_user_override=False).decode('utf-8')
else: else:
dynamic = (self._dynamic_coffeescript and dynamic = self._dynamic_coffeescript and calibre.__file__ and not calibre.__file__.endswith('.pyo') and os.path.exists(calibre.__file__)
calibre.__file__ and not calibre.__file__.endswith('.pyo') and
os.path.exists(calibre.__file__))
ans = compiled_coffeescript(src, dynamic=dynamic).decode('utf-8') ans = compiled_coffeescript(src, dynamic=dynamic).decode('utf-8')
self._cache[name] = ans self._cache[name] = ans
@ -80,29 +77,6 @@ class JavaScriptLoader(object):
src = self.get(x) src = self.get(x)
evaljs(src) evaljs(src)
if not lang: js, lang = load_hyphenator_dicts(self._hp_cache, lang, default_lang)
lang = default_lang or 'en' evaljs(js)
def lang_name(l):
l = l.lower()
l = lang_as_iso639_1(l)
if not l:
l = 'en'
l = {'en':'en-us', 'nb':'nb-no', 'el':'el-monoton'}.get(l, l)
return l.lower().replace('_', '-')
if not self._hp_cache:
with zipfile.ZipFile(P('viewer/hyphenate/patterns.zip',
allow_user_override=False), 'r') as zf:
for pat in zf.namelist():
raw = zf.read(pat).decode('utf-8')
self._hp_cache[pat.partition('.')[0]] = raw
if lang_name(lang) not in self._hp_cache:
lang = lang_name(default_lang)
lang = lang_name(lang)
evaljs('\n\n'.join(self._hp_cache.itervalues()))
return lang return lang

View File

@ -64,6 +64,7 @@ class PathResolver(object):
return ans return ans
_resolver = PathResolver() _resolver = PathResolver()
@ -117,5 +118,36 @@ def compiled_coffeescript(name, dynamic=False):
else: else:
return zf.read(name+'.js') return zf.read(name+'.js')
def load_hyphenator_dicts(hp_cache, lang, default_lang='en'):
from calibre.utils.localization import lang_as_iso639_1
import zipfile
if not lang:
lang = default_lang or 'en'
def lang_name(l):
l = l.lower()
l = lang_as_iso639_1(l)
if not l:
l = 'en'
l = {'en':'en-us', 'nb':'nb-no', 'el':'el-monoton'}.get(l, l)
return l.lower().replace('_', '-')
if not hp_cache:
with zipfile.ZipFile(P('viewer/hyphenate/patterns.zip',
allow_user_override=False), 'r') as zf:
for pat in zf.namelist():
raw = zf.read(pat).decode('utf-8')
hp_cache[pat.partition('.')[0]] = raw
if lang_name(lang) not in hp_cache:
lang = lang_name(default_lang)
lang = lang_name(lang)
js = '\n\n'.join(hp_cache.itervalues())
return js, lang
__builtin__.__dict__['P'] = get_path __builtin__.__dict__['P'] = get_path
__builtin__.__dict__['I'] = get_image_path __builtin__.__dict__['I'] = get_image_path