mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-08-11 09:13:57 -04:00
Make code to load hyphenation dicts re-useable
This commit is contained in:
parent
0b53f53529
commit
7b63184107
@ -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
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user