##// END OF EJS Templates
Upgrade default mathjax version....
Björn Grüning -
Show More
@@ -1,238 +1,238 b''
1 #!/usr/bin/python
1 #!/usr/bin/python
2 """Utility function for installing MathJax javascript library into
2 """Utility function for installing MathJax javascript library into
3 your IPython nbextensions directory, for offline use.
3 your IPython nbextensions directory, for offline use.
4
4
5 Authors:
5 Authors:
6
6
7 * Min RK
7 * Min RK
8 * Mark Sienkiewicz
8 * Mark Sienkiewicz
9 * Matthias Bussonnier
9 * Matthias Bussonnier
10
10
11 To download and install MathJax:
11 To download and install MathJax:
12
12
13 From Python:
13 From Python:
14
14
15 >>> from IPython.external.mathjax import install_mathjax
15 >>> from IPython.external.mathjax import install_mathjax
16 >>> install_mathjax()
16 >>> install_mathjax()
17
17
18 From the command line:
18 From the command line:
19
19
20 $ python -m IPython.external.mathjax
20 $ python -m IPython.external.mathjax
21
21
22 To a specific location:
22 To a specific location:
23
23
24 $ python -m IPython.external.mathjax -i /usr/share/
24 $ python -m IPython.external.mathjax -i /usr/share/
25
25
26 will install mathjax to /usr/share/mathjax
26 will install mathjax to /usr/share/mathjax
27
27
28 To install MathJax from a file you have already downloaded:
28 To install MathJax from a file you have already downloaded:
29
29
30 $ python -m IPython.external.mathjax mathjax-xxx.tar.gz
30 $ python -m IPython.external.mathjax mathjax-xxx.tar.gz
31 $ python -m IPython.external.mathjax mathjax-xxx.zip
31 $ python -m IPython.external.mathjax mathjax-xxx.zip
32
32
33 It will not install MathJax if it is already there. Use -r to
33 It will not install MathJax if it is already there. Use -r to
34 replace the existing copy of MathJax.
34 replace the existing copy of MathJax.
35
35
36 To find the directory where IPython would like MathJax installed:
36 To find the directory where IPython would like MathJax installed:
37
37
38 $ python -m IPython.external.mathjax -d
38 $ python -m IPython.external.mathjax -d
39
39
40 """
40 """
41 from __future__ import print_function
41 from __future__ import print_function
42
42
43
43
44 #-----------------------------------------------------------------------------
44 #-----------------------------------------------------------------------------
45 # Copyright (C) 2011 The IPython Development Team
45 # Copyright (C) 2011 The IPython Development Team
46 #
46 #
47 # Distributed under the terms of the BSD License. The full license is in
47 # Distributed under the terms of the BSD License. The full license is in
48 # the file COPYING, distributed as part of this software.
48 # the file COPYING, distributed as part of this software.
49 #-----------------------------------------------------------------------------
49 #-----------------------------------------------------------------------------
50
50
51
51
52 #-----------------------------------------------------------------------------
52 #-----------------------------------------------------------------------------
53 # Imports
53 # Imports
54 #-----------------------------------------------------------------------------
54 #-----------------------------------------------------------------------------
55
55
56 import argparse
56 import argparse
57 import os
57 import os
58 import shutil
58 import shutil
59 import sys
59 import sys
60 import tarfile
60 import tarfile
61 import zipfile
61 import zipfile
62
62
63 from IPython.utils.path import get_ipython_dir
63 from IPython.utils.path import get_ipython_dir
64
64
65 try:
65 try:
66 from urllib.request import urlopen # Py 3
66 from urllib.request import urlopen # Py 3
67 except ImportError:
67 except ImportError:
68 from urllib2 import urlopen
68 from urllib2 import urlopen
69
69
70 #-----------------------------------------------------------------------------
70 #-----------------------------------------------------------------------------
71 #
71 #
72 #-----------------------------------------------------------------------------
72 #-----------------------------------------------------------------------------
73
73
74 # Where mathjax will be installed
74 # Where mathjax will be installed
75
75
76 nbextensions = os.path.join(get_ipython_dir(), 'nbextensions')
76 nbextensions = os.path.join(get_ipython_dir(), 'nbextensions')
77 default_dest = os.path.join(nbextensions, 'mathjax')
77 default_dest = os.path.join(nbextensions, 'mathjax')
78
78
79 # Test for access to install mathjax
79 # Test for access to install mathjax
80
80
81 def prepare_dest(dest, replace=False):
81 def prepare_dest(dest, replace=False):
82 """prepare the destination folder for mathjax install
82 """prepare the destination folder for mathjax install
83
83
84 Returns False if mathjax appears to already be installed and there is nothing to do,
84 Returns False if mathjax appears to already be installed and there is nothing to do,
85 True otherwise.
85 True otherwise.
86 """
86 """
87
87
88 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
88 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
89 if not os.path.exists(parent):
89 if not os.path.exists(parent):
90 os.makedirs(parent)
90 os.makedirs(parent)
91
91
92 if os.path.exists(dest):
92 if os.path.exists(dest):
93 if replace:
93 if replace:
94 print("removing existing MathJax at %s" % dest)
94 print("removing existing MathJax at %s" % dest)
95 shutil.rmtree(dest)
95 shutil.rmtree(dest)
96 return True
96 return True
97 else:
97 else:
98 mathjax_js = os.path.join(dest, 'MathJax.js')
98 mathjax_js = os.path.join(dest, 'MathJax.js')
99 if not os.path.exists(mathjax_js):
99 if not os.path.exists(mathjax_js):
100 raise IOError("%s exists, but does not contain MathJax.js" % dest)
100 raise IOError("%s exists, but does not contain MathJax.js" % dest)
101 print("%s already exists" % mathjax_js)
101 print("%s already exists" % mathjax_js)
102 return False
102 return False
103 else:
103 else:
104 return True
104 return True
105
105
106
106
107 def extract_tar(fd, dest):
107 def extract_tar(fd, dest):
108 """extract a tarball from filelike `fd` to destination `dest`"""
108 """extract a tarball from filelike `fd` to destination `dest`"""
109 # use 'r|gz' stream mode, because socket file-like objects can't seek:
109 # use 'r|gz' stream mode, because socket file-like objects can't seek:
110 tar = tarfile.open(fileobj=fd, mode='r|gz')
110 tar = tarfile.open(fileobj=fd, mode='r|gz')
111
111
112 # The first entry in the archive is the top-level dir
112 # The first entry in the archive is the top-level dir
113 topdir = tar.firstmember.path
113 topdir = tar.firstmember.path
114
114
115 # extract the archive (contains a single directory) to the destination directory
115 # extract the archive (contains a single directory) to the destination directory
116 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
116 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
117 tar.extractall(parent)
117 tar.extractall(parent)
118
118
119 # it will be mathjax-MathJax-<sha>, rename to just mathjax
119 # it will be mathjax-MathJax-<sha>, rename to just mathjax
120 os.rename(os.path.join(parent, topdir), dest)
120 os.rename(os.path.join(parent, topdir), dest)
121
121
122
122
123 def extract_zip(fd, dest):
123 def extract_zip(fd, dest):
124 """extract a zip file from filelike `fd` to destination `dest`"""
124 """extract a zip file from filelike `fd` to destination `dest`"""
125 z = zipfile.ZipFile(fd, 'r')
125 z = zipfile.ZipFile(fd, 'r')
126
126
127 # The first entry in the archive is the top-level dir
127 # The first entry in the archive is the top-level dir
128 topdir = z.namelist()[0]
128 topdir = z.namelist()[0]
129
129
130 # extract the archive (contains a single directory) to the static/ directory
130 # extract the archive (contains a single directory) to the static/ directory
131 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
131 parent = os.path.abspath(os.path.join(dest, os.path.pardir))
132 z.extractall(parent)
132 z.extractall(parent)
133
133
134 # it will be mathjax-MathJax-<sha>, rename to just mathjax
134 # it will be mathjax-MathJax-<sha>, rename to just mathjax
135 d = os.path.join(parent, topdir)
135 d = os.path.join(parent, topdir)
136 os.rename(os.path.join(parent, topdir), dest)
136 os.rename(os.path.join(parent, topdir), dest)
137
137
138
138
139 def install_mathjax(tag='v2.2', dest=default_dest, replace=False, file=None, extractor=extract_tar):
139 def install_mathjax(tag='2.4.0', dest=default_dest, replace=False, file=None, extractor=extract_tar):
140 """Download and/or install MathJax for offline use.
140 """Download and/or install MathJax for offline use.
141
141
142 This will install mathjax to the nbextensions dir in your IPYTHONDIR.
142 This will install mathjax to the nbextensions dir in your IPYTHONDIR.
143
143
144 MathJax is a ~15MB download, and ~150MB installed.
144 MathJax is a ~15MB download, and ~150MB installed.
145
145
146 Parameters
146 Parameters
147 ----------
147 ----------
148
148
149 replace : bool [False]
149 replace : bool [False]
150 Whether to remove and replace an existing install.
150 Whether to remove and replace an existing install.
151 dest : str [IPYTHONDIR/nbextensions/mathjax]
151 dest : str [IPYTHONDIR/nbextensions/mathjax]
152 Where to install mathjax
152 Where to install mathjax
153 tag : str ['v2.2']
153 tag : str ['2.4.0']
154 Which tag to download. Default is 'v2.2', the current stable release,
154 Which tag to download. Default is '2.4.0', the current stable release,
155 but alternatives include 'v1.1a' and 'master'.
155 but alternatives include 'v1.1a' and 'master'.
156 file : file like object [ defualt to content of https://github.com/mathjax/MathJax/tarball/#{tag}]
156 file : file like object [ defualt to content of https://github.com/mathjax/MathJax/tarball/#{tag}]
157 File handle from which to untar/unzip/... mathjax
157 File handle from which to untar/unzip/... mathjax
158 extractor : function
158 extractor : function
159 Method to use to untar/unzip/... `file`
159 Method to use to untar/unzip/... `file`
160 """
160 """
161 try:
161 try:
162 anything_to_do = prepare_dest(dest, replace)
162 anything_to_do = prepare_dest(dest, replace)
163 except OSError as e:
163 except OSError as e:
164 print("ERROR %s, require write access to %s" % (e, dest))
164 print("ERROR %s, require write access to %s" % (e, dest))
165 return 1
165 return 1
166 else:
166 else:
167 if not anything_to_do:
167 if not anything_to_do:
168 return 0
168 return 0
169
169
170 if file is None:
170 if file is None:
171 # download mathjax
171 # download mathjax
172 mathjax_url = "https://github.com/mathjax/MathJax/archive/%s.tar.gz" %tag
172 mathjax_url = "https://github.com/mathjax/MathJax/archive/%s.tar.gz" %tag
173 print("Downloading mathjax source from %s" % mathjax_url)
173 print("Downloading mathjax source from %s" % mathjax_url)
174 response = urlopen(mathjax_url)
174 response = urlopen(mathjax_url)
175 file = response.fp
175 file = response.fp
176
176
177 print("Extracting to %s" % dest)
177 print("Extracting to %s" % dest)
178 extractor(file, dest)
178 extractor(file, dest)
179 return 0
179 return 0
180
180
181
181
182 def main():
182 def main():
183 parser = argparse.ArgumentParser(
183 parser = argparse.ArgumentParser(
184 description="""Install mathjax from internet or local archive""",
184 description="""Install mathjax from internet or local archive""",
185 )
185 )
186
186
187 parser.add_argument(
187 parser.add_argument(
188 '-i',
188 '-i',
189 '--install-dir',
189 '--install-dir',
190 default=nbextensions,
190 default=nbextensions,
191 help='custom installation directory. Mathjax will be installed in here/mathjax')
191 help='custom installation directory. Mathjax will be installed in here/mathjax')
192
192
193 parser.add_argument(
193 parser.add_argument(
194 '-d',
194 '-d',
195 '--print-dest',
195 '--print-dest',
196 action='store_true',
196 action='store_true',
197 help='print where mathjax would be installed and exit')
197 help='print where mathjax would be installed and exit')
198 parser.add_argument(
198 parser.add_argument(
199 '-r',
199 '-r',
200 '--replace',
200 '--replace',
201 action='store_true',
201 action='store_true',
202 help='Whether to replace current mathjax if it already exists')
202 help='Whether to replace current mathjax if it already exists')
203 parser.add_argument('filename',
203 parser.add_argument('filename',
204 help="the local tar/zip-ball filename containing mathjax",
204 help="the local tar/zip-ball filename containing mathjax",
205 nargs='?',
205 nargs='?',
206 metavar='filename')
206 metavar='filename')
207
207
208 pargs = parser.parse_args()
208 pargs = parser.parse_args()
209
209
210 dest = os.path.join(pargs.install_dir, 'mathjax')
210 dest = os.path.join(pargs.install_dir, 'mathjax')
211
211
212 if pargs.print_dest:
212 if pargs.print_dest:
213 print(dest)
213 print(dest)
214 return
214 return
215
215
216 # remove/replace existing mathjax?
216 # remove/replace existing mathjax?
217 replace = pargs.replace
217 replace = pargs.replace
218
218
219 # do it
219 # do it
220 if pargs.filename:
220 if pargs.filename:
221 fname = pargs.filename
221 fname = pargs.filename
222
222
223 # automatically detect zip/tar - could do something based
223 # automatically detect zip/tar - could do something based
224 # on file content, but really not cost-effective here.
224 # on file content, but really not cost-effective here.
225 if fname.endswith('.zip'):
225 if fname.endswith('.zip'):
226 extractor = extract_zip
226 extractor = extract_zip
227 else :
227 else :
228 extractor = extract_tar
228 extractor = extract_tar
229 # do it
229 # do it
230 return install_mathjax(file=open(fname, "rb"), replace=replace, extractor=extractor, dest=dest)
230 return install_mathjax(file=open(fname, "rb"), replace=replace, extractor=extractor, dest=dest)
231 else:
231 else:
232 return install_mathjax(replace=replace, dest=dest)
232 return install_mathjax(replace=replace, dest=dest)
233
233
234
234
235 if __name__ == '__main__' :
235 if __name__ == '__main__' :
236 sys.exit(main())
236 sys.exit(main())
237
237
238 __all__ = ['install_mathjax', 'main', 'default_dest']
238 __all__ = ['install_mathjax', 'main', 'default_dest']
General Comments 0
You need to be logged in to leave comments. Login now