|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00012 include_once("wikimarkup.php"); 00013 00014 class nwiki_parser extends wiki_markup_parser { 00015 00016 protected $blockrules = array( 00017 'nowiki' => array( 00018 'expression' => "/^<nowiki>(.*?)<\/nowiki>/ims", 00019 'tags' => array(), 00020 'token' => array('<nowiki>', '</nowiki>') 00021 ), 00022 'header' => array( 00023 'expression' => "/^\ *(={1,6})\ *(.+?)(={1,6})\ *$/ims", 00024 'tags' => array(), //none 00025 'token' => '=' 00026 ), 00027 'line_break' => array( 00028 'expression' => "/^-{3,4}\s*$/im", 00029 'tags' => array(), 00030 'token' => '---' 00031 ), 00032 'desc_list' => array( 00033 'expression' => "/(?:^.+?\:.+?\;\n)+/ims", 00034 'tags' => array(), 00035 'token' => array(':', ';'), 00036 'tag' => 'dl' 00037 ), 00038 'table' => array( 00039 'expression' => "/\{\|(.+?)\|\}/ims" 00040 ), 00041 'tab_paragraph' => array( 00042 'expression' => "/^(\:+)(.+?)$/ims", 00043 'tag' => 'p' 00044 ), 00045 'list' => array( 00046 'expression' => "/^((?:\ *[\*|#]{1,5}\ *.+?)+)(\n\s*(?:\n|<(?:h\d|pre|table|tbody|thead|tr|th|td|ul|li|ol|hr)\ *\/?>))/ims", 00047 'tags' => array(), 00048 'token' => array('*', '#') 00049 ), 00050 'paragraph' => array( 00051 'expression' => "/^\ *((?:<(?!\ *\/?(?:h\d|pre|table|tbody|thead|tr|th|td|ul|li|ol|hr)\ *\/?>)|[^<\s]).+?)\n\s*\n/ims", 00052 //not specified -> all tags (null or unset) 00053 'tag' => 'p' 00054 ) 00055 ); 00056 00057 protected $tagrules = array( 00058 'nowiki' => array( 00059 'expression' => "/<nowiki>(.*?)<\/nowiki>/is", 00060 'token' => array('<nowiki>', '</nowiki>') 00061 ), 00062 'image' => array( 00063 'expression' => "/\[\[image:(.+?)\|(.+?)\]\]/is", 00064 'token' => array("[[image:", "|alt]]") 00065 ), 00066 'attach' => array( 00067 'expression' => "/\[\[attach:(.+?)\]\]/is", 00068 'token' => array("[[attach:", "|name]]") 00069 ), 00070 'link' => array( 00071 'expression' => "/\[\[(.+?)\]\]/is", 00072 'tag' => 'a', 00073 'token' => array("[[", "]]") 00074 ), 00075 'url_tag' => array( 00076 'expression' => "/\[(.+?)\]/is", 00077 'tag' => 'a', 00078 'token' => array("[", "]") 00079 ), 00080 'url' => array( 00081 'expression' => "/(?<!=\")((?:https?|ftp):\/\/[^\s\n]+[^,\.\?!:;\"\'\n\ ])/i", 00082 'tag' => 'a', 00083 'token' => 'http://' 00084 ), 00085 'italic' => array( 00086 'expression' => "/\'{3}(.+?)(\'{3}(?:\'{2})?)/is", 00087 'tag' => 'em', 00088 'token' => array("'''", "'''") 00089 ), 00090 'bold' => array( 00091 'expression' => "/\'{2}(.+?)\'{2}/is", 00092 'tag' => 'strong', 00093 'token' => array("''", "''") 00094 ) 00095 ); 00096 00097 protected function after_parsing() { 00098 parent::after_parsing(); 00099 } 00100 00105 protected function header_block_rule($match) { 00106 if($match[1] != $match[3]) { 00107 return $match[0]; 00108 } 00109 00110 $num = strlen($match[1]); 00111 00112 return $this->generate_header($match[2], $num); 00113 } 00114 00115 protected function table_block_rule($match) { 00116 $rows = explode("\n|-", $match[1]); 00117 $table = array(); 00118 foreach($rows as $r) { 00119 $colsendline = explode("\n", $r); 00120 $cols = array(); 00121 foreach($colsendline as $ce) { 00122 $cols = array_merge($cols, $this->get_table_cells($ce)); 00123 } 00124 00125 if(!empty($cols)) { 00126 $table[] = $cols; 00127 } 00128 } 00129 return $this->generate_table($table); 00130 } 00131 00132 private function get_table_cells($string) { 00133 $string = ltrim($string); 00134 $type = (!empty($string) && $string[0] == "!") ? 'header' : 'normal'; 00135 $string = substr($string, 1); 00136 if(empty($string)) { 00137 $normalcells = array(); 00138 } 00139 else { 00140 $normalcells = explode("||", $string); 00141 } 00142 $cells = array(); 00143 foreach($normalcells as $nc) { 00144 $headercells = explode("!!", $nc); 00145 $countheadercells = count($headercells); 00146 for($i = 0; $i < $countheadercells; $i++) { 00147 $cells[] = array($type, $headercells[$i]); 00148 $type = 'header'; 00149 } 00150 $type = 'normal'; 00151 } 00152 00153 return $cells; 00154 } 00155 00156 protected function tab_paragraph_block_rule($match) { 00157 $num = strlen($match[1]); 00158 $text = $match[2]; 00159 $html = ""; 00160 for($i = 0; $i < $num - 1; $i++) { 00161 $html = parser_utils::h('p', $html, array('class' => 'wiki_tab_paragraph')); 00162 } 00163 00164 return parser_utils::h('p', $text, array('class' => 'wiki_tab_paragraph')); 00165 } 00166 00167 protected function desc_list_block_rule($match) { 00168 preg_match_all("/^(.+?)\:(.+?)\;$/ims", $match[0], $listitems, PREG_SET_ORDER); 00169 00170 $list = ""; 00171 foreach($listitems as $li) { 00172 $term = $li[1]; 00173 $this->rules($term); 00174 00175 $description = $li[2]; 00176 $this->rules($description); 00177 00178 $list .= parser_utils::h('dt', $term).parser_utils::h('dd', $description); 00179 } 00180 00181 return $list; 00182 } 00183 00191 protected function italic_tag_rule($match) { 00192 $text = $match[1]; 00193 if(strlen($match[2]) == 5) { 00194 $text .= "''"; 00195 } 00196 00197 $this->rules($text, array('only' => array('bold'))); 00198 if(strpos($text, "''") !== false) { 00199 $text = str_replace("''", $this->protect("''"), $text); 00200 } 00201 00202 return array($text, array()); 00203 } 00204 00209 protected function link_tag_rule($match) { 00210 return $this->format_link($match[1]); 00211 } 00212 00213 protected function url_tag_tag_rule($match) { 00214 $text = trim($match[1]); 00215 if(preg_match("/(.+?)\|(.+)/is", $text, $matches)) { 00216 $link = $matches[1]; 00217 $text = $matches[2]; 00218 } 00219 else if(preg_match("/(.+?)\ (.+)/is", $text, $matches)) { 00220 $link = $matches[1]; 00221 $text = $matches[2]; 00222 } 00223 else { 00224 $link = $text; 00225 } 00226 return array($this->protect($text), array('href' => $this->protect($link))); 00227 } 00228 00229 protected function url_tag_rule($match) { 00230 $url = $this->protect($match[1]); 00231 $options = array('href' => $url); 00232 00233 return array($url, $options); 00234 } 00235 00240 protected function image_tag_rule($match) { 00241 return $this->format_image($match[1], $match[2]); 00242 } 00243 00244 protected function attach_tag_rule($match) { 00245 $parts = explode("|", $match[1]); 00246 00247 $url = array_shift($parts); 00248 00249 if(count($parts) > 0) { 00250 $text = array_shift($parts); 00251 } 00252 00253 $extension = substr($url, strrpos($url, ".")); 00254 $text = empty($text) ? $url : $text; 00255 00256 $imageextensions = array('jpg', 'jpeg', 'png', 'bmp', 'gif', 'tif'); 00257 if(in_array($extension, $imageextensions)) { 00258 $align = 'left'; 00259 if(count($parts) > 0) { 00260 switch(strtolower($text)) { 00261 case 'right': 00262 $align = 'right'; 00263 break; 00264 case 'center': 00265 $align = 'center'; 00266 break; 00267 default: 00268 $align = 'left'; 00269 } 00270 $text = $parts[0]; 00271 } 00272 return $this->format_image($url, $text, $text, $align); 00273 } 00274 else { 00275 $url = $this->real_path($url); 00276 return parser_utils::h('a', $text, array('href' => $url, 'class' => 'wiki-attachment')); 00277 } 00278 } 00279 }