weekly release
[moodle.git] / filter / tex / filter.php
CommitLineData
35716b86
PS
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * Moodle - Filter for converting TeX expressions to cached gif images
20 * @package filter
21 * @subpackage tex
22 * @copyright 2004 Zbigniew Fiedorowicz fiedorow@math.ohio-state.edu
23 * Originally based on code provided by Bruno Vernier bruno@vsbeducation.ca
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
c49dede8 27//-------------------------------------------------------------------------
28// NOTE: This Moodle text filter converts TeX expressions delimited
29// by either $$...$$ or by <tex...>...</tex> tags to gif images using
30// mimetex.cgi obtained from http://www.forkosh.com/mimetex.html authored by
220a90c5 31// John Forkosh john@forkosh.com. Several binaries of this areincluded with
32// this distribution.
c49dede8 33// Note that there may be patent restrictions on the production of gif images
34// in Canada and some parts of Western Europe and Japan until July 2004.
35//-------------------------------------------------------------------------
4da85078 36/////////////////////////////////////////////////////////////////////////////
37// To activate this filter, add a line like this to your //
38// list of filters in your Filter configuration: //
39// //
40// filter/tex/filter.php //
41/////////////////////////////////////////////////////////////////////////////
c49dede8 42
35716b86 43function filter_text_image($imagefile, $tex= "", $height="", $width="", $align="middle", $alt='') {
e4cd4de2 44 global $CFG, $OUTPUT;
c49dede8 45
c1d9eeb9 46 if ($alt==='') {
47 $alt = s($tex);
48 }
49
50 // Work out any necessary inline style.
51 $rules = array();
e47d5ade 52 if ($align !== 'middle') {
c1d9eeb9 53 $rules[] = 'vertical-align:' . $align . ';';
e47d5ade 54 }
55 if ($height) {
c1d9eeb9 56 $rules[] = 'height:' . $height . 'px;';
e47d5ade 57 }
58 if ($width) {
c1d9eeb9 59 $rules[] = 'width:' . $width . 'px;';
e47d5ade 60 }
c1d9eeb9 61 if (!empty($rules)) {
62 $style = ' style="' . implode('', $rules) . '" ';
63 } else {
64 $style = '';
65 }
66
67 // Prepare the title attribute.
9d49b430 68 if ($tex) {
69 $tex = str_replace('&','&amp;',$tex);
70 $tex = str_replace('<','&lt;',$tex);
71 $tex = str_replace('>','&gt;',$tex);
72 $tex = str_replace('"','&quot;',$tex);
73 $tex = str_replace("\'",'&#39;',$tex);
f5ceb06a 74 // Note that we retain the title tag as TeX format rather than using
4317f92f
PS
75 // the alt text, even if supplied. The alt text is intended for blind
76 // users (to provide a text equivalent to the equation) while the title
77 // is there as a convenience for sighted users who want to see the TeX
78 // code.
9d49b430 79 $title = "title=\"$tex\"";
80 }
c1d9eeb9 81
82 // Build the output.
83 $output = "";
9d49b430 84 if ($imagefile) {
eaed2fd1
AD
85 $anchorcontents = "<img class=\"texrender\" $title alt=\"$alt\" src=\"";
86 if ($CFG->slasharguments) { // Use this method if possible for better caching
87 $anchorcontents .= "$CFG->wwwroot/filter/tex/pix.php/$imagefile";
d633b4eb 88 } else {
eaed2fd1 89 $anchorcontents .= "$CFG->wwwroot/filter/tex/pix.php?file=$imagefile";
894ff63f 90 }
eaed2fd1
AD
91 $anchorcontents .= "\" $style/>";
92
93 $link = $action = null;
94 if (!file_exists("$CFG->dataroot/filter/tex/$imagefile") && has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
95 $link = '/filter/tex/texdebug.php';
9d49b430 96 } else {
eaed2fd1
AD
97 $link = '/filter/tex/displaytex.php?'.urlencode($tex);
98 $action = new popup_action('click', $link, 'popup', array('height'=>300,'width'=>240));
9d49b430 99 }
e4cd4de2 100 $output .= $OUTPUT->action_link($link, $anchorcontents, $action, array('title'=>'TeX'));
c49dede8 101 } else {
9d49b430 102 $output .= "Error: must pass URL or course";
c49dede8 103 }
9d49b430 104 return $output;
c49dede8 105}
106
35716b86 107class filter_tex extends moodle_text_filter {
dcfffe30 108 function filter ($text, array $options = array()) {
9e3f34d1 109
110 global $CFG, $DB;
111
112 /// Do a quick check using stripos to avoid unnecessary work
113 if (!preg_match('/<tex/i',$text) and !strstr($text,'$$') and !strstr($text,'\\[') and !preg_match('/\[tex/i',$text)) { //added one more tag (dlnsk)
114 return $text;
115 }
c49dede8 116
9d49b430 117# //restrict filtering to forum 130 (Maths Tools on moodle.org)
118# $scriptname = $_SERVER['SCRIPT_NAME'];
119# if (!strstr($scriptname,'/forum/')) {
120# return $text;
121# }
122# if (strstr($scriptname,'post.php')) {
123# $parent = forum_get_post_full($_GET['reply']);
82a5323c 124# $discussion = $DB->get_record("forum_discussions", array("id"=>$parent->discussion));
9d49b430 125# } else if (strstr($scriptname,'discuss.php')) {
82a5323c 126# $discussion = $DB->get_record("forum_discussions", array("id"=>$_GET['d']));
9d49b430 127# } else {
128# return $text;
129# }
130# if ($discussion->forum != 130) {
131# return $text;
132# }
9e3f34d1 133 $text .= ' ';
134 preg_match_all('/\$(\$\$+?)([^\$])/s',$text,$matches);
135 for ($i=0;$i<count($matches[0]);$i++) {
136 $replacement = str_replace('$','&#x00024;',$matches[1][$i]).$matches[2][$i];
137 $text = str_replace($matches[0][$i],$replacement,$text);
f352814a 138 }
9e3f34d1 139
140 // <tex> TeX expression </tex>
141 // or <tex alt="My alternative text to be used instead of the TeX form"> TeX expression </tex>
142 // or $$ TeX expression $$
143 // or \[ TeX expression \] // original tag of MathType and TeXaide (dlnsk)
144 // or [tex] TeX expression [/tex] // somtime it's more comfortable than <tex> (dlnsk)
145 preg_match_all('/<tex(?:\s+alt=["\'](.*?)["\'])?>(.+?)<\/tex>|\$\$(.+?)\$\$|\\\\\[(.+?)\\\\\]|\\[tex\\](.+?)\\[\/tex\\]/is', $text, $matches);
146 for ($i=0; $i<count($matches[0]); $i++) {
147 $texexp = $matches[2][$i] . $matches[3][$i] . $matches[4][$i] . $matches[5][$i];
148 $alt = $matches[1][$i];
149 $texexp = str_replace('<nolink>','',$texexp);
150 $texexp = str_replace('</nolink>','',$texexp);
151 $texexp = str_replace('<span class="nolink">','',$texexp);
152 $texexp = str_replace('</span>','',$texexp);
c78a948e 153 $texexp = preg_replace("/<br[[:space:]]*\/?>/i", '', $texexp); //dlnsk
9e3f34d1 154 $align = "middle";
155 if (preg_match('/^align=bottom /',$texexp)) {
156 $align = "text-bottom";
157 $texexp = preg_replace('/^align=bottom /','',$texexp);
158 } else if (preg_match('/^align=top /',$texexp)) {
159 $align = "text-top";
160 $texexp = preg_replace('/^align=top /','',$texexp);
161 }
162 $md5 = md5($texexp);
163 if (! $texcache = $DB->get_record("cache_filters", array("filter"=>"tex", "md5key"=>$md5))) {
164 $texcache->filter = 'tex';
165 $texcache->version = 1;
166 $texcache->md5key = $md5;
167 $texcache->rawtext = $texexp;
168 $texcache->timemodified = time();
169 $DB->insert_record("cache_filters", $texcache, false);
170 }
cf4e7548 171 $filename = $md5 . ".{$CFG->filter_tex_convertformat}";
35716b86 172 $text = str_replace( $matches[0][$i], filter_text_image($filename, $texexp, '', '', $align, $alt), $text);
c49dede8 173 }
9e3f34d1 174 return $text;
c49dede8 175 }
9d49b430 176}
c49dede8 177
4317f92f 178