Debian Codegraph @

setup/mathjax.py

raw
1 #!/usr/bin/env python
2
3
4 __license__ = 'GPL v3'
5 __copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
6 __docformat__ = 'restructuredtext en'
7
8 import json
9 import os
10 from hashlib import sha1
11
12 from setup.revendor import ReVendor
13
14
15 class MathJax(ReVendor):
16 description = 'Create the MathJax bundle'
17 NAME = 'mathjax'
18 TAR_NAME = 'MathJax'
19 VERSION = '3.1.4'
20 DOWNLOAD_URL = f'https://github.com/mathjax/MathJax/archive/{VERSION}.tar.gz'
21
22 def add_file_pre(self, name, raw):
23 self.h.update(raw)
24 self.mathjax_files[name] = len(raw)
25
26 def already_present(self):
27 manifest = self.j(self.vendored_dir, 'manifest.json')
28 if os.path.exists(manifest):
29 with open(manifest, 'rb') as f:
30 return json.load(f).get('version') == self.VERSION
31 return False
32
33 def run(self, opts):
34 if not opts.system_mathjax and self.already_present():
35 self.info('MathJax already present in the resources directory, not downloading')
36 return
37 self.use_symlinks = opts.system_mathjax
38 self.h = sha1()
39 self.mathjax_files = {}
40 self.clean()
41 os.mkdir(self.vendored_dir)
42 with self.temp_dir(suffix='-calibre-mathjax-build') as tdir:
43 src = opts.path_to_mathjax or self.download_vendor_release(tdir, opts.mathjax_url)
44 if os.path.isdir(os.path.join(src, 'es5')):
45 src = os.path.join(src, 'es5')
46 self.info('Adding MathJax...')
47 for x in 'core loader startup input/tex-full input/asciimath input/mml input/mml/entities output/chtml'.split():
48 self.add_file(self.j(src, x + '.js'), x + '.js')
49 self.add_tree(self.j(src, 'output', 'chtml'), 'output/chtml')
50 etag = self.h.hexdigest()
51 with open(self.j(self.RESOURCES, 'mathjax', 'manifest.json'), 'wb') as f:
52 f.write(json.dumps({'etag': etag, 'files': self.mathjax_files, 'version': self.VERSION}, indent=2).encode('utf-8'))