|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00035 defined('MOODLE_INTERNAL') || die; 00036 00048 function filter_text_image($imagefile, $tex, $height, $width, $align, $alt) { 00049 global $CFG, $OUTPUT; 00050 00051 if (!$imagefile) { 00052 throw new coding_exception('image file argument empty in filter_text_image()'); 00053 } 00054 00055 // Work out any necessary inline style. 00056 $rules = array(); 00057 if ($align !== 'middle') { 00058 $rules[] = 'vertical-align:' . $align . ';'; 00059 } 00060 if ($height) { 00061 $rules[] = 'height:' . $height . 'px;'; 00062 } 00063 if ($width) { 00064 $rules[] = 'width:' . $width . 'px;'; 00065 } 00066 if (!empty($rules)) { 00067 $style = ' style="' . implode('', $rules) . '" '; 00068 } else { 00069 $style = ''; 00070 } 00071 00072 // Prepare the title attribute. 00073 // Note that we retain the title tag as TeX format rather than using 00074 // the alt text, even if supplied. The alt text is intended for blind 00075 // users (to provide a text equivalent to the equation) while the title 00076 // is there as a convenience for sighted users who want to see the TeX 00077 // code. 00078 $title = 'title="'.s($tex).'"'; 00079 00080 if ($alt === '') { 00081 $alt = s($tex); 00082 } else { 00083 $alt = s(html_entity_decode($tex, ENT_QUOTES, 'UTF-8')); 00084 } 00085 00086 // Build the output. 00087 $anchorcontents = "<img class=\"texrender\" $title alt=\"$alt\" src=\""; 00088 if ($CFG->slasharguments) { // Use this method if possible for better caching 00089 $anchorcontents .= "$CFG->wwwroot/filter/tex/pix.php/$imagefile"; 00090 } else { 00091 $anchorcontents .= "$CFG->wwwroot/filter/tex/pix.php?file=$imagefile"; 00092 } 00093 $anchorcontents .= "\" $style/>"; 00094 00095 if (!file_exists("$CFG->dataroot/filter/tex/$imagefile") && has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { 00096 $link = '/filter/tex/texdebug.php'; 00097 $action = null; 00098 } else { 00099 $link = new moodle_url('/filter/tex/displaytex.php', array('texexp'=>$tex)); 00100 $action = new popup_action('click', $link, 'popup', array('width'=>320,'height'=>240)); 00101 } 00102 $output = $OUTPUT->action_link($link, $anchorcontents, $action, array('title'=>'TeX')); //TODO: the popups do not work when text caching is enabled!! 00103 00104 return $output; 00105 } 00106 00107 00111 class filter_tex extends moodle_text_filter { 00112 function filter($text, array $options = array()) { 00113 00114 global $CFG, $DB; 00115 00117 if (!preg_match('/<tex/i',$text) and !strstr($text,'$$') and !strstr($text,'\\[') and !preg_match('/\[tex/i',$text)) { //added one more tag (dlnsk) 00118 return $text; 00119 } 00120 00121 # //restrict filtering to forum 130 (Maths Tools on moodle.org) 00122 # $scriptname = $_SERVER['SCRIPT_NAME']; 00123 # if (!strstr($scriptname,'/forum/')) { 00124 # return $text; 00125 # } 00126 # if (strstr($scriptname,'post.php')) { 00127 # $parent = forum_get_post_full($_GET['reply']); 00128 # $discussion = $DB->get_record("forum_discussions", array("id"=>$parent->discussion)); 00129 # } else if (strstr($scriptname,'discuss.php')) { 00130 # $discussion = $DB->get_record("forum_discussions", array("id"=>$_GET['d'])); 00131 # } else { 00132 # return $text; 00133 # } 00134 # if ($discussion->forum != 130) { 00135 # return $text; 00136 # } 00137 $text .= ' '; 00138 preg_match_all('/\$(\$\$+?)([^\$])/s',$text,$matches); 00139 for ($i=0; $i<count($matches[0]); $i++) { 00140 $replacement = str_replace('$','$', $matches[1][$i]).$matches[2][$i]; 00141 $text = str_replace($matches[0][$i], $replacement, $text); 00142 } 00143 00144 // <tex> TeX expression </tex> 00145 // or <tex alt="My alternative text to be used instead of the TeX form"> TeX expression </tex> 00146 // or $$ TeX expression $$ 00147 // or \[ TeX expression \] // original tag of MathType and TeXaide (dlnsk) 00148 // or [tex] TeX expression [/tex] // somtime it's more comfortable than <tex> (dlnsk) 00149 preg_match_all('/<tex(?:\s+alt=["\'](.*?)["\'])?>(.+?)<\/tex>|\$\$(.+?)\$\$|\\\\\[(.+?)\\\\\]|\\[tex\\](.+?)\\[\/tex\\]/is', $text, $matches); 00150 for ($i=0; $i<count($matches[0]); $i++) { 00151 $texexp = $matches[2][$i] . $matches[3][$i] . $matches[4][$i] . $matches[5][$i]; 00152 $alt = $matches[1][$i]; 00153 $texexp = str_replace('<nolink>','',$texexp); 00154 $texexp = str_replace('</nolink>','',$texexp); 00155 $texexp = str_replace('<span class="nolink">','',$texexp); 00156 $texexp = str_replace('</span>','',$texexp); 00157 $texexp = preg_replace("/<br[[:space:]]*\/?>/i", '', $texexp); //dlnsk 00158 $align = "middle"; 00159 if (preg_match('/^align=bottom /',$texexp)) { 00160 $align = "text-bottom"; 00161 $texexp = preg_replace('/^align=bottom /','',$texexp); 00162 } else if (preg_match('/^align=top /',$texexp)) { 00163 $align = "text-top"; 00164 $texexp = preg_replace('/^align=top /','',$texexp); 00165 } 00166 00167 // decode entities encoded by editor, luckily there is very little chance of double decoding 00168 $texexp = html_entity_decode($texexp, ENT_QUOTES, 'UTF-8'); 00169 00170 if ($texexp === '') { 00171 continue; 00172 } 00173 00174 $md5 = md5($texexp); 00175 if (!$DB->record_exists("cache_filters", array("filter"=>"tex", "md5key"=>$md5))) { 00176 $texcache = new stdClass(); 00177 $texcache->filter = 'tex'; 00178 $texcache->version = 1; 00179 $texcache->md5key = $md5; 00180 $texcache->rawtext = $texexp; 00181 $texcache->timemodified = time(); 00182 $DB->insert_record("cache_filters", $texcache, false); 00183 } 00184 $filename = $md5 . ".{$CFG->filter_tex_convertformat}"; 00185 $text = str_replace( $matches[0][$i], filter_text_image($filename, $texexp, 0, 0, $align, $alt), $text); 00186 } 00187 return $text; 00188 } 00189 } 00190 00191