Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 import os
00021 import os.path
00022 import shutil
00023 import sys
00024 import re
00025 from string import Template
00026 import subprocess
00027
00028 tex_file = \
00029 r"""
00030 \documentclass{article}
00031 \pagestyle{empty}
00032 \begin{document}
00033 ${formula}
00034 \end{document}
00035 """
00036
00037 tex_file_template = Template(tex_file)
00038 tex_math_re = re.compile(r"<span class=\"math-inline\">(.*?)</span>")
00039
00040 def from_tex(tex_code, filename, density = 100):
00041 """Given a LaTeX string, generates a PNG file at a given density.
00042 Temporary files are generated in the current folder, prefixed by filename,
00043 then erased.
00044
00045 Inputs:
00046 tex_code: String with a valid LaTeX code (ex: $a_i = b_i$)
00047 filename Filename, without the extension. Can be located in another directory
00048 density (Optional) Output density, by default at 100
00049
00050 Output:
00051 No output."""
00052
00053 basename = os.path.basename(filename)
00054 f = open("{0}.tex".format(basename),'w')
00055 f.write(tex_file_template.substitute(formula=tex_code))
00056 f.close()
00057 devnull = open(os.devnull)
00058 retcode = subprocess.call(["texi2dvi","{0}.tex".format(basename)], stdout=devnull, stderr=devnull)
00059 retcode = subprocess.call(["dvips","-E","{0}.dvi".format(basename)], stdout=devnull, stderr=devnull)
00060 retcode = subprocess.call(["convert","-density","{0}x{0}".format(density),"{0}.ps".format(basename),"{0}.png".format(basename)])
00061
00062 if basename != filename:
00063 local_png = "{0}.png".format(basename)
00064 new_png = "{0}.png".format(filename)
00065 shutil.copy(local_png, new_png)
00066 os.remove(local_png)
00067 os.remove("{0}.tex".format(basename))
00068 os.remove("{0}.aux".format(basename))
00069 os.remove("{0}.dvi".format(basename))
00070 os.remove("{0}.log".format(basename))
00071 os.remove("{0}.ps".format(basename))
00072
00073 def from_html(source_html_file, output_basename):
00074 """Given an HTML file, convert all LaTeX codes enclosed between <span class="math-inline"> tags.
00075
00076 Inputs:
00077 source_html_file Path to the HTML file
00078 output_basename Basename for the output files, without any extension.
00079 Corresponding .html and .png files will be generated.
00080
00081 Output:
00082 No output."""
00083
00084 try:
00085 f = open(source_html_file, 'r')
00086 except IOError:
00087 print >> sys.stderr, "No such file: {0}".format(source_html_file)
00088 return
00089 else:
00090 html = f.read()
00091 f.close()
00092
00093
00094 i = 0
00095
00096
00097 tex_lookup = dict()
00098
00099
00100 pos = 0
00101 while 1:
00102 match = tex_math_re.search(html, pos)
00103 if match:
00104 tex_expr = match.group(1)
00105 if tex_lookup.has_key(tex_expr):
00106
00107 filename = tex_lookup[tex_expr]
00108 else:
00109
00110 filename = output_basename + "-eq" + str(i)
00111 i += 1
00112 print " " + filename + ".png"
00113 from_tex(tex_expr, filename)
00114
00115 tex_lookup[tex_expr] = filename
00116
00117 html = tex_math_re.sub("<img src=\"" + os.path.basename(filename) + ".png\">", html, 1)
00118 else:
00119 break
00120
00121
00122 output_html = output_basename + ".html"
00123 try:
00124 f = open(output_html, 'w')
00125 except IOError:
00126 print >> sys.stderr, "Unable to write the result to: {0}".format(output_html)
00127 return
00128 else:
00129 f.write(html)
00130 f.close()
00131
00132
00133 if __name__ == "__main__":
00134
00135
00136 from_html("en_asebastdnative.html", "test-out")