adding some strings
[moodle-linuxchix.git] / filter / tex / filter.php
blobe8b0709e0bb1193c3303a1875058a6adc59c05ac
1 <?PHP
2 /////////////////////////////////////////////////////////////////////////////
3 // //
4 // NOTICE OF COPYRIGHT //
5 // //
6 // Moodle - Filter for converting TeX expressions to cached gif images //
7 // //
8 // Copyright (C) 2004 Zbigniew Fiedorowicz fiedorow@math.ohio-state.edu //
9 // Originally based on code provided by Bruno Vernier bruno@vsbeducation.ca//
10 // This program is free software; you can redistribute it and/or modify //
11 // it under the terms of the GNU General Public License as published by //
12 // the Free Software Foundation; either version 2 of the License, or //
13 // (at your option) any later version. //
14 // //
15 // This program is distributed in the hope that it will be useful, //
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
18 // GNU General Public License for more details: //
19 // //
20 // http://www.gnu.org/copyleft/gpl.html //
21 // //
22 /////////////////////////////////////////////////////////////////////////////
23 //-------------------------------------------------------------------------
24 // NOTE: This Moodle text filter converts TeX expressions delimited
25 // by either $$...$$ or by <tex...>...</tex> tags to gif images using
26 // mimetex.cgi obtained from http://www.forkosh.com/mimetex.html authored by
27 // John Forkosh john@forkosh.com. Several binaries of this areincluded with
28 // this distribution.
29 // Note that there may be patent restrictions on the production of gif images
30 // in Canada and some parts of Western Europe and Japan until July 2004.
31 //-------------------------------------------------------------------------
32 /////////////////////////////////////////////////////////////////////////////
33 // To activate this filter, add a line like this to your //
34 // list of filters in your Filter configuration: //
35 // //
36 // filter/tex/filter.php //
37 /////////////////////////////////////////////////////////////////////////////
39 $CFG->texfilterdir = "filter/tex";
42 function string_file_picture_tex($imagefile, $tex= "", $height="", $width="", $align="middle") {
43 // Given the path to a picture file in a course, or a URL,
44 // this function includes the picture in the page.
45 global $CFG;
47 $output = "";
48 $origtex = $tex;
49 $style = 'style="border:0px; vertical-align:'.$align.';';
50 if ($tex) {
51 $tex = str_replace('&','&amp;',$tex);
52 $tex = str_replace('<','&lt;',$tex);
53 $tex = str_replace('>','&gt;',$tex);
54 $tex = str_replace('"','&quot;',$tex);
55 $tex = str_replace("\'",'&#39;',$tex);
56 $title = "title=\"$tex\"";
58 if ($height) {
59 $style .= " height:{$height}px;";
61 if ($width) {
62 $style .= " width:{$width}px;";
64 $style .= '"';
65 if ($imagefile) {
66 if (!file_exists("$CFG->dataroot/$CFG->texfilterdir/$imagefile") && has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
67 $output .= "<a href=\"$CFG->wwwroot/$CFG->texfilterdir/texdebug.php\">";
68 } else {
69 $output .= "<a target=\"popup\" title=\"TeX\" href=";
70 $output .= "\"$CFG->wwwroot/$CFG->texfilterdir/displaytex.php?";
71 $output .= urlencode($tex) . "\" onclick=\"return openpopup('/$CFG->texfilterdir/displaytex.php?";
72 $output .= urlencode($tex) . "', 'popup', 'menubar=0,location=0,scrollbars,";
73 $output .= "resizable,width=300,height=240', 0);\">";
75 $output .= "<img class=\"texrender\" $title alt=\"".s($origtex)."\" src=\"";
76 if ($CFG->slasharguments) { // Use this method if possible for better caching
77 $output .= "$CFG->wwwroot/$CFG->texfilterdir/pix.php/$imagefile";
78 } else {
79 $output .= "$CFG->wwwroot/$CFG->texfilterdir/pix.php?file=$imagefile";
81 $output .= "\" $style />";
82 $output .= "</a>";
83 } else {
84 $output .= "Error: must pass URL or course";
86 return $output;
89 function tex_filter ($courseid, $text) {
91 global $CFG;
93 /// Do a quick check using stripos to avoid unnecessary work
94 if (!preg_match('/<tex/i',$text) and !strstr($text,'$$') and !strstr($text,'\\[') and !preg_match('/\[tex/i',$text)) { //added one more tag (dlnsk)
95 return $text;
98 # //restrict filtering to forum 130 (Maths Tools on moodle.org)
99 # $scriptname = $_SERVER['SCRIPT_NAME'];
100 # if (!strstr($scriptname,'/forum/')) {
101 # return $text;
103 # if (strstr($scriptname,'post.php')) {
104 # $parent = forum_get_post_full($_GET['reply']);
105 # $discussion = get_record("forum_discussions","id",$parent->discussion);
106 # } else if (strstr($scriptname,'discuss.php')) {
107 # $discussion = get_record("forum_discussions","id",$_GET['d'] );
108 # } else {
109 # return $text;
111 # if ($discussion->forum != 130) {
112 # return $text;
114 $text .= ' ';
115 preg_match_all('/\$(\$\$+?)([^\$])/s',$text,$matches);
116 for ($i=0;$i<count($matches[0]);$i++) {
117 $replacement = str_replace('$','&#x00024;',$matches[1][$i]).$matches[2][$i];
118 $text = str_replace($matches[0][$i],$replacement,$text);
121 // <tex> TeX expression </tex>
122 // or $$ TeX expression $$
123 // or \[ TeX expression \] // original tag of MathType and TeXaide (dlnsk)
124 // or [tex] TeX expression [/tex] // somtime it's more comfortable than <tex> (dlnsk)
125 preg_match_all('/<tex>(.+?)<\/tex>|\$\$(.+?)\$\$|\\\\\[(.+?)\\\\\]|\\[tex\\](.+?)\\[\/tex\\]/is', $text, $matches);
126 for ($i=0; $i<count($matches[0]); $i++) {
127 $texexp = $matches[1][$i] . $matches[2][$i] . $matches[3][$i] . $matches[4][$i];
128 $texexp = str_replace('<nolink>','',$texexp);
129 $texexp = str_replace('</nolink>','',$texexp);
130 $texexp = str_replace('<span class="nolink">','',$texexp);
131 $texexp = str_replace('</span>','',$texexp);
132 $texexp = eregi_replace("<br[[:space:]]*\/?>", '', $texexp); //dlnsk
133 $align = "middle";
134 if (preg_match('/^align=bottom /',$texexp)) {
135 $align = "text-bottom";
136 $texexp = preg_replace('/^align=bottom /','',$texexp);
137 } else if (preg_match('/^align=top /',$texexp)) {
138 $align = "text-top";
139 $texexp = preg_replace('/^align=top /','',$texexp);
141 $md5 = md5($texexp);
142 if (! $texcache = get_record("cache_filters","filter","tex", "md5key", $md5)) {
143 $texcache->filter = 'tex';
144 $texcache->version = 1;
145 $texcache->md5key = $md5;
146 $texcache->rawtext = addslashes($texexp);
147 $texcache->timemodified = time();
148 insert_record("cache_filters",$texcache, false);
150 $filename = $md5 . ".gif";
151 $text = str_replace( $matches[0][$i], string_file_picture_tex($filename, $texexp, '', '', $align), $text);
153 return $text;