weekly release 2.9dev
[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
593b8385 9 * @package mod_wiki
00710f4c
DC
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
2264828f 22 // Headers are considered tags here.
b4de35e6 23 $this->tagrules['header'] = array('expression' => "/<\s*h([1-$this->maxheaderdepth])\s*>(.+?)<\/h[1-$this->maxheaderdepth]>/is"
00710f4c
DC
24 );
25 }
26
27 protected function before_parsing() {
28 parent::before_parsing();
29
30 $this->rules($this->string);
31 }
32
33 /**
2264828f
CF
34 * Header tag rule
35 * @param array $match Header regex match
36 * @return string
00710f4c 37 */
2264828f
CF
38 protected function header_tag_rule($match) {
39 return $this->generate_header($match[2], $match[1]);
00710f4c
DC
40 }
41
42 /**
43 * Section editing: Special for HTML Parser (It parses <h1></h1>)
44 */
45
46 public function get_section($header, $text, $clean = false) {
47 if ($clean) {
48 $text = preg_replace('/\r\n/', "\n", $text);
49 $text = preg_replace('/\r/', "\n", $text);
50 $text .= "\n\n";
51 }
52
53 $h1 = array("<\s*h1\s*>", "<\/h1>");
54
55 preg_match("/(.*?)({$h1[0]}\s*\Q$header\E\s*{$h1[1]}.*?)((?:\n{$h1[0]}.*)|$)/is", $text, $match);
56
57 if (!empty($match)) {
58 return array($match[1], $match[2], $match[3]);
59 } else {
60 return false;
61 }
62 }
63
64 protected function get_repeated_sections(&$text, $repeated = array()) {
65 $this->repeated_sections = $repeated;
2264828f 66 return preg_replace_callback($this->tagrules['header'], array($this, 'get_repeated_sections_callback'), $text);
00710f4c
DC
67 }
68
69 protected function get_repeated_sections_callback($match) {
2264828f 70 $text = trim($match[2]);
00710f4c
DC
71
72 if (in_array($text, $this->repeated_sections)) {
73 $this->returnvalues['repeated_sections'][] = $text;
74 return parser_utils::h('p', $text);
75 } else {
76 $this->repeated_sections[] = $text;
77 }
78
79 return $match[0];
80 }
81}