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