$search
00001 #!/usr/bin/env python 00002 # Aseba - an event-based framework for distributed robot control 00003 # Copyright (C) 2007--2011: 00004 # Stephane Magnenat <stephane at magnenat dot net> 00005 # (http://stephane.magnenat.net) 00006 # and other contributors, see authors.txt for details 00007 # 00008 # This program is free software: you can redistribute it and/or modify 00009 # it under the terms of the GNU Lesser General Public License as published 00010 # by the Free Software Foundation, version 3 of the License. 00011 # 00012 # This program is distributed in the hope that it will be useful, 00013 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 # GNU Lesser General Public License for more details. 00016 # 00017 # You should have received a copy of the GNU Lesser General Public License 00018 # along with this program. If not, see <http://www.gnu.org/licenses/>. 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) # process in the current folder, result will be copied later 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 # copy the result to the final file (if directory differes) 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 # counter 00094 i = 0 00095 00096 # lookup dictionnary 00097 tex_lookup = dict() 00098 00099 # loop search for the regular expression 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 # already encountered this expression, retrive the file 00107 filename = tex_lookup[tex_expr] 00108 else: 00109 # we must convert to png 00110 filename = output_basename + "-eq" + str(i) 00111 i += 1 00112 print " " + filename + ".png" 00113 from_tex(tex_expr, filename) 00114 # register into the lookup 00115 tex_lookup[tex_expr] = filename 00116 #html = tex_math_re.sub("<img class=inline-math src=\"" + os.path.basename(filename) + ".png\">", html, 1) 00117 html = tex_math_re.sub("<img src=\"" + os.path.basename(filename) + ".png\">", html, 1) 00118 else: 00119 break 00120 00121 # Save the file 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 # Test the module 00133 if __name__ == "__main__": 00134 # from_tex(r"$\frac{a}{b} = c$", "test", 200) 00135 # from_html("test.html", "test-out") 00136 from_html("en_asebastdnative.html", "test-out")