MDL-49807 mod_wiki: minimum heading level should always be <h3>
[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
3d89ccb3
MG
18 /** @var int Minimum level of the headers on the page (usually tinymce uses <h1> and atto <h3>) */
19 protected $minheaderlevel = null;
20
00710f4c
DC
21 public function __construct() {
22 parent::__construct();
c159e2aa
FM
23 // The order is important, headers should be parsed before links.
24 $this->tagrules = array(
25 // Headers are considered tags here.
26 'header' => array(
3d89ccb3 27 'expression' => "/<\s*h([1-6])\s*>(.+?)<\/h[1-6]>/is"
c159e2aa
FM
28 ),
29 'link' => $this->tagrules['link'],
30 'url' => $this->tagrules['url']
00710f4c
DC
31 );
32 }
33
3d89ccb3
MG
34 /**
35 * Find minimum header level used on the page (<h1>, <h3>, ...)
36 *
37 * @param string $text
38 * @return int
39 */
40 protected function find_min_header_level($text) {
41 preg_match_all($this->tagrules['header']['expression'], $text, $matches);
42 return !empty($matches[1]) ? min($matches[1]) : 1;
43 }
44
00710f4c
DC
45 protected function before_parsing() {
46 parent::before_parsing();
47
3d89ccb3 48 $this->minheaderlevel = $this->find_min_header_level($this->string);
00710f4c
DC
49 $this->rules($this->string);
50 }
51
52 /**
2264828f
CF
53 * Header tag rule
54 * @param array $match Header regex match
55 * @return string
00710f4c 56 */
2264828f 57 protected function header_tag_rule($match) {
3d89ccb3 58 return $this->generate_header($match[2], (int)$match[1] - $this->minheaderlevel + 1);
00710f4c
DC
59 }
60
61 /**
62 * Section editing: Special for HTML Parser (It parses <h1></h1>)
63 */
64
65 public function get_section($header, $text, $clean = false) {
66 if ($clean) {
67 $text = preg_replace('/\r\n/', "\n", $text);
68 $text = preg_replace('/\r/', "\n", $text);
69 $text .= "\n\n";
70 }
71
3d89ccb3
MG
72 $minheaderlevel = $this->find_min_header_level($text);
73
74 $h1 = array("<\s*h{$minheaderlevel}\s*>", "<\/h{$minheaderlevel}>");
00710f4c 75
c159e2aa
FM
76 $regex = "/(.*?)({$h1[0]}\s*".preg_quote($header, '/')."\s*{$h1[1]}.*?)((?:\n{$h1[0]}.*)|$)/is";
77 preg_match($regex, $text, $match);
00710f4c
DC
78
79 if (!empty($match)) {
80 return array($match[1], $match[2], $match[3]);
81 } else {
82 return false;
83 }
84 }
85
86 protected function get_repeated_sections(&$text, $repeated = array()) {
87 $this->repeated_sections = $repeated;
2264828f 88 return preg_replace_callback($this->tagrules['header'], array($this, 'get_repeated_sections_callback'), $text);
00710f4c
DC
89 }
90
91 protected function get_repeated_sections_callback($match) {
2264828f 92 $text = trim($match[2]);
00710f4c
DC
93
94 if (in_array($text, $this->repeated_sections)) {
95 $this->returnvalues['repeated_sections'][] = $text;
96 return parser_utils::h('p', $text);
97 } else {
98 $this->repeated_sections[] = $text;
99 }
100
101 return $match[0];
102 }
103}