Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This filter provides automatic support for MathJax
  19   *
  20   * @package    filter_mathjaxloader
  21   * @copyright  2013 Damyon Wiese (damyon@moodle.com)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Mathjax filtering
  29   */
  30  class filter_mathjaxloader extends moodle_text_filter {
  31  
  32      /*
  33       * Perform a mapping of the moodle language code to the equivalent for MathJax.
  34       *
  35       * @param string $moodlelangcode - The moodle language code - e.g. en_pirate
  36       * @return string The MathJax language code.
  37       */
  38      public function map_language_code($moodlelangcode) {
  39  
  40          // List of language codes found in the MathJax/localization/ directory.
  41          $mathjaxlangcodes = [
  42              'ar', 'ast', 'bcc', 'bg', 'br', 'ca', 'cdo', 'ce', 'cs', 'cy', 'da', 'de', 'diq', 'en', 'eo', 'es', 'fa',
  43              'fi', 'fr', 'gl', 'he', 'ia', 'it', 'ja', 'kn', 'ko', 'lb', 'lki', 'lt', 'mk', 'nl', 'oc', 'pl', 'pt',
  44              'pt-br', 'qqq', 'ru', 'scn', 'sco', 'sk', 'sl', 'sv', 'th', 'tr', 'uk', 'vi', 'zh-hans', 'zh-hant'
  45          ];
  46  
  47          // List of explicit mappings and known exceptions (moodle => mathjax).
  48          $explicit = [
  49              'cz' => 'cs',
  50              'pt_br' => 'pt-br',
  51              'zh_tw' => 'zh-hant',
  52              'zh_cn' => 'zh-hans',
  53          ];
  54  
  55          // If defined, explicit mapping takes the highest precedence.
  56          if (isset($explicit[$moodlelangcode])) {
  57              return $explicit[$moodlelangcode];
  58          }
  59  
  60          // If there is exact match, it will be probably right.
  61          if (in_array($moodlelangcode, $mathjaxlangcodes)) {
  62              return $moodlelangcode;
  63          }
  64  
  65          // Finally try to find the best matching mathjax pack.
  66          $parts = explode('_', $moodlelangcode, 2);
  67          if (in_array($parts[0], $mathjaxlangcodes)) {
  68              return $parts[0];
  69          }
  70  
  71          // No more guessing, use English.
  72          return 'en';
  73      }
  74  
  75      /*
  76       * Add the javascript to enable mathjax processing on this page.
  77       *
  78       * @param moodle_page $page The current page.
  79       * @param context $context The current context.
  80       */
  81      public function setup($page, $context) {
  82  
  83          if ($page->requires->should_create_one_time_item_now('filter_mathjaxloader-scripts')) {
  84              $url = get_config('filter_mathjaxloader', 'httpsurl');
  85              $lang = $this->map_language_code(current_language());
  86              $url = new moodle_url($url, array('delayStartupUntil' => 'configured'));
  87  
  88              $page->requires->js($url);
  89  
  90              $config = get_config('filter_mathjaxloader', 'mathjaxconfig');
  91              $wwwroot = new moodle_url('/');
  92  
  93              $config = str_replace('{wwwroot}', $wwwroot->out(true), $config);
  94  
  95              $params = array('mathjaxconfig' => $config, 'lang' => $lang);
  96  
  97              $page->requires->js_call_amd('filter_mathjaxloader/loader', 'configure', [$params]);
  98          }
  99      }
 100  
 101      /*
 102       * This function wraps the filtered text in a span, that mathjaxloader is configured to process.
 103       *
 104       * @param string $text The text to filter.
 105       * @param array $options The filter options.
 106       */
 107      public function filter($text, array $options = array()) {
 108          global $PAGE;
 109  
 110          $legacy = get_config('filter_mathjaxloader', 'texfiltercompatibility');
 111          $extradelimiters = explode(',', get_config('filter_mathjaxloader', 'additionaldelimiters'));
 112          if ($legacy) {
 113              // This replaces any of the tex filter maths delimiters with the default for inline maths in MathJAX "\( blah \)".
 114              // E.g. "<tex.*> blah </tex>".
 115              $text = preg_replace('|<(/?) *tex( [^>]*)?>|u', '[\1tex]', $text);
 116              // E.g. "[tex.*] blah [/tex]".
 117              $text = str_replace('[tex]', '\\(', $text);
 118              $text = str_replace('[/tex]', '\\)', $text);
 119              // E.g. "$$ blah $$".
 120              $text = preg_replace('|\$\$([\S\s]*?)\$\$|u', '\\(\1\\)', $text);
 121              // E.g. "\[ blah \]".
 122              $text = str_replace('\\[', '\\(', $text);
 123              $text = str_replace('\\]', '\\)', $text);
 124          }
 125  
 126          $hasextra = false;
 127          foreach ($extradelimiters as $extra) {
 128              if ($extra && strpos($text, $extra) !== false) {
 129                  $hasextra = true;
 130                  break;
 131              }
 132          }
 133  
 134          $hasdisplayorinline = false;
 135          if ($hasextra) {
 136              // If custom dilimeters are used, wrap whole text to prevent autolinking.
 137              $text = '<span class="nolink">' . $text . '</span>';
 138          } else if (preg_match('/\\\\[[(]/', $text) || preg_match('/\$\$/', $text)) {
 139              // Only parse the text if there are mathjax symbols in it. The recognized
 140              // math environments are \[ \] and $$ $$ for display mathematics and \( \)
 141              // for inline mathematics.
 142              // Note: 2 separate regexes seems to perform better here than using a single
 143              // regex with groupings.
 144  
 145              // Wrap display and inline math environments in nolink spans.
 146              // Do not wrap nested environments, i.e., if inline math is nested
 147              // inside display math, only the outer display math is wrapped in
 148              // a span. The span HTML inside a LaTex math environment would break
 149              // MathJax. See MDL-61981.
 150              list($text, $hasdisplayorinline) = $this->wrap_math_in_nolink($text);
 151          }
 152  
 153          if ($hasdisplayorinline || $hasextra) {
 154              $PAGE->requires->js_call_amd('filter_mathjaxloader/loader', 'typeset');
 155              return '<span class="filter_mathjaxloader_equation">' . $text . '</span>';
 156          }
 157          return $text;
 158      }
 159  
 160      /**
 161       * Find math environments in the $text and wrap them in no link spans
 162       * (<span class="nolink"></span>). If math environments are nested, only
 163       * the outer environment is wrapped in the span.
 164       *
 165       * The recognized math environments are \[ \] and $$ $$ for display
 166       * mathematics and \( \) for inline mathematics.
 167       *
 168       * @param string $text The text to filter.
 169       * @return array An array containing the potentially modified text and
 170       * a boolean that is true if any changes were made to the text.
 171       */
 172      protected function wrap_math_in_nolink($text) {
 173          $i = 1;
 174          $len = strlen($text);
 175          $displaystart = -1;
 176          $displaybracket = false;
 177          $displaydollar = false;
 178          $inlinestart = -1;
 179          $changesdone = false;
 180          // Loop over the $text once.
 181          while ($i < $len) {
 182              if ($displaystart === -1) {
 183                  // No display math has started yet.
 184                  if ($text[$i - 1] === '\\' && $text[$i] === '[') {
 185                      // Display mode \[ begins.
 186                      $displaystart = $i - 1;
 187                      $displaybracket = true;
 188                  } else if ($text[$i - 1] === '$' && $text[$i] === '$') {
 189                      // Display mode $$ begins.
 190                      $displaystart = $i - 1;
 191                      $displaydollar = true;
 192                  } else if ($text[$i - 1] === '\\' && $text[$i] === '(') {
 193                      // Inline math \( begins, not nested inside display math.
 194                      $inlinestart = $i - 1;
 195                  } else if ($text[$i - 1] === '\\' && $text[$i] === ')' && $inlinestart > -1) {
 196                      // Inline math ends, not nested inside display math.
 197                      // Wrap the span around it.
 198                      $text = $this->insert_span($text, $inlinestart, $i);
 199  
 200                      $inlinestart = -1; // Reset.
 201                      $i += 28; // The $text length changed due to the <span>.
 202                      $len += 28;
 203                      $changesdone = true;
 204                  }
 205              } else {
 206                  // Display math open.
 207                  if (($text[$i - 1] === '\\' && $text[$i] === ']' && $displaybracket) ||
 208                          ($text[$i - 1] === '$' && $text[$i] === '$' && $displaydollar)) {
 209                      // Display math ends, wrap the span around it.
 210                      $text = $this->insert_span($text, $displaystart, $i);
 211  
 212                      $displaystart = -1; // Reset.
 213                      $displaybracket = false;
 214                      $displaydollar = false;
 215                      $i += 28; // The $text length changed due to the <span>.
 216                      $len += 28;
 217                      $changesdone = true;
 218                  }
 219              }
 220  
 221              ++$i;
 222          }
 223          return array($text, $changesdone);
 224      }
 225  
 226      /**
 227       * Wrap a portion of the $text inside a no link span
 228       * (<span class="nolink"></span>). The whole text is then returned.
 229       *
 230       * @param string $text The text to modify.
 231       * @param int $start The start index of the substring in $text that should
 232       * be wrapped in the span.
 233       * @param int $end The end index of the substring in $text that should be
 234       * wrapped in the span.
 235       * @return string The whole $text with the span inserted around
 236       * the defined substring.
 237       */
 238      protected function insert_span($text, $start, $end) {
 239          return substr_replace($text,
 240                  '<span class="nolink">'. substr($text, $start, $end - $start + 1) .'</span>',
 241                  $start,
 242                  $end - $start + 1);
 243      }
 244  }