3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Moodle - Filter for converting simple calculator-type algebraic
20 * expressions to cached gif images
24 * @copyright 2004 Zbigniew Fiedorowicz fiedorow@math.ohio-state.edu
25 * Originally based on code provided by Bruno Vernier bruno@vsbeducation.ca
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') ||
die();
31 //-------------------------------------------------------------------------
32 // NOTE: This Moodle text filter converts algebraic expressions delimited
33 // by either @@...@@ or by <algebra...>...</algebra> tags
34 // first converts it to TeX using WeBWorK algebra parser Perl library
35 // AlgParser.pm, part of the WeBWorK distribution obtained from
36 // http://webhost.math.rochester.edu/downloadwebwork/
37 // then converts the TeX to gif images using
38 // mimetex.cgi obtained from http://www.forkosh.com/mimetex.html authored by
39 // John Forkosh john@forkosh.com. The mimetex.cgi ELF binary compiled for Linux i386
40 // as well as AlgParser.pm are included with this distribution.
41 // Note that there may be patent restrictions on the production of gif images
42 // in Canada and some parts of Western Europe and Japan until July 2004.
43 //-------------------------------------------------------------------------
44 // You will then need to edit your moodle/config.php to invoke mathml_filter.php
45 //-------------------------------------------------------------------------
47 function filter_algebra_image($imagefile, $tex= "", $height="", $width="", $align="middle") {
48 // Given the path to a picture file in a course, or a URL,
49 // this function includes the picture in the page.
53 $style = 'style="border:0px; vertical-align:'.$align.';';
56 $tex = html_entity_decode($tex, ENT_QUOTES
, 'UTF-8');
57 $title = 'title="'.s($tex).'"';
60 $style .= " height:{$height}px;";
63 $style .= " width:{$width}px;";
68 $anchorcontents .= "<img $title alt=\"".s($tex)."\" src=\"";
69 if ($CFG->slasharguments
) { // Use this method if possible for better caching
70 $anchorcontents .= "$CFG->wwwroot/filter/algebra/pix.php/$imagefile";
72 $anchorcontents .= "$CFG->wwwroot/filter/algebra/pix.php?file=$imagefile";
74 $anchorcontents .= "\" $style />";
76 if (!file_exists("$CFG->dataroot/filter/algebra/$imagefile") && has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM
))) {
77 $link = '/filter/algebra/algebradebug.php';
80 $link = new moodle_url('/filter/tex/displaytex.php', array('texexp'=>$tex));
81 $action = new popup_action('click', $link, 'popup', array('width'=>320,'height'=>240)); //TODO: the popups do not work when text caching is enabled!!
83 $output .= $OUTPUT->action_link($link, $anchorcontents, $action, array('title'=>'TeX'));
86 $output .= "Error: must pass URL or course";
91 class filter_algebra
extends moodle_text_filter
{
92 function filter($text, array $options = array()){
95 /// Do a quick check using stripos to avoid unnecessary wor
96 if (!preg_match('/<algebra/i',$text) && !strstr($text,'@@')) {
100 //restrict filtering to forum 130 (Maths Tools on moodle.org)
101 # $scriptname = $_SERVER['SCRIPT_NAME'];
102 # if (!strstr($scriptname,'/forum/')) {
105 # if (strstr($scriptname,'post.php')) {
106 # $parent = forum_get_post_full($_GET['reply']);
107 # $discussion = $DB->get_record("forum_discussions",array("id"=>$parent->discussion));
108 # } else if (strstr($scriptname,'discuss.php')) {
109 # $discussion = $DB->get_record("forum_discussions",array("id"=>$_GET['d']));
113 # if ($discussion->forum != 130) {
120 preg_match_all('/@(@@+)([^@])/',$text,$matches);
121 for ($i=0;$i<count($matches[0]);$i++
) {
122 $replacement = str_replace('@','@',$matches[1][$i]).$matches[2][$i];
123 $text = str_replace($matches[0][$i],$replacement,$text);
126 // <algebra> some algebraic input expression </algebra>
127 // or @@ some algebraic input expression @@
129 preg_match_all('/<algebra>(.+?)<\/algebra>|@@(.+?)@@/is', $text, $matches);
130 for ($i=0; $i<count($matches[0]); $i++
) {
131 $algebra = $matches[1][$i] . $matches[2][$i];
132 $algebra = str_replace('<nolink>','',$algebra);
133 $algebra = str_replace('</nolink>','',$algebra);
134 $algebra = str_replace('<span class="nolink">','',$algebra);
135 $algebra = str_replace('</span>','',$algebra);
137 if (preg_match('/^align=bottom /',$algebra)) {
138 $align = "text-bottom";
139 $algebra = preg_replace('/^align=bottom /','',$algebra);
140 } else if (preg_match('/^align=top /',$algebra)) {
142 $algebra = preg_replace('/^align=top /','',$algebra);
144 $md5 = md5($algebra);
145 $filename = $md5 . ".gif";
146 if (! $texcache = $DB->get_record("cache_filters",array("filter"=>"algebra", "md5key"=>$md5))) {
147 $algebra = str_replace('<','<',$algebra);
148 $algebra = str_replace('>','>',$algebra);
149 $algebra = str_replace('<>','#',$algebra);
150 $algebra = str_replace('<=','%',$algebra);
151 $algebra = str_replace('>=','!',$algebra);
152 $algebra = preg_replace('/([=><%!#] *)-/',"\$1 zeroplace -",$algebra);
153 $algebra = str_replace('delta','zdelta',$algebra);
154 $algebra = str_replace('beta','bita',$algebra);
155 $algebra = str_replace('theta','thita',$algebra);
156 $algebra = str_replace('zeta','zita',$algebra);
157 $algebra = str_replace('eta','xeta',$algebra);
158 $algebra = str_replace('epsilon','zepslon',$algebra);
159 $algebra = str_replace('upsilon','zupslon',$algebra);
160 $algebra = preg_replace('!\r\n?!',' ',$algebra);
161 $algebra = escapeshellarg($algebra);
162 if ( (PHP_OS
== "WINNT") ||
(PHP_OS
== "WIN32") ||
(PHP_OS
== "Windows") ) {
163 $cmd = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl $algebra";
165 $cmd = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl $algebra";
168 if (preg_match('/parsehilight/',$texexp)) {
169 $text = str_replace( $matches[0][$i],"<b>Syntax error:</b> " . $texexp,$text);
170 } else if ($texexp) {
171 $texexp = str_replace('zeroplace','',$texexp);
172 $texexp = str_replace('#','\not= ',$texexp);
173 $texexp = str_replace('%','\leq ',$texexp);
174 $texexp = str_replace('!','\geq ',$texexp);
175 $texexp = str_replace('\left{','{',$texexp);
176 $texexp = str_replace('\right}','}',$texexp);
177 $texexp = str_replace('\fun',' ',$texexp);
178 $texexp = str_replace('infty','\infty',$texexp);
179 $texexp = str_replace('alpha','\alpha',$texexp);
180 $texexp = str_replace('gamma','\gamma',$texexp);
181 $texexp = str_replace('iota','\iota',$texexp);
182 $texexp = str_replace('kappa','\kappa',$texexp);
183 $texexp = str_replace('lambda','\lambda',$texexp);
184 $texexp = str_replace('mu','\mu',$texexp);
185 $texexp = str_replace('nu','\nu',$texexp);
186 $texexp = str_replace('xi','\xi',$texexp);
187 $texexp = str_replace('rho','\rho',$texexp);
188 $texexp = str_replace('sigma','\sigma',$texexp);
189 $texexp = str_replace('tau','\tau',$texexp);
190 $texexp = str_replace('phi','\phi',$texexp);
191 $texexp = str_replace('chi','\chi',$texexp);
192 $texexp = str_replace('psi','\psi',$texexp);
193 $texexp = str_replace('omega','\omega',$texexp);
194 $texexp = str_replace('zdelta','\delta',$texexp);
195 $texexp = str_replace('bita','\beta',$texexp);
196 $texexp = str_replace('thita','\theta',$texexp);
197 $texexp = str_replace('zita','\zeta',$texexp);
198 $texexp = str_replace('xeta','\eta',$texexp);
199 $texexp = str_replace('zepslon','\epsilon',$texexp);
200 $texexp = str_replace('zupslon','\upsilon',$texexp);
201 $texexp = str_replace('\mbox{logten}','\mbox{log}_{10}',$texexp);
202 $texexp = str_replace('\mbox{acos}','\mbox{cos}^{-1}',$texexp);
203 $texexp = str_replace('\mbox{asin}','\mbox{sin}^{-1}',$texexp);
204 $texexp = str_replace('\mbox{atan}','\mbox{tan}^{-1}',$texexp);
205 $texexp = str_replace('\mbox{asec}','\mbox{sec}^{-1}',$texexp);
206 $texexp = str_replace('\mbox{acsc}','\mbox{csc}^{-1}',$texexp);
207 $texexp = str_replace('\mbox{acot}','\mbox{cot}^{-1}',$texexp);
208 $texexp = str_replace('\mbox{acosh}','\mbox{cosh}^{-1}',$texexp);
209 $texexp = str_replace('\mbox{asinh}','\mbox{sinh}^{-1}',$texexp);
210 $texexp = str_replace('\mbox{atanh}','\mbox{tanh}^{-1}',$texexp);
211 $texexp = str_replace('\mbox{asech}','\mbox{sech}^{-1}',$texexp);
212 $texexp = str_replace('\mbox{acsch}','\mbox{csch}^{-1}',$texexp);
213 $texexp = str_replace('\mbox{acoth}','\mbox{coth}^{-1}',$texexp);
214 //$texexp = preg_replace('/\\\frac{(.+?)}{\\\left\((.+?)\\\right\)}/s','\frac{'."\$1}{\$2}",$texexp);
215 $texexp = preg_replace('/\\\sqrt{(.+?),(.+?)}/s','\sqrt['. "\$2]{\$1}",$texexp);
216 $texexp = preg_replace('/\\\mbox{abs}\\\left\((.+?)\\\right\)/s',"|\$1|",$texexp);
217 $texexp = preg_replace('/\\\log\\\left\((.+?),(.+?)\\\right\)/s','\log_{'. "\$2}\\left(\$1\\right)",$texexp);
218 $texexp = preg_replace('/(\\\cos|\\\sin|\\\tan|\\\sec|\\\csc|\\\cot)([h]*)\\\left\((.+?),(.+?)\\\right\)/s',"\$1\$2^{". "\$4}\\left(\$3\\right)",$texexp);
219 $texexp = preg_replace('/\\\int\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\int_'. "{\$2}^{\$3}\$1 ",$texexp);
220 $texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp);
221 $texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp);
222 $texexp = str_replace('\mbox', '', $texexp); // now blacklisted in tex, sorry
223 $texcache->filter
= 'algebra';
224 $texcache->version
= 1;
225 $texcache->md5key
= $md5;
226 $texcache->rawtext
= $texexp;
227 $texcache->timemodified
= time();
228 $DB->insert_record("cache_filters", $texcache, false);
229 $text = str_replace( $matches[0][$i], filter_algebra_image($filename, $texexp, '', '', $align), $text);
231 $text = str_replace( $matches[0][$i],"<b>Undetermined error:</b> ",$text);
234 $text = str_replace( $matches[0][$i], filter_algebra_image($filename, $texcache->rawtext
), $text);