PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/filter/tex/filter.php

https://bitbucket.org/ceu/moodle_demo
PHP | 173 lines | 96 code | 11 blank | 66 comment | 19 complexity | 141ebc2a3a45a41caed62b9b515fc628 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1
  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
  27. // John Forkosh john@forkosh.com. Several binaries of this areincluded with
  28. // this distribution.
  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. //-------------------------------------------------------------------------
  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. /////////////////////////////////////////////////////////////////////////////
  38. function string_file_picture_tex($imagefile, $tex= "", $height="", $width="", $align="middle", $alt='') {
  39. global $CFG;
  40. if ($alt==='') {
  41. $alt = s($tex);
  42. }
  43. // Work out any necessary inline style.
  44. $rules = array();
  45. if ($align !== 'middle') {
  46. $rules[] = 'vertical-align:' . $align . ';';
  47. }
  48. if ($height) {
  49. $rules[] = 'height:' . $height . 'px;';
  50. }
  51. if ($width) {
  52. $rules[] = 'width:' . $width . 'px;';
  53. }
  54. if (!empty($rules)) {
  55. $style = ' style="' . implode('', $rules) . '" ';
  56. } else {
  57. $style = '';
  58. }
  59. // Prepare the title attribute.
  60. if ($tex) {
  61. $tex = str_replace('&','&amp;',$tex);
  62. $tex = str_replace('<','&lt;',$tex);
  63. $tex = str_replace('>','&gt;',$tex);
  64. $tex = str_replace('"','&quot;',$tex);
  65. $tex = str_replace("\'",'&#39;',$tex);
  66. // Note that we retain the title tag as TeX format rather than using
  67. // the alt text, even if supplied. The alt text is intended for blind
  68. // users (to provide a text equivalent to the equation) while the title
  69. // is there as a convenience for sighted users who want to see the TeX
  70. // code.
  71. $title = "title=\"$tex\"";
  72. }
  73. // Build the output.
  74. $output = "";
  75. if ($imagefile) {
  76. if (!file_exists("$CFG->dataroot/filter/tex/$imagefile") && has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
  77. $output .= "<a href=\"$CFG->wwwroot/filter/tex/texdebug.php\">";
  78. } else {
  79. $output .= "<a target=\"popup\" title=\"TeX\" href=";
  80. $output .= "\"$CFG->wwwroot/filter/tex/displaytex.php?";
  81. $output .= urlencode($tex) . "\" onclick=\"return openpopup('/filter/tex/displaytex.php?";
  82. $output .= urlencode($tex) . "', 'popup', 'menubar=0,location=0,scrollbars,";
  83. $output .= "resizable,width=300,height=240', 0);\">";
  84. }
  85. $output .= "<img class=\"texrender\" $title alt=\"$alt\" src=\"";
  86. if ($CFG->slasharguments) { // Use this method if possible for better caching
  87. $output .= "$CFG->wwwroot/filter/tex/pix.php/$imagefile";
  88. } else {
  89. $output .= "$CFG->wwwroot/filter/tex/pix.php?file=$imagefile";
  90. }
  91. $output .= "\" $style/>";
  92. $output .= "</a>";
  93. } else {
  94. $output .= "Error: must pass URL or course";
  95. }
  96. return $output;
  97. }
  98. function tex_filter ($courseid, $text) {
  99. global $CFG;
  100. /// Do a quick check using stripos to avoid unnecessary work
  101. if (!preg_match('/<tex/i',$text) and !strstr($text,'$$') and !strstr($text,'\\[') and !preg_match('/\[tex/i',$text)) { //added one more tag (dlnsk)
  102. return $text;
  103. }
  104. # //restrict filtering to forum 130 (Maths Tools on moodle.org)
  105. # $scriptname = $_SERVER['SCRIPT_NAME'];
  106. # if (!strstr($scriptname,'/forum/')) {
  107. # return $text;
  108. # }
  109. # if (strstr($scriptname,'post.php')) {
  110. # $parent = forum_get_post_full($_GET['reply']);
  111. # $discussion = get_record("forum_discussions","id",$parent->discussion);
  112. # } else if (strstr($scriptname,'discuss.php')) {
  113. # $discussion = get_record("forum_discussions","id",$_GET['d'] );
  114. # } else {
  115. # return $text;
  116. # }
  117. # if ($discussion->forum != 130) {
  118. # return $text;
  119. # }
  120. $text .= ' ';
  121. preg_match_all('/\$(\$\$+?)([^\$])/s',$text,$matches);
  122. for ($i=0;$i<count($matches[0]);$i++) {
  123. $replacement = str_replace('$','&#x00024;',$matches[1][$i]).$matches[2][$i];
  124. $text = str_replace($matches[0][$i],$replacement,$text);
  125. }
  126. // <tex> TeX expression </tex>
  127. // or <tex alt="My alternative text to be used instead of the TeX form"> TeX expression </tex>
  128. // or $$ TeX expression $$
  129. // or \[ TeX expression \] // original tag of MathType and TeXaide (dlnsk)
  130. // or [tex] TeX expression [/tex] // somtime it's more comfortable than <tex> (dlnsk)
  131. preg_match_all('/<tex(?:\s+alt=["\'](.*?)["\'])?>(.+?)<\/tex>|\$\$(.+?)\$\$|\\\\\[(.+?)\\\\\]|\\[tex\\](.+?)\\[\/tex\\]/is', $text, $matches);
  132. for ($i=0; $i<count($matches[0]); $i++) {
  133. $texexp = $matches[2][$i] . $matches[3][$i] . $matches[4][$i] . $matches[5][$i];
  134. $alt = $matches[1][$i];
  135. $texexp = str_replace('<nolink>','',$texexp);
  136. $texexp = str_replace('</nolink>','',$texexp);
  137. $texexp = str_replace('<span class="nolink">','',$texexp);
  138. $texexp = str_replace('</span>','',$texexp);
  139. $texexp = eregi_replace("<br[[:space:]]*\/?>", '', $texexp); //dlnsk
  140. $align = "middle";
  141. if (preg_match('/^align=bottom /',$texexp)) {
  142. $align = "text-bottom";
  143. $texexp = preg_replace('/^align=bottom /','',$texexp);
  144. } else if (preg_match('/^align=top /',$texexp)) {
  145. $align = "text-top";
  146. $texexp = preg_replace('/^align=top /','',$texexp);
  147. }
  148. $md5 = md5($texexp);
  149. if (! $texcache = get_record("cache_filters","filter","tex", "md5key", $md5)) {
  150. $texcache->filter = 'tex';
  151. $texcache->version = 1;
  152. $texcache->md5key = $md5;
  153. $texcache->rawtext = addslashes($texexp);
  154. $texcache->timemodified = time();
  155. insert_record("cache_filters",$texcache, false);
  156. }
  157. $filename = $md5 . ".{$CFG->filter_tex_convertformat}";
  158. $text = str_replace( $matches[0][$i], string_file_picture_tex($filename, $texexp, '', '', $align, $alt), $text);
  159. }
  160. return $text;
  161. }
  162. ?>