Show More
@@ -41,6 +41,8 b" def latex_to_png(s, encode=False, backend='mpl'):" | |||||
41 | backend : {mpl, dvipng} |
|
41 | backend : {mpl, dvipng} | |
42 | Backend for producing PNG data. |
|
42 | Backend for producing PNG data. | |
43 |
|
43 | |||
|
44 | None is returned when the backend cannot be used. | |||
|
45 | ||||
44 | """ |
|
46 | """ | |
45 | if backend == 'mpl': |
|
47 | if backend == 'mpl': | |
46 | f = latex_to_png_mpl |
|
48 | f = latex_to_png_mpl | |
@@ -49,13 +51,16 b" def latex_to_png(s, encode=False, backend='mpl'):" | |||||
49 | else: |
|
51 | else: | |
50 | raise ValueError('No such backend {0}'.format(backend)) |
|
52 | raise ValueError('No such backend {0}'.format(backend)) | |
51 | bin_data = f(s) |
|
53 | bin_data = f(s) | |
52 | if encode: |
|
54 | if encode and bin_data: | |
53 | bin_data = encodestring(bin_data) |
|
55 | bin_data = encodestring(bin_data) | |
54 | return bin_data |
|
56 | return bin_data | |
55 |
|
57 | |||
56 |
|
58 | |||
57 | def latex_to_png_mpl(s): |
|
59 | def latex_to_png_mpl(s): | |
|
60 | try: | |||
58 | from matplotlib import mathtext |
|
61 | from matplotlib import mathtext | |
|
62 | except ImportError: | |||
|
63 | return None | |||
59 |
|
64 | |||
60 | mt = mathtext.MathTextParser('bitmap') |
|
65 | mt = mathtext.MathTextParser('bitmap') | |
61 | f = StringIO() |
|
66 | f = StringIO() | |
@@ -84,12 +89,13 b' def latex_to_png_dvipng(s):' | |||||
84 |
|
89 | |||
85 | with open(outfile) as f: |
|
90 | with open(outfile) as f: | |
86 | bin_data = f.read() |
|
91 | bin_data = f.read() | |
|
92 | except subprocess.CalledProcessError: | |||
|
93 | bin_data = None | |||
87 | finally: |
|
94 | finally: | |
88 | shutil.rmtree(workdir) |
|
95 | shutil.rmtree(workdir) | |
89 | return bin_data |
|
96 | return bin_data | |
90 |
|
97 | |||
91 |
|
98 | |||
92 |
|
||||
93 | _latex_header = r''' |
|
99 | _latex_header = r''' | |
94 | \documentclass{article} |
|
100 | \documentclass{article} | |
95 | \usepackage{amsmath} |
|
101 | \usepackage{amsmath} | |
@@ -116,6 +122,7 b" def latex_to_html(s, alt='image'):" | |||||
116 | The alt text to use for the HTML. |
|
122 | The alt text to use for the HTML. | |
117 | """ |
|
123 | """ | |
118 | base64_data = latex_to_png(s, encode=True) |
|
124 | base64_data = latex_to_png(s, encode=True) | |
|
125 | if base64_data: | |||
119 | return _data_uri_template_png % (base64_data, alt) |
|
126 | return _data_uri_template_png % (base64_data, alt) | |
120 |
|
127 | |||
121 |
|
128 |
General Comments 0
You need to be logged in to leave comments.
Login now