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.
   1  <?php
   2  
   3  /**
   4   * Parser utils and default callbacks.
   5   *
   6   * @author Josep ArĂºs
   7   *
   8   * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
   9   * @package mod_wiki
  10   */
  11  
  12  require_once($CFG->dirroot . "/lib/outputcomponents.php");
  13  
  14  class parser_utils {
  15  
  16      public static function h($tag, $text = null, $options = array(), $escape_text = false) {
  17          $tag = htmlentities($tag, ENT_COMPAT, 'UTF-8');
  18          if(!empty($text) && $escape_text) {
  19                  $text = htmlentities($text, ENT_COMPAT, 'UTF-8');
  20              }
  21          return html_writer::tag($tag, $text, $options);
  22      }
  23  
  24      /**
  25       * Default link generator
  26       */
  27  
  28      public static function wiki_parser_link_callback($link, $options) {
  29          $l = urlencode($link);
  30          if(!empty($options['anchor'])) {
  31              $l .= "#".urlencode($options['anchor']);
  32          }
  33          return array('content' => $link, 'url' => "http://".$l);
  34      }
  35  
  36  
  37      /**
  38       * Default table generator
  39       */
  40  
  41      public static function wiki_parser_table_callback($table) {
  42          $html = "";
  43          $headers = $table[0];
  44          $columncount = count($headers);
  45          $headerhtml = "";
  46          foreach($headers as $h) {
  47              $text = trim($h[1]);
  48              if($h[0] == 'header') {
  49                  $headerhtml .= "\n".parser_utils::h('th', $text)."\n";
  50                  $hasheaders = true;
  51              }
  52              else if($h[0] == 'normal'){
  53                  $headerhtml .= "\n".parser_utils::h("td", $text)."\n";
  54              }
  55          }
  56          $headerhtml = "\n".parser_utils::h('tr', $headerhtml)."\n";
  57          $bodyhtml = "";
  58          if(isset($hasheaders)) {
  59              $html = "\n".parser_utils::h('thead', $headerhtml)."\n";
  60          }
  61          else {
  62              $bodyhtml .= $headerhtml;
  63          }
  64  
  65          array_shift($table);
  66          foreach($table as $row) {
  67              $htmlrow = "";
  68              for($i = 0; $i < $columncount; $i++) {
  69                  $text = "";
  70                  if(!isset($row[$i])) {
  71                      $htmlrow .= "\n".parser_utils::h('td', $text)."\n";
  72                  }
  73                  else {
  74                      $text = trim($row[$i][1]);
  75                      if($row[$i][0] == 'header') {
  76                          $htmlrow .= "\n".parser_utils::h('th', $text)."\n";
  77                      }
  78                      else if($row[$i][0] == 'normal'){
  79                          $htmlrow .= "\n".parser_utils::h('td', $text)."\n";
  80                      }
  81                  }
  82              }
  83              $bodyhtml .= "\n".parser_utils::h('tr', $htmlrow)."\n";
  84          }
  85  
  86          $html .= "\n".parser_utils::h('tbody', $bodyhtml)."\n";
  87          return "\n".parser_utils::h('table', $html)."\n";
  88      }
  89  
  90      /**
  91       * Default path converter
  92       */
  93  
  94      public static function wiki_parser_real_path($url) {
  95          return $url;
  96      }
  97  }
  98