From f4d7ae95dbb3d51b5aee45dd55407febf32d3878 Mon Sep 17 00:00:00 2001 From: pmeignen Date: Mon, 21 Jun 2004 10:50:36 +0000 Subject: [PATCH] Adding comments git-svn-id: svn://svn.code.sf.net/p/phpwiki/code/trunk@3751 96ab9672-09ca-45d6-a79d-3d69d39ca109 --- lib/plugin/TeX2png.php | 253 ++++++++++++++++++++++------------------- 1 file changed, 138 insertions(+), 115 deletions(-) diff --git a/lib/plugin/TeX2png.php b/lib/plugin/TeX2png.php index ddfde2c52..c4d835253 100644 --- a/lib/plugin/TeX2png.php +++ b/lib/plugin/TeX2png.php @@ -1,5 +1,5 @@ - // url: RecentChanges?days=1&show_all=1&show_minor=0 - assert($op == '||='); - $defaults[$arg] = $val; - } - } - - if ($argstr) { - $this->handle_plugin_args_cruft($argstr, $args); - } - - return array($args, $defaults); + $qq_p = '" ( (?:[^"\\\\]|\\\\.)* ) "'; + //"<--kludge for brain-dead syntax coloring + $q_p = "' ( (?:[^'\\\\]|\\\\.)* ) '"; + $gt_p = "_\\( $opt_ws $qq_p $opt_ws \\)"; + $argspec_p = "($arg_p) $opt_ws ($op_p) $opt_ws (?: $qq_p|$q_p|$gt_p|($word_p))"; + + $args = array(); + $defaults = array(); + + while (preg_match("/^$opt_ws $argspec_p $opt_ws/x", $argstr, $m)) { + @ list(,$arg,$op,$qq_val,$q_val,$gt_val,$word_val) = $m; + $argstr = substr($argstr, strlen($m[0])); + + // Remove quotes from string values. + if ($qq_val) + // we don't remove backslashes in tex formulas + // $val = stripslashes($qq_val); + $val = $qq_val; + elseif ($q_val) + $val = stripslashes($q_val); + elseif ($gt_val) + $val = _(stripslashes($gt_val)); + else + $val = $word_val; + + if ($op == '=') { + $args[$arg] = $val; + } + else { + // NOTE: This does work for multiple args. Use the + // separator character defined in your webserver + // configuration, usually & or & (See + // http://www.htmlhelp.com/faq/cgifaq.4.html) + // e.g. + // url: RecentChanges?days=1&show_all=1&show_minor=0 + assert($op == '||='); + $defaults[$arg] = $val; + } + } + + if ($argstr) { + $this->handle_plugin_args_cruft($argstr, $args); + } + + return array($args, $defaults); } - + function createTexFile($texfile, $text) { - $fp = fopen($texfile, 'w'); - $str = "\documentclass{article}\n"; - $str .= "\usepackage{amsfonts}\n"; - $str .= "\usepackage{amssymb}\n"; - $str .= "\pagestyle{empty}\n"; - $str .= "\begin{document}\n"; - $str .= $text . "\n"; - $str .= "\end{document}"; - fwrite($fp, $str); - fclose($fp); - return 0; + // this is the small latex file + // which contains only the mathematical + // expression + $fp = fopen($texfile, 'w'); + $str = "\documentclass{article}\n"; + $str .= "\usepackage{amsfonts}\n"; + $str .= "\usepackage{amssymb}\n"; + // Here tou can add some package in order + // to produce more sophisticated output + $str .= "\pagestyle{empty}\n"; + $str .= "\begin{document}\n"; + $str .= $text . "\n"; + $str .= "\end{document}"; + fwrite($fp, $str); + fclose($fp); + return 0; } function createPngFile($imagepath, $imagename) { - $commandes = "$latexbin temp.tex; $dvipsbin temp.dvi -o temp.ps;"; - $commandes .= "$pstoimgbin -type png -margins 0,0 -crop a -geometry 600x300"; - $commandes .= " -aaliastext -color 1"; - $options = " -scale 1.5 "; - $commandes .= $commandes . $options . "temp.ps -o " . $imagename; - exec("cd $imagepath; $commandes"); - unlink("$imagepath/temp.dvi"); - unlink("$imagepath/temp.tex"); - unlink("$imagepath/temp.aux"); - unlink("$imagepath/temp.log"); - unlink("$imagepath/temp.ps"); - return 0; + // to create dvi file from the latex file + $commandes = $this->latexbin . " temp.tex;"; + exec("cd $imagepath;$commandes"); + // to create png file from the dvi file + // there is no option but it is possible + // to add one (scale for example) + if (file_exists("$imagepath/temp.dvi")){ + $commandes = $this->dvipsbin . " temp.dvi -o temp.ps;"; + $commandes .= $this->pstoimgbin . " -type png -margins 0,0 "; + $commandes .= "-crop a -geometry 600x300 "; + $commandes .= "-aaliastext -color 1 -scale 1.5 "; + $commandes .= "temp.ps -o " . $imagename; + exec("cd $imagepath;$commandes"); + unlink("$imagepath/temp.dvi"); + unlink("$imagepath/temp.ps"); + } else + echo _(" (syntax error for latex) "); + // to clean the directory + unlink("$imagepath/temp.tex"); + unlink("$imagepath/temp.aux"); + unlink("$imagepath/temp.log"); + return 0; } - - function isMathExp($text) { - $last = strlen($text) - 1; - if($text[0] != "$" || $text[$last] != "$") - return 0; - else if ($text[1] == "$" && $text[$last - 1] == "$") - return 2; - return 1; + + function isMathExp(&$text) { + // this function returns + // 0 : text is too long or not a mathematical expression + // 1 : text is $xxxxxx$ hence in line + // 2 : text is $$xxxx$$ hence centered + $last = strlen($text) - 1; + if($last >= 250){ + $text = "Too long !"; + return 0; + } elseif($last <= 1 || strpos($text, '$') != 0){ + return 0; + } elseif(strpos($text, '$', 1) == $last) + return 1; + elseif($last > 3 && + strpos($text, '$', 1) == 1 && + strpos($text, '$', 2) == $last - 1) + return 2; + return 0; } function tex2png($text) { - if($this->isMathExp($text) == 0){ - $error_html =_("Sorry, not a full mathematical expression: " . $text); - trigger_error($error_html, E_USER_NOTICE); - } else { - if (!file_exists($this->imagepath)) { - $oldumask = umask(0); - // permissions affected by user the www server is running as - mkdir($this->imagepath, 0777); - umask($oldumask); - } - if (!file_exists($this->imagepath)) { - trigger_error(sprintf("Failed to mkdir '%s'.", $this->imagepath), E_USER_WARNING); - return ''; - } + // the name of the png cached file + $imagename = md5($text) . ".png"; + $url = $this->imagepath . "/$imagename"; - $imagename = md5($text) . ".png"; - $url = $this->imagepath . "/" . $imagename; - - if(!file_exists($url)){ - $texfile = $this->imagepath . "/temp.tex"; - $this->createTexFile($texfile, $text); - $this->createPngFile($this->imagepath, $imagename); - } - } + if(!file_exists($url)){ + if(is_writable($this->imagepath)){ + $texfile = $this->imagepath . "/temp.tex"; + $this->createTexFile($texfile, $text); + $this->createPngFile($this->imagepath, $imagename); + } else { + $error_html = _("TeX directory not writable."); + trigger_error($error_html, E_USER_NOTICE); + } + } - switch($this->isMathExp($text)) { - case 0: - $html = HTML::tt(array('class'=>'tex', 'style'=>'color:red;'), - $text); + // there is always something in the html page + // even if the tex directory doesn't exist + // or mathematical expression is wrong + switch($this->isMathExp($text)) { + case 0: // not a mathematical expression + $html = HTML::tt(array('class'=>'tex', + 'style'=>'color:red;'), $text); break; - case 1: - $html = HTML::img(array('class'=>'tex', 'src' => $url, 'alt' => $text)); + case 1: // an inlined mathematical expression + $html = HTML::img(array('class'=>'tex', + 'src' => $url, + 'alt' => $text)); break; - case 2: - $html = HTML::img(array('class'=>'tex', 'src' => $url, 'alt' => $text)); + case 2: // mathematical expression on separate line + $html = HTML::img(array('class'=>'tex', + 'src' => $url, + 'alt' => $text)); $html = HTML::div(array('align' => 'center'), $html); break; default: break; } - - return $html; + + return $html; } - + function run($dbi, $argstr, &$request, $basepage) { // from text2png.php if (ImageTypes() & IMG_PNG) { // we have gd & png so go ahead. extract($this->getArgs($argstr, $request)); - return $this->tex2png($text); + return $this->tex2png($text); } else { // we don't have png and/or gd. $error_html = _("Sorry, this version of PHP cannot create PNG image files."); -- 2.45.0