[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 mod_wiki 10 */ 11 include_once ("nwiki.php"); 12 13 class html_parser extends nwiki_parser { 14 protected $blockrules = array(); 15 16 protected $section_editing = true; 17 18 /** @var int Minimum level of the headers on the page (usually tinymce uses <h1> and atto <h3>) */ 19 protected $minheaderlevel = null; 20 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. 26 'header' => array( 27 'expression' => "/<\s*h([1-6])\s*>(.+?)<\/h[1-6]>/is" 28 ), 29 'link' => $this->tagrules['link'], 30 'url' => $this->tagrules['url'] 31 ); 32 } 33 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 45 protected function before_parsing() { 46 parent::before_parsing(); 47 48 $this->minheaderlevel = $this->find_min_header_level($this->string); 49 $this->rules($this->string); 50 } 51 52 /** 53 * Header tag rule 54 * @param array $match Header regex match 55 * @return string 56 */ 57 protected function header_tag_rule($match) { 58 return $this->generate_header($match[2], (int)$match[1] - $this->minheaderlevel + 1); 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 72 $minheaderlevel = $this->find_min_header_level($text); 73 74 $h1 = array("<\s*h{$minheaderlevel}\s*>", "<\/h{$minheaderlevel}>"); 75 76 $regex = "/(.*?)({$h1[0]}\s*".preg_quote($header, '/')."\s*{$h1[1]}.*?)((?:{$h1[0]}.*)|$)/is"; 77 preg_match($regex, $text, $match); 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; 88 return preg_replace_callback($this->tagrules['header'], array($this, 'get_repeated_sections_callback'), $text); 89 } 90 91 protected function get_repeated_sections_callback($match) { 92 $text = trim($match[2]); 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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |