Use a forked MathJax, makes it easier to maintain the patch

This commit is contained in:
Kovid Goyal 2016-05-03 10:20:29 +05:30
parent 0009208119
commit cf5755f6a2
2 changed files with 24 additions and 39 deletions

View File

@ -7,7 +7,7 @@ __license__ = 'GPL v3'
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import os, re import os
from urllib import urlretrieve from urllib import urlretrieve
from zipfile import ZipFile, ZIP_STORED, ZipInfo from zipfile import ZipFile, ZIP_STORED, ZipInfo
from hashlib import sha1 from hashlib import sha1
@ -19,38 +19,22 @@ from setup import Command
class MathJax(Command): class MathJax(Command):
description = 'Create the MathJax bundle' description = 'Create the MathJax bundle'
MATH_JAX_VERSION = '2.6.1' MATH_JAX_URL = 'https://github.com/kovidgoyal/MathJax/archive/master.zip'
MATH_JAX_URL = 'https://github.com/mathjax/MathJax/archive/%s.zip' % MATH_JAX_VERSION
FONT_FAMILY = 'TeX' FONT_FAMILY = 'TeX'
def add_options(self, parser): def add_options(self, parser):
parser.add_option('--path-to-mathjax', help='Path to the MathJax source code') parser.add_option('--path-to-mathjax', help='Path to the MathJax source code')
def download_mathjax_release(self): def download_mathjax_release(self, tdir):
from calibre.ptempfile import TemporaryDirectory
self.info('Downloading MathJax:', self.MATH_JAX_URL) self.info('Downloading MathJax:', self.MATH_JAX_URL)
filename = urlretrieve(self.MATH_JAX_URL)[0] filename = urlretrieve(self.MATH_JAX_URL)[0]
with ZipFile(filename) as zf, TemporaryDirectory() as tdir: with ZipFile(filename) as zf:
zf.extractall(tdir) zf.extractall(tdir)
for d in os.listdir(tdir): return os.path.join(tdir, 'MathJax-master')
q = os.path.join(tdir, d)
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): def add_file(self, zf, path, name):
with open(path, 'rb') as f: with open(path, 'rb') as f:
raw = f.read() raw = f.read()
if name == 'jax/output/CommonHTML/fonts/TeX/fontdata.js':
raw = self.patch_webfonts(raw)
self.patched = True
self.h.update(raw) self.h.update(raw)
zi = ZipInfo(name) zi = ZipInfo(name)
zi.external_attr = 0o444 << 16L zi.external_attr = 0o444 << 16L
@ -67,9 +51,10 @@ class MathJax(Command):
return '/fonts/' in name and self.FONT_FAMILY not in name return '/fonts/' in name and self.FONT_FAMILY not in name
def run(self, opts): def run(self, opts):
self.patched = False from calibre.ptempfile import TemporaryDirectory
self.h = sha1() self.h = sha1()
src = opts.path_to_mathjax or self.download_mathjax_release() with TemporaryDirectory() as tdir:
src = opts.path_to_mathjax or self.download_mathjax_release(tdir)
self.info('Compressing MathJax...') self.info('Compressing MathJax...')
from calibre.ptempfile import SpooledTemporaryFile from calibre.ptempfile import SpooledTemporaryFile
t = SpooledTemporaryFile() t = SpooledTemporaryFile()
@ -81,8 +66,6 @@ class MathJax(Command):
zf.comment = self.h.hexdigest() zf.comment = self.h.hexdigest()
t.seek(0) 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: with open(self.j(self.RESOURCES, 'content-server', 'mathjax.zip.xz'), 'wb') as f:
compress(t, f, level=9) compress(t, f, level=9)
with open(self.j(self.RESOURCES, 'content-server', 'mathjax.version'), 'wb') as f: with open(self.j(self.RESOURCES, 'content-server', 'mathjax.version'), 'wb') as f:

View File

@ -23,6 +23,8 @@ def monkeypatch(mathjax_files):
return name return name
ans = get_url(mathjax_files, name) ans = get_url(mathjax_files, name)
if ans is name and not name.startswith('blob:'): if ans is name and not name.startswith('blob:'):
if ans.endswith('.eot') or ans.endswith('.otf'):
return ''
print('WARNING: Failed to resolve MathJax file:', name) print('WARNING: Failed to resolve MathJax file:', name)
return ans return ans
window.MathJax.Ajax.fileURL = file_url window.MathJax.Ajax.fileURL = file_url