4 * HTML parser implementation. It only implements links.
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
11 include_once("nwiki.php");
13 class html_parser extends nwiki_parser {
14 protected $blockrules = array();
16 protected $section_editing = true;
18 /** @var int Minimum level of the headers on the page (usually tinymce uses <h1> and atto <h3>) */
19 protected $minheaderlevel = null;
21 public function __construct() {
22 parent::__construct();
23 // The order is important, headers should be parsed before links.
24 $this->tagrules = array(
25 // Headers are considered tags here.
27 'expression' => "/<\s*h([1-6])\s*>(.+?)<\/h[1-6]>/is"
29 'link' => $this->tagrules['link'],
30 'url' => $this->tagrules['url']
35 * Find minimum header level used on the page (<h1>, <h3>, ...)
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;
45 protected function before_parsing() {
46 parent::before_parsing();
48 $this->minheaderlevel = $this->find_min_header_level($this->string);
49 $this->rules($this->string);
54 * @param array $match Header regex match
57 protected function header_tag_rule($match) {
58 return $this->generate_header($match[2], (int)$match[1] - $this->minheaderlevel + 1);
62 * Section editing: Special for HTML Parser (It parses <h1></h1>)
65 public function get_section($header, $text, $clean = false) {
67 $text = preg_replace('/\r\n/', "\n", $text);
68 $text = preg_replace('/\r/', "\n", $text);
72 $minheaderlevel = $this->find_min_header_level($text);
74 $h1 = array("<\s*h{$minheaderlevel}\s*>", "<\/h{$minheaderlevel}>");
76 $regex = "/(.*?)({$h1[0]}\s*".preg_quote($header, '/')."\s*{$h1[1]}.*?)((?:{$h1[0]}.*)|$)/is";
77 preg_match($regex, $text, $match);
80 return array($match[1], $match[2], $match[3]);
86 protected function get_repeated_sections(&$text, $repeated = array()) {
87 $this->repeated_sections = $repeated;
88 return preg_replace_callback($this->tagrules['header'], array($this, 'get_repeated_sections_callback'), $text);
91 protected function get_repeated_sections_callback($match) {
92 $text = trim($match[2]);
94 if (in_array($text, $this->repeated_sections)) {
95 $this->returnvalues['repeated_sections'][] = $text;
96 return parser_utils::h('p', $text);
98 $this->repeated_sections[] = $text;