mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Patch MathJax so that correct webfont URLs are generated
This commit is contained in:
parent
ef5f490cf4
commit
514a8a2863
@ -7,7 +7,7 @@ __license__ = 'GPL v3'
|
||||
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import os
|
||||
import os, re
|
||||
from urllib import urlretrieve
|
||||
from zipfile import ZipFile, ZIP_STORED, ZipInfo
|
||||
from hashlib import sha1
|
||||
@ -37,9 +37,20 @@ class MathJax(Command):
|
||||
if os.path.isdir(q):
|
||||
return q
|
||||
|
||||
def patch_webfonts(self, raw):
|
||||
raw = raw.decode('utf-8')
|
||||
nraw = re.sub(r'font = {[^}]+WOFFDIR[^}]+}',
|
||||
'''font = {'font-family':family+'w', src:'url(' + AJAX.fileURL(WOFFDIR+'/'+name+'-'+variant+'.woff') + ') format("woff")'}''', raw)
|
||||
if raw == nraw:
|
||||
raise SystemExit('Patching of webfonts failed')
|
||||
return nraw.encode('utf-8')
|
||||
|
||||
def add_file(self, zf, path, name):
|
||||
with open(path, 'rb') as f:
|
||||
raw = f.read()
|
||||
if name == 'jax/output/CommonHTML/fonts/TeX/fontdata.js':
|
||||
raw = self.patch_webfonts(raw)
|
||||
self.patched = True
|
||||
self.h.update(raw)
|
||||
zi = ZipInfo(name)
|
||||
zi.external_attr = 0o444 << 16L
|
||||
@ -56,6 +67,7 @@ class MathJax(Command):
|
||||
return '/fonts/' in name and self.FONT_FAMILY not in name
|
||||
|
||||
def run(self, opts):
|
||||
self.patched = False
|
||||
self.h = sha1()
|
||||
src = opts.path_to_mathjax or self.download_mathjax_release()
|
||||
self.info('Compressing MathJax...')
|
||||
@ -69,6 +81,8 @@ class MathJax(Command):
|
||||
|
||||
zf.comment = self.h.hexdigest()
|
||||
t.seek(0)
|
||||
if not self.patched:
|
||||
raise SystemExit('Could not find fontdata.js to patch')
|
||||
with open(self.j(self.RESOURCES, 'content-server', 'mathjax.zip.xz'), 'wb') as f:
|
||||
compress(t, f, level=9)
|
||||
with open(self.j(self.RESOURCES, 'content-server', 'mathjax.version'), 'wb') as f:
|
||||
|
Loading…
x
Reference in New Issue
Block a user