PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/wiki/parser/parser.php

http://github.com/moodle/moodle
PHP | 303 lines | 188 code | 54 blank | 61 comment | 45 complexity | b21310e0b75aa89ebbfdbbf8a50818b9 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  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. * Library of functions and constants for module wiki
  18. *
  19. * It contains the great majority of functions defined by Moodle
  20. * that are mandatory to develop a module.
  21. *
  22. * @package mod_wiki
  23. * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
  24. * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
  25. *
  26. * @author Jordi Piguillem
  27. * @author Marc Alier
  28. * @author David Jimenez
  29. * @author Josep Arus
  30. * @author Kenneth Riba
  31. *
  32. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33. */
  34. defined('MOODLE_INTERNAL') || die();
  35. /**
  36. * Generic parser implementation
  37. *
  38. * @author Josep ArĂºs
  39. *
  40. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  41. * @package mod_wiki
  42. */
  43. class wiki_parser_proxy {
  44. private static $parsers = array();
  45. private static $basepath = "";
  46. public static function parse(&$string, $type, $options = array()) {
  47. if (empty(self::$basepath)) {
  48. global $CFG;
  49. self::$basepath = $CFG->dirroot . '/mod/wiki/parser/';
  50. }
  51. $type = strtolower($type);
  52. self::$parsers[$type] = null; // Reset the current parser because it may have other options.
  53. if (self::create_parser_instance($type)) {
  54. return self::$parsers[$type]->parse($string, $options);
  55. } else {
  56. return false;
  57. }
  58. }
  59. public static function get_token($name, $type) {
  60. if (self::create_parser_instance($type)) {
  61. return self::$parsers[$type]->get_token($name);
  62. } else {
  63. return false;
  64. }
  65. }
  66. public static function get_section(&$string, $type, $section, $allcontent = false) {
  67. if (self::create_parser_instance($type)) {
  68. $content = self::$parsers[$type]->get_section($section, $string, true);
  69. if ($allcontent) {
  70. return $content;
  71. } else {
  72. return is_array($content) ? $content[1] : null;
  73. }
  74. } else {
  75. return false;
  76. }
  77. }
  78. private static function create_parser_instance($type) {
  79. if (empty(self::$parsers[$type])) {
  80. include_once(self::$basepath . "markups/$type.php");
  81. $class = strtolower($type) . "_parser";
  82. if (class_exists($class)) {
  83. self::$parsers[$type] = new $class;
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. } else {
  89. return true;
  90. }
  91. }
  92. }
  93. require_once('utils.php');
  94. abstract class generic_parser {
  95. protected $string;
  96. protected $blockrules = array();
  97. protected $tagrules = array();
  98. private $rulestack = array();
  99. protected $parserstatus = 'Before';
  100. /**
  101. * Dynamic return values
  102. */
  103. protected $returnvalues = array();
  104. private $nowikiindex = array();
  105. protected $nowikitoken = "%!";
  106. public function __construct() {
  107. }
  108. /**
  109. * Parse function
  110. */
  111. public function parse(&$string, $options = array()) {
  112. if (!is_string($string)) {
  113. return false;
  114. }
  115. $this->string =& $string;
  116. $this->set_options(is_array($options) ? $options : array());
  117. $this->initialize_nowiki_index();
  118. if (method_exists($this, 'before_parsing')) {
  119. $this->before_parsing();
  120. }
  121. $this->parserstatus = 'Parsing';
  122. foreach ($this->blockrules as $name => $block) {
  123. $this->process_block_rule($name, $block);
  124. }
  125. $this->commit_nowiki_index();
  126. $this->parserstatus = 'After';
  127. if (method_exists($this, 'after_parsing')) {
  128. $this->after_parsing();
  129. }
  130. return array('parsed_text' => $this->string) + $this->returnvalues;
  131. }
  132. /**
  133. * Initialize options
  134. */
  135. protected function set_options($options) {
  136. }
  137. /**
  138. * Block processing function & callbacks
  139. */
  140. protected function process_block_rule($name, $block) {
  141. $this->rulestack[] = array('callback' => method_exists($this, $name . "_block_rule") ? $name . "_block_rule" : null,
  142. 'rule' => $block);
  143. $this->string = preg_replace_callback($block['expression'], array($this, 'block_callback'), $this->string);
  144. array_pop($this->rulestack);
  145. }
  146. private function block_callback($match) {
  147. $rule = end($this->rulestack);
  148. if (!empty($rule['callback'])) {
  149. $stuff = $this->{$rule['callback']}($match);
  150. } else {
  151. $stuff = $match[1];
  152. }
  153. if (is_array($stuff) && $rule['rule']['tag']) {
  154. $this->rules($stuff[0], $rule['rule']['tags']);
  155. $stuff = "\n" . parser_utils::h($rule['rule']['tag'], $stuff[0], $stuff[1]) . "\n";
  156. } else {
  157. if (!isset($rule['rule']['tags'])) {
  158. $rule['rule']['tags'] = null;
  159. }
  160. $this->rules($stuff, $rule['rule']['tags']);
  161. if (isset($rule['rule']['tag']) && is_string($rule['rule']['tag'])) {
  162. $stuff = "\n" . parser_utils::h($rule['rule']['tag'], $stuff) . "\n";
  163. }
  164. }
  165. return $stuff;
  166. }
  167. /**
  168. * Rules processing function & callback
  169. */
  170. protected final function rules(&$text, $rules = null) {
  171. if ($rules === null) {
  172. $rules = array('except' => array());
  173. } else if (is_array($rules) && count($rules) > 1) {
  174. $rules = array('only' => $rules);
  175. }
  176. if (isset($rules['only']) && is_array($rules['only'])) {
  177. $rules = $rules['only'];
  178. foreach ($rules as $r) {
  179. if (!empty($this->tagrules[$r])) {
  180. $this->process_tag_rule($r, $this->tagrules[$r], $text);
  181. }
  182. }
  183. } else if (isset($rules['except']) && is_array($rules['except'])) {
  184. $rules = $rules['except'];
  185. foreach ($this->tagrules as $r => $tr) {
  186. if (!in_array($r, $rules)) {
  187. $this->process_tag_rule($r, $tr, $text);
  188. }
  189. }
  190. }
  191. }
  192. private function process_tag_rule($name, $rule, &$text) {
  193. if (method_exists($this, $name . "_tag_rule")) {
  194. $this->rulestack[] = array('callback' => $name . "_tag_rule", 'rule' => $rule);
  195. $text = preg_replace_callback($rule['expression'], array($this, 'tag_callback'), $text);
  196. array_pop($this->rulestack);
  197. } else {
  198. if (isset($rule['simple'])) {
  199. $replace = "<{$rule['tag']} />";
  200. } else {
  201. $replace = parser_utils::h($rule['tag'], "$1");
  202. }
  203. $text = preg_replace($rule['expression'], $replace, $text);
  204. }
  205. }
  206. private function tag_callback($match) {
  207. $rule = end($this->rulestack);
  208. $stuff = $this->{$rule['callback']}($match);
  209. if (is_array($stuff)) {
  210. return parser_utils::h($rule['rule']['tag'], $stuff[0], $stuff[1]);
  211. } else {
  212. return $stuff;
  213. }
  214. }
  215. /**
  216. * Special nowiki parser index
  217. */
  218. private function initialize_nowiki_index() {
  219. $token = "\Q" . $this->nowikitoken . "\E";
  220. $this->string = preg_replace_callback("/" . $token . "\d+" . $token . "/",
  221. array($this, "initialize_nowiki_index_callback"), $this->string);
  222. }
  223. private function initialize_nowiki_index_callback($match) {
  224. return $this->protect($match[0]);
  225. }
  226. protected function protect($text) {
  227. $this->nowikiindex[] = $text;
  228. return $this->nowikitoken . (count($this->nowikiindex) - 1) . $this->nowikitoken;
  229. }
  230. private function commit_nowiki_index() {
  231. $token = "\Q" . $this->nowikitoken . "\E";
  232. $this->string = preg_replace_callback("/" . $token . "(\d+)" . $token . "/",
  233. array($this, "commit_nowiki_index_callback"), $this->string);
  234. }
  235. private function commit_nowiki_index_callback($match) {
  236. return $this->nowikiindex[intval($match[1])];
  237. }
  238. /**
  239. * Get token of the parsable element $name.
  240. */
  241. public function get_token($name) {
  242. foreach (array_merge($this->blockrules, $this->tagrules) as $n => $v) {
  243. if ($name == $n && isset($v['token'])) {
  244. return $v['token'] ? $v['token'] : false;
  245. }
  246. }
  247. return false;
  248. }
  249. }