|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00012 include_once("wikimarkup.php"); 00013 00014 class creole_parser extends wiki_markup_parser { 00015 00016 protected $blockrules = array( 00017 'nowiki' => array( 00018 'expression' => "/^\{\{\{(.*?)\}\}\}/ims", 00019 'tags' => array(), 00020 'token' => array('{{{', '}}}') 00021 ), 00022 'header' => array( 00023 'expression' => "/^\ *(={1,6})\ *(.+?)=*\ *$/ims", 00024 'tags' => array(), //none 00025 'token' => '=' 00026 ), 00027 'table' => array( 00028 'expression' => "/^(?:\|.*?\|\ *\n)+/ims" 00029 ), 00030 'line_break' => array( 00031 'expression' => "/^----\s*$/im", 00032 'token' => '----', 00033 'tags' => array() 00034 ), 00035 'list' => array( 00036 'expression' => "/((?:^\ *[\*#][^\*#]\ *.+?)(?:^\ *[\*#]{1,5}\ *.+?)*)(\n\s*(?:\n|<(?:h\d|pre|table|tbody|thead|tr|th|td|ul|li|ol|hr)))/ims", 00037 'tags' => array(), 00038 'token' => array('*', '#') 00039 ), 00040 'paragraph' => array( 00041 'expression' => "/^\ *((?:<(?!\ *\/?(?:h\d|pre|table|tbody|thead|tr|th|td|ul|li|ol|hr)\ *\/?>)|[^<\s]).+?)\n\s*\n/ims", 00042 //not specified -> all tags (null or unset) 00043 'tag' => 'p' 00044 ) 00045 ); 00046 00047 protected $tagrules = array( 00048 'nowiki' => array( 00049 'expression' => "/\{\{\{(.*?)\}\}\}/is", 00050 'token' => array('{{{', '}}}') 00051 ), 00052 'image' => array( 00053 'expression' => "/\~?\{\{(.+)\|(.+)\}\}/i", 00054 'tags' => array(), 00055 'token' => array('{{', '|Alt}}') 00056 ), 00057 'link' => array( 00058 'expression' => "/\~?\[\[(.+?)\]\]/is", 00059 'tag' => 'a', 00060 'token' => array('[[', ']]') 00061 ), 00062 'url' => array( 00063 'expression' => "/\~?(?<!=\")((?:https?|ftp):\/\/[^\s\n]+[^,\.\?!:;\"\'\n\ ])/is", 00064 'tag' => 'a', 00065 'token' => "http://" 00066 ), 00067 'line_break' => array( 00068 'expression' => "/\\\\\\\\/", 00069 'tag' => 'br', 00070 'simple' => true, 00071 'token' => '----' 00072 ), 00073 'bold' => array( 00074 'expression' => "/\*\*(.*?)(?:\*\*|$)/is", 00075 'tag' => 'strong', 00076 'token' => array('**', '**') 00077 ), 00078 'italic' => array( 00079 'expression' => "#(?<!http:|https:|ftp:)//(.+?)(?<!http:|https:|ftp:)//#is", 00080 'tag' => 'em', 00081 'token' => array('//', '//') 00082 ) 00083 ); 00084 00089 protected function before_parsing() { 00090 $this->string = htmlspecialchars($this->string); 00091 parent::before_parsing(); 00092 } 00093 00094 protected function header_block_rule($match) { 00095 $num = strlen($match[1]); 00096 00097 $text = trim($match[2]); 00098 00099 $text = preg_replace("/\s*={1,$num}$/im", "", $text); 00100 00101 return $this->generate_header($text, $num); 00102 } 00103 00108 protected function table_block_rule($match) { 00109 00110 $rows = explode("\n", $match[0]); 00111 $table = array(); 00112 foreach($rows as $r) { 00113 if(empty($r)) { 00114 continue; 00115 } 00116 $rawcells = explode("|", $r); 00117 $cells = array(); 00118 00119 array_shift($rawcells); 00120 array_pop($rawcells); 00121 00122 foreach($rawcells as $c) { 00123 if(!empty($c)) { 00124 if($c[0] == "=") { 00125 $type = 'header'; 00126 $c = substr($c, 1); 00127 } 00128 else { 00129 $type = 'normal'; 00130 } 00131 $this->rules($c); 00132 $cells[] = array($type, $c); 00133 } 00134 } 00135 $table[] = $cells; 00136 } 00137 00138 return $this->generate_table($table); 00139 } 00140 00141 protected function paragraph_block_rule($match) { 00142 $text = $match[1]; 00143 foreach($this->tagrules as $tr) { 00144 if(isset($tr['token'])) { 00145 if(is_array($tr['token'])) { 00146 $this->escape_token_string($text, $tr['token'][0]); 00147 $this->escape_token_string($text, $tr['token'][1]); 00148 } 00149 else { 00150 $this->escape_token_string($text, $tr['token']); 00151 } 00152 } 00153 } 00154 $this->escape_token_string($text, "~"); 00155 00156 return $text; 00157 } 00158 00162 private function escape_token_string(&$text, $token) { 00163 $text = str_replace("~".$token, $this->protect($token), $text); 00164 } 00165 00170 protected function url_tag_rule($match) { 00171 if(strpos($match[0], "~") === 0) { 00172 return substr($match[0], 1); 00173 } 00174 else { 00175 $text = trim($match[0]); 00176 $options = array('href' => $text); 00177 00178 return array($text, $options); 00179 } 00180 } 00181 00182 protected function link_tag_rule($match) { 00183 $text = trim($match[1]); 00184 00185 if(strpos($match[0], "~") === 0) { 00186 return substr($match[0], 1); 00187 } 00188 else { 00189 return $this->format_link($text); 00190 } 00191 } 00192 00196 protected function bold_tag_rule($match) { 00197 $text = $match[1]; 00198 $this->rules($text, array('only' => array('italic'))); 00199 if(strpos($text, "//") !== false) { 00200 $text = str_replace("//", $this->protect("//"), $text); 00201 } 00202 return array($text, array()); 00203 } 00204 00205 protected function image_tag_rule($match) { 00206 if(strpos($match[0], "~") === 0) { 00207 return substr($match[0], 1); 00208 } 00209 00210 return $this->format_image($match[1], $match[2]); 00211 } 00212 }