From: fiedorow Date: Tue, 10 Feb 2004 16:46:30 +0000 (+0000) Subject: Test version of TeX filter designed for moodle.org X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=c49dede89d470bca32104193ad64a814fb171fe3;p=moodle.git Test version of TeX filter designed for moodle.org --- diff --git a/filter/tex/mimetex.cgi b/filter/tex/mimetex.cgi new file mode 100755 index 0000000000..a9eee39eb9 Binary files /dev/null and b/filter/tex/mimetex.cgi differ diff --git a/filter/tex/tex_filter.php b/filter/tex/tex_filter.php new file mode 100644 index 0000000000..327165e153 --- /dev/null +++ b/filter/tex/tex_filter.php @@ -0,0 +1,182 @@ +... tags to gif images using +// mimetex.cgi obtained from http://www.forkosh.com/mimetex.html authored by +// John Forkosh john@forkosh.com. The mimetex.cgi ELF binary compiled for Linux i386 +// is included with this distribution. +// Note that there may be patent restrictions on the production of gif images +// in Canada and some parts of Western Europe and Japan until July 2004. +//------------------------------------------------------------------------- +// You will then need to edit your moodle/config.php to invoke tex_filter.php +//------------------------------------------------------------------------- + + +/// Edit these lines to correspond to your installation +// File path to the directory where mathml_filter.php resides + $CFG->filterDirectory = "/var/www/html/moodle1_2d/filter/tex"; +// File paths to the echo binary executable + $CFG->echo = "/bin/echo"; +// Frequency with which cache cleanup code is called: 119 means once in 119 times +// that the filter is invoked + $CFG->cacheCleanFreq = 119; +// Time in seconds after which image gifs which haven't been viewed are considered stale +// and are scheduled for deletion + $CFG->cacheCleanTime = 14*24*3600; +// Command used to list the oldest cached gif files to be scheduled for deletion, in +// conjunction with the value of cacheCleanTime + $CFG->cleanFiles = "cd ". $CFG->dataroot . "/1/tex_files;/bin/ls -tr | /usr/bin/head -20"; + + +/// These lines are important - the variable must match the name +/// of the actual function below + $textfilter_function='tex_filter'; + + if (function_exists($textfilter_function)) { + return; + } + + +function string_file_picture($path, $courseid=0, $height="", $width="", $link="") { + // Given the path to a picture file in a course, or a URL, + // this function includes the picture in the page. + global $CFG; + $output = ""; + if ($height) { + $height = "height=\"$height\""; + } + if ($width) { + $width = "width=\"$width\""; + } + if ($link) { + $output .= ""; + } + if (substr(strtolower($path), 0, 7) == "http://") { + $output .= ""; + + } else if ($courseid) { + $output .= "slasharguments) { // Use this method if possible for better caching + $output .= "$CFG->wwwroot/file.php/$courseid/$path"; + } else { + $output .= "$CFG->wwwroot/file.php?file=/$courseid/$path"; + } + $output .= "\" />"; + } else { + $output .= "Error: must pass URL or course"; + } + if ($link) { + $output .= ""; + } + return $output; +} + +function tex_filter ($courseid, $text) { + + global $CFG; + $filterDirectory = $CFG->filterDirectory; + + $scriptname = $_SERVER['SCRIPT_NAME']; + if (!strstr($scriptname,'/forum/')) { + return $text; + } + /// Do a quick check using stripos to avoid unnecessary wor + if (!preg_match('/discussion); + } else if (strstr($scriptname,'discuss.php')) { + $discussion = get_record("forum_discussions","id",$_GET['d'] ); + } else { + return $text; + } + if ($discussion->forum != 130) { + return $text; + } + + $old_umask = umask(); + + if (!file_exists($CFG->dataroot . "/1/")) { + mkdir($CFG->dataroot . "/1/",0775); + } + + if (!file_exists($CFG->dataroot . "/1/tex_files/")) { + mkdir($CFG->dataroot . "/1/tex_files/",0775); + } + umask($old_umask); + echo "\n\n"; + if (isadmin()) { error_reporting (E_ALL); }; //for debugging + $timenow = time(); + if (!($timenow % $CFG->cacheCleanFreq)) { + $cleanFiles = explode("\n",`$CFG->cleanFiles`); + foreach ($cleanFiles as $cleanFile) { + $pathname = $CFG->dataroot . "/1/tex_files/" . $cleanFile; + if ($timenow - filemtime($pathname)>$CFG->cacheCleanTime) { + unlink($pathname); + } else { + break; + } + } + } + + + $text .= ' '; + preg_match_all('/\$(\$\$+?)([^\$])/s',$text,$matches); + for ($i=0;$i TeX expression + // or $$ TeX expression $$ + + preg_match_all('/(.+?)<\/tex>|\$\$(.+?)\$\$/is', $text, $matches); + for ($i=0; $idataroot . "/1/" . $filename; + + if (file_exists($pathname)) { + touch($pathname); + $text = str_replace( $matches[0][$i], string_file_picture($filename, 1), $text); + } else { + $texexp = str_replace('<','<',$texexp); + $texexp = str_replace('>','>',$texexp); + $texexp = preg_replace('!\r\n?!',' ',$texexp); + + system("QUERY_STRING=;export QUERY_STRING;$filterDirectory/mimetex.cgi -d ". escapeshellarg($texexp) . " >$pathname"); + $text = str_replace( $matches[0][$i], string_file_picture($filename, 1), $text); + } + + } + return $text; +}; + + +?> \ No newline at end of file