weekly release 2.4dev
[moodle.git] / mod / wiki / parser / markups / html.php
CommitLineData
00710f4c
DC
1<?php
2
3/**
4 * HTML parser implementation. It only implements links.
5 *
6 * @author Josep ArĂºs
7 *
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package wiki
10 */
11include_once("nwiki.php");
12
13class html_parser extends nwiki_parser {
14 protected $blockrules = array();
15
16 protected $section_editing = true;
17
18 public function __construct() {
19 parent::__construct();
20 $this->tagrules = array('link' => $this->tagrules['link'], 'url' => $this->tagrules['url']);
21
22 //headers are considered tags here...
23 $h1 = array("<\s*h1\s*>", "<\/h1>");
24 $this->tagrules['header1'] = array('expression' => "/{$h1[0]}(.+?){$h1[1]}/is"
25 );
26 }
27
28 protected function before_parsing() {
29 parent::before_parsing();
30
31 $this->rules($this->string);
32 }
33
34 /**
35 * Header 1 tag rule
36 */
37 protected function header1_tag_rule($match) {
38 return $this->generate_header($match[1], 1);
39 }
40
41 /**
42 * Section editing: Special for HTML Parser (It parses <h1></h1>)
43 */
44
45 public function get_section($header, $text, $clean = false) {
46 if ($clean) {
47 $text = preg_replace('/\r\n/', "\n", $text);
48 $text = preg_replace('/\r/', "\n", $text);
49 $text .= "\n\n";
50 }
51
52 $h1 = array("<\s*h1\s*>", "<\/h1>");
53
54 preg_match("/(.*?)({$h1[0]}\s*\Q$header\E\s*{$h1[1]}.*?)((?:\n{$h1[0]}.*)|$)/is", $text, $match);
55
56 if (!empty($match)) {
57 return array($match[1], $match[2], $match[3]);
58 } else {
59 return false;
60 }
61 }
62
63 protected function get_repeated_sections(&$text, $repeated = array()) {
64 $this->repeated_sections = $repeated;
65 return preg_replace_callback($this->tagrules['header1'], array($this, 'get_repeated_sections_callback'), $text);
66 }
67
68 protected function get_repeated_sections_callback($match) {
69 $text = trim($match[1]);
70
71 if (in_array($text, $this->repeated_sections)) {
72 $this->returnvalues['repeated_sections'][] = $text;
73 return parser_utils::h('p', $text);
74 } else {
75 $this->repeated_sections[] = $text;
76 }
77
78 return $match[0];
79 }
80}