"FILEPICKER/MDL-13766, fix type"
[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='') {
40 if($alt==='') {
41 $alt=s($tex);
42 }
e47d5ade 43 // Given the path to a picture file in a course, or a URL,
9d49b430 44 // this function includes the picture in the page.
45 global $CFG;
c49dede8 46
9d49b430 47 $output = "";
2bbaf749 48 $origtex = $tex;
e47d5ade 49 if ($align !== 'middle') {
50 $style = ' style="vertical-align:'.$align.';';
51 } else {
52 $style = '';
53 }
54 if ($height) {
55 $style .= " height:{$height}px;";
56 }
57 if ($width) {
58 $style .= " width:{$width}px;";
59 }
60 $style .= '"';
9d49b430 61 if ($tex) {
62 $tex = str_replace('&','&amp;',$tex);
63 $tex = str_replace('<','&lt;',$tex);
64 $tex = str_replace('>','&gt;',$tex);
65 $tex = str_replace('"','&quot;',$tex);
66 $tex = str_replace("\'",'&#39;',$tex);
f5ceb06a 67 // Note that we retain the title tag as TeX format rather than using
68 // the alt text, even if supplied. The alt text is intended for blind
69 // users (to provide a text equivalent to the equation) while the title
70 // is there as a convenience for sighted users who want to see the TeX
71 // code.
9d49b430 72 $title = "title=\"$tex\"";
73 }
9d49b430 74 if ($imagefile) {
09ee7b0d 75 if (!file_exists("$CFG->dataroot/filter/tex/$imagefile") && has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
220a90c5 76 $output .= "<a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">";
d633b4eb 77 } else {
30fc04e4 78 $output .= "<a target=\"popup\" title=\"TeX\" href=";
220a90c5 79 $output .= "\"$CFG->wwwroot/filter/tex/displaytex.php?";
80 $output .= urlencode($tex) . "\" onclick=\"return openpopup('/filter/tex/displaytex.php?";
30fc04e4 81 $output .= urlencode($tex) . "', 'popup', 'menubar=0,location=0,scrollbars,";
82 $output .= "resizable,width=300,height=240', 0);\">";
894ff63f 83 }
f5ceb06a 84 $output .= "<img class=\"texrender\" $title alt=\"$alt\" src=\"";
9d49b430 85 if ($CFG->slasharguments) { // Use this method if possible for better caching
220a90c5 86 $output .= "$CFG->wwwroot/filter/tex/pix.php/$imagefile";
9d49b430 87 } else {
220a90c5 88 $output .= "$CFG->wwwroot/filter/tex/pix.php?file=$imagefile";
9d49b430 89 }
0f13ec8f 90 $output .= "\" $style />";
d633b4eb 91 $output .= "</a>";
c49dede8 92 } else {
9d49b430 93 $output .= "Error: must pass URL or course";
c49dede8 94 }
9d49b430 95 return $output;
c49dede8 96}
97
9e3f34d1 98class tex_filter extends filter_base {
99 function __construct($courseid, $format, $options) {
100 parent::__construct($courseid, $format, $options);
c49dede8 101 }
9e3f34d1 102 function filter ($text) {
103
104 global $CFG, $DB;
105
106 /// Do a quick check using stripos to avoid unnecessary work
107 if (!preg_match('/<tex/i',$text) and !strstr($text,'$$') and !strstr($text,'\\[') and !preg_match('/\[tex/i',$text)) { //added one more tag (dlnsk)
108 return $text;
109 }
c49dede8 110
9d49b430 111# //restrict filtering to forum 130 (Maths Tools on moodle.org)
112# $scriptname = $_SERVER['SCRIPT_NAME'];
113# if (!strstr($scriptname,'/forum/')) {
114# return $text;
115# }
116# if (strstr($scriptname,'post.php')) {
117# $parent = forum_get_post_full($_GET['reply']);
82a5323c 118# $discussion = $DB->get_record("forum_discussions", array("id"=>$parent->discussion));
9d49b430 119# } else if (strstr($scriptname,'discuss.php')) {
82a5323c 120# $discussion = $DB->get_record("forum_discussions", array("id"=>$_GET['d']));
9d49b430 121# } else {
122# return $text;
123# }
124# if ($discussion->forum != 130) {
125# return $text;
126# }
9e3f34d1 127 $text .= ' ';
128 preg_match_all('/\$(\$\$+?)([^\$])/s',$text,$matches);
129 for ($i=0;$i<count($matches[0]);$i++) {
130 $replacement = str_replace('$','&#x00024;',$matches[1][$i]).$matches[2][$i];
131 $text = str_replace($matches[0][$i],$replacement,$text);
f352814a 132 }
9e3f34d1 133
134 // <tex> TeX expression </tex>
135 // or <tex alt="My alternative text to be used instead of the TeX form"> TeX expression </tex>
136 // or $$ TeX expression $$
137 // or \[ TeX expression \] // original tag of MathType and TeXaide (dlnsk)
138 // or [tex] TeX expression [/tex] // somtime it's more comfortable than <tex> (dlnsk)
139 preg_match_all('/<tex(?:\s+alt=["\'](.*?)["\'])?>(.+?)<\/tex>|\$\$(.+?)\$\$|\\\\\[(.+?)\\\\\]|\\[tex\\](.+?)\\[\/tex\\]/is', $text, $matches);
140 for ($i=0; $i<count($matches[0]); $i++) {
141 $texexp = $matches[2][$i] . $matches[3][$i] . $matches[4][$i] . $matches[5][$i];
142 $alt = $matches[1][$i];
143 $texexp = str_replace('<nolink>','',$texexp);
144 $texexp = str_replace('</nolink>','',$texexp);
145 $texexp = str_replace('<span class="nolink">','',$texexp);
146 $texexp = str_replace('</span>','',$texexp);
147 $texexp = eregi_replace("<br[[:space:]]*\/?>", '', $texexp); //dlnsk
148 $align = "middle";
149 if (preg_match('/^align=bottom /',$texexp)) {
150 $align = "text-bottom";
151 $texexp = preg_replace('/^align=bottom /','',$texexp);
152 } else if (preg_match('/^align=top /',$texexp)) {
153 $align = "text-top";
154 $texexp = preg_replace('/^align=top /','',$texexp);
155 }
156 $md5 = md5($texexp);
157 if (! $texcache = $DB->get_record("cache_filters", array("filter"=>"tex", "md5key"=>$md5))) {
158 $texcache->filter = 'tex';
159 $texcache->version = 1;
160 $texcache->md5key = $md5;
161 $texcache->rawtext = $texexp;
162 $texcache->timemodified = time();
163 $DB->insert_record("cache_filters", $texcache, false);
164 }
165 $filename = $md5 . ".gif";
166 $text = str_replace( $matches[0][$i], string_file_picture_tex($filename, $texexp, '', '', $align, $alt), $text);
c49dede8 167 }
9e3f34d1 168 return $text;
c49dede8 169 }
9d49b430 170}
c49dede8 171
1cdd857c 172?>