00001 <?php
00002
00009 class LatexRenderModule {
00010
00014 var $LATEX_PATH = "/usr/bin/latex";
00015 var $DVIPS_PATH = "/usr/bin/dvips";
00016 var $CONVERT_PATH = "/usr/bin/convert";
00017
00021 var $TMP_DIR =
00022 "/var/tmp/texcache/tmp";
00023 var $CACHE_DIR =
00024 "/var/tmp/texcache/cache";
00025
00029 var $URL_PATH = "/pr2/components/texCache.php5?fileName=";
00030
00031
00032
00041 function wrap($thunk) {
00042
00043 $newThunk = <<<EOS
00044 \documentclass[10pt]{article}
00045
00046 % add additional packages here
00047 \usepackage{amsmath}
00048 \usepackage{amsfonts}
00049 \usepackage{amssymb}
00050 \usepackage{pst-plot}
00051 \usepackage{color}
00052
00053 \pagestyle{empty}
00054 \begin{document}
00055 $thunk
00056 \end{document}
00057 EOS;
00058
00059
00060
00061 return $newThunk;
00062 }
00063
00064
00073 function transform($text) {
00074 $thunk = $text;
00075 $hash = md5($thunk);
00076
00077 $full_name = $this->CACHE_DIR . "/" .
00078 $hash . ".png";
00079 $url = SPath::getRelPath("Common") . $this->URL_PATH . $hash;
00080
00081 if (!is_file($full_name)) {
00082 $this->render_latex($thunk, $hash);
00083
00084 }
00085
00086 $text = "<img src=\"$url\" alt=\"tex\" class=\"latex StdXML3\" >";
00087
00088
00089 return $text;
00090 }
00100 function render_latex($thunk, $hash) {
00101
00102 $thunk = html_entity_decode($thunk);
00103 $thunk = $this->wrap($thunk);
00104 $current_dir = getcwd();
00105
00106
00107
00108 $fp = fopen($this->TMP_DIR . "/$hash.tex", "w+");
00109 fwrite($fp, $thunk);
00110 fclose($fp);
00111
00112 $dir = dirname(__FILE__);
00113 $dirString = "sh " . $dir . "/ps2png.sh " . $hash;
00114
00115 exec($dirString);
00116
00117
00118 copy($this->TMP_DIR . "/$hash.png", $this->CACHE_DIR . "/$hash.png");
00119
00120 chdir($current_dir);
00121 }
00122
00131 function cleanup($hash) {
00132 $current_dir = getcwd();
00133 chdir($this->TMP_DIR);
00134
00135 unlink($this->TMP_DIR . "/$hash.tex");
00136 unlink($this->TMP_DIR . "/$hash.aux");
00137 unlink($this->TMP_DIR . "/$hash.log");
00138 unlink($this->TMP_DIR . "/$hash.dvi");
00139 unlink($this->TMP_DIR . "/$hash.ps");
00140 unlink($this->TMP_DIR . "/$hash.png");
00141
00142 chdir($current_dir);
00143 }
00144
00145 }
00146
00147 ?>