MDL-23479 backup - choice revised & old code deleted (but restore logs)
[moodle.git] / filter / tex / filter.php
CommitLineData
c49dede8 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
220a90c5 27// John Forkosh john@forkosh.com. Several binaries of this areincluded with
28// this distribution.
c49dede8 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//-------------------------------------------------------------------------
4da85078 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/////////////////////////////////////////////////////////////////////////////
c49dede8 38
f5ceb06a 39function string_file_picture_tex($imagefile, $tex= "", $height="", $width="", $align="middle", $alt='') {
e4cd4de2 40 global $CFG, $OUTPUT;
c49dede8 41
c1d9eeb9 42 if ($alt==='') {
43 $alt = s($tex);
44 }
45
46 // Work out any necessary inline style.
47 $rules = array();
e47d5ade 48 if ($align !== 'middle') {
c1d9eeb9 49 $rules[] = 'vertical-align:' . $align . ';';
e47d5ade 50 }
51 if ($height) {
c1d9eeb9 52 $rules[] = 'height:' . $height . 'px;';
e47d5ade 53 }
54 if ($width) {
c1d9eeb9 55 $rules[] = 'width:' . $width . 'px;';
e47d5ade 56 }
c1d9eeb9 57 if (!empty($rules)) {
58 $style = ' style="' . implode('', $rules) . '" ';
59 } else {
60 $style = '';
61 }
62
63 // Prepare the title attribute.
9d49b430 64 if ($tex) {
65 $tex = str_replace('&','&amp;',$tex);
66 $tex = str_replace('<','&lt;',$tex);
67 $tex = str_replace('>','&gt;',$tex);
68 $tex = str_replace('"','&quot;',$tex);
69 $tex = str_replace("\'",'&#39;',$tex);
f5ceb06a 70 // Note that we retain the title tag as TeX format rather than using
4317f92f
PS
71 // the alt text, even if supplied. The alt text is intended for blind
72 // users (to provide a text equivalent to the equation) while the title
73 // is there as a convenience for sighted users who want to see the TeX
74 // code.
9d49b430 75 $title = "title=\"$tex\"";
76 }
c1d9eeb9 77
78 // Build the output.
79 $output = "";
9d49b430 80 if ($imagefile) {
eaed2fd1
AD
81 $anchorcontents = "<img class=\"texrender\" $title alt=\"$alt\" src=\"";
82 if ($CFG->slasharguments) { // Use this method if possible for better caching
83 $anchorcontents .= "$CFG->wwwroot/filter/tex/pix.php/$imagefile";
d633b4eb 84 } else {
eaed2fd1 85 $anchorcontents .= "$CFG->wwwroot/filter/tex/pix.php?file=$imagefile";
894ff63f 86 }
eaed2fd1
AD
87 $anchorcontents .= "\" $style/>";
88
89 $link = $action = null;
90 if (!file_exists("$CFG->dataroot/filter/tex/$imagefile") && has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
91 $link = '/filter/tex/texdebug.php';
9d49b430 92 } else {
eaed2fd1
AD
93 $link = '/filter/tex/displaytex.php?'.urlencode($tex);
94 $action = new popup_action('click', $link, 'popup', array('height'=>300,'width'=>240));
9d49b430 95 }
e4cd4de2 96 $output .= $OUTPUT->action_link($link, $anchorcontents, $action, array('title'=>'TeX'));
c49dede8 97 } else {
9d49b430 98 $output .= "Error: must pass URL or course";
c49dede8 99 }
9d49b430 100 return $output;
c49dede8 101}
102
ccc161f8 103class tex_filter extends moodle_text_filter {
9e3f34d1 104 function filter ($text) {
105
106 global $CFG, $DB;
107
108 /// Do a quick check using stripos to avoid unnecessary work
109 if (!preg_match('/<tex/i',$text) and !strstr($text,'$$') and !strstr($text,'\\[') and !preg_match('/\[tex/i',$text)) { //added one more tag (dlnsk)
110 return $text;
111 }
c49dede8 112
9d49b430 113# //restrict filtering to forum 130 (Maths Tools on moodle.org)
114# $scriptname = $_SERVER['SCRIPT_NAME'];
115# if (!strstr($scriptname,'/forum/')) {
116# return $text;
117# }
118# if (strstr($scriptname,'post.php')) {
119# $parent = forum_get_post_full($_GET['reply']);
82a5323c 120# $discussion = $DB->get_record("forum_discussions", array("id"=>$parent->discussion));
9d49b430 121# } else if (strstr($scriptname,'discuss.php')) {
82a5323c 122# $discussion = $DB->get_record("forum_discussions", array("id"=>$_GET['d']));
9d49b430 123# } else {
124# return $text;
125# }
126# if ($discussion->forum != 130) {
127# return $text;
128# }
9e3f34d1 129 $text .= ' ';
130 preg_match_all('/\$(\$\$+?)([^\$])/s',$text,$matches);
131 for ($i=0;$i<count($matches[0]);$i++) {
132 $replacement = str_replace('$','&#x00024;',$matches[1][$i]).$matches[2][$i];
133 $text = str_replace($matches[0][$i],$replacement,$text);
f352814a 134 }
9e3f34d1 135
136 // <tex> TeX expression </tex>
137 // or <tex alt="My alternative text to be used instead of the TeX form"> TeX expression </tex>
138 // or $$ TeX expression $$
139 // or \[ TeX expression \] // original tag of MathType and TeXaide (dlnsk)
140 // or [tex] TeX expression [/tex] // somtime it's more comfortable than <tex> (dlnsk)
141 preg_match_all('/<tex(?:\s+alt=["\'](.*?)["\'])?>(.+?)<\/tex>|\$\$(.+?)\$\$|\\\\\[(.+?)\\\\\]|\\[tex\\](.+?)\\[\/tex\\]/is', $text, $matches);
142 for ($i=0; $i<count($matches[0]); $i++) {
143 $texexp = $matches[2][$i] . $matches[3][$i] . $matches[4][$i] . $matches[5][$i];
144 $alt = $matches[1][$i];
145 $texexp = str_replace('<nolink>','',$texexp);
146 $texexp = str_replace('</nolink>','',$texexp);
147 $texexp = str_replace('<span class="nolink">','',$texexp);
148 $texexp = str_replace('</span>','',$texexp);
c78a948e 149 $texexp = preg_replace("/<br[[:space:]]*\/?>/i", '', $texexp); //dlnsk
9e3f34d1 150 $align = "middle";
151 if (preg_match('/^align=bottom /',$texexp)) {
152 $align = "text-bottom";
153 $texexp = preg_replace('/^align=bottom /','',$texexp);
154 } else if (preg_match('/^align=top /',$texexp)) {
155 $align = "text-top";
156 $texexp = preg_replace('/^align=top /','',$texexp);
157 }
158 $md5 = md5($texexp);
159 if (! $texcache = $DB->get_record("cache_filters", array("filter"=>"tex", "md5key"=>$md5))) {
160 $texcache->filter = 'tex';
161 $texcache->version = 1;
162 $texcache->md5key = $md5;
163 $texcache->rawtext = $texexp;
164 $texcache->timemodified = time();
165 $DB->insert_record("cache_filters", $texcache, false);
166 }
cf4e7548 167 $filename = $md5 . ".{$CFG->filter_tex_convertformat}";
9e3f34d1 168 $text = str_replace( $matches[0][$i], string_file_picture_tex($filename, $texexp, '', '', $align, $alt), $text);
c49dede8 169 }
9e3f34d1 170 return $text;
c49dede8 171 }
9d49b430 172}
c49dede8 173
4317f92f 174