|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00011 include_once("nwiki.php"); 00012 00013 class html_parser extends nwiki_parser { 00014 protected $blockrules = array(); 00015 00016 protected $section_editing = true; 00017 00018 public function __construct() { 00019 parent::__construct(); 00020 $this->tagrules = array('link' => $this->tagrules['link'], 'url' => $this->tagrules['url']); 00021 00022 //headers are considered tags here... 00023 $h1 = array("<\s*h1\s*>", "<\/h1>"); 00024 $this->tagrules['header1'] = array('expression' => "/{$h1[0]}(.+?){$h1[1]}/is" 00025 ); 00026 } 00027 00028 protected function before_parsing() { 00029 parent::before_parsing(); 00030 00031 $this->rules($this->string); 00032 } 00033 00037 protected function header1_tag_rule($match) { 00038 return $this->generate_header($match[1], 1); 00039 } 00040 00045 public function get_section($header, $text, $clean = false) { 00046 if ($clean) { 00047 $text = preg_replace('/\r\n/', "\n", $text); 00048 $text = preg_replace('/\r/', "\n", $text); 00049 $text .= "\n\n"; 00050 } 00051 00052 $h1 = array("<\s*h1\s*>", "<\/h1>"); 00053 00054 preg_match("/(.*?)({$h1[0]}\s*\Q$header\E\s*{$h1[1]}.*?)((?:\n{$h1[0]}.*)|$)/is", $text, $match); 00055 00056 if (!empty($match)) { 00057 return array($match[1], $match[2], $match[3]); 00058 } else { 00059 return false; 00060 } 00061 } 00062 00063 protected function get_repeated_sections(&$text, $repeated = array()) { 00064 $this->repeated_sections = $repeated; 00065 return preg_replace_callback($this->tagrules['header1'], array($this, 'get_repeated_sections_callback'), $text); 00066 } 00067 00068 protected function get_repeated_sections_callback($match) { 00069 $text = trim($match[1]); 00070 00071 if (in_array($text, $this->repeated_sections)) { 00072 $this->returnvalues['repeated_sections'][] = $text; 00073 return parser_utils::h('p', $text); 00074 } else { 00075 $this->repeated_sections[] = $text; 00076 } 00077 00078 return $match[0]; 00079 } 00080 }