delegate,$method)) {
+ $result = $this->delegate->$method($result);
+ }
+ //echo "$$$$$ ".$result."\n";
+
+ return $result;
+
+ }
+}
+
+
+?>
+
+// high level
+$parser = create_wiki_parser();
+$html_text = $parser->parse($wiki_text);
+
+// low level
+ $parser = new Gakoparser();
+ $parser->setDelegate(new MarkupInterpreter());
+ $parser->addDelim('bold','**');
+ echo $parser->parse('hello **world**');
+
+
+ */
+
+
+class GakowikiMarkupToHTMLTranslator {
+
+ var $toc = array();
+
+ var $references = array();
+
+ /** replaces all line breaks by "__newline__" that are meant to replaced back by a call to __post() */
+ function __pre($str) {
+ $result = $str;
+
+ // we often use nelines to have pretty HTML code
+ // such as in tables
+ // however, they are no "real" newlines to be transformed in
+ $result = preg_replace("/>\s*(\n|\r\n)/",'>__newline__',$result);
+ return $result;
+ }
+
+ function bib($str) {
+ $this->references[] = $str;
+ return '['.count($this->references).'] '.$str;
+ }
+
+ function cite($str) {
+ return "@@@".$str."@@@";
+ }
+
+ function escape_newline($str) {
+ return preg_replace("/(\n|\r\n)/","__newline__",$str);
+ }
+
+ function toc($str) {
+ return '+++TOC+++';
+ }
+
+ function __post($str) {
+ $result = $str;
+ $result = preg_replace("/(\n|\r\n)/","
\n",$result);
+
+ // workaround to support the semantics change in pre mode
+ // and the semantics of embedded HTML
+ $result = preg_replace("/__newline__/","\n",$result);// must be at the end
+
+ // cleaning the additional
+ // this is really nice
+ $result = preg_replace("/(<\/h.>)
/i","\\1 ",$result);
+
+ // adding the table of contents
+ $result = str_replace($this->toc(''),implode('
',$this->toc),$result);
+
+ // adding the references
+ $citeregexp = '/@@@(.*?)@@@/';
+ if (preg_match_all($citeregexp,$result,$matches)) {
+ foreach($matches[1] as $m) {
+ $theref = '';
+ foreach ($this->references as $k => $ref) {
+ if (preg_match('/'.preg_quote($m).'/i', $ref)) {
+//echo $m.' '.$ref;
+ // if we have already a match it is not deterministic
+ if ($theref!='') $result = "undeterministic citation: ".$m;
+ $theref = $ref;
+ $result = preg_replace('/@@@'.preg_quote($m).'@@@/i', '['.($k+1).']', $result);
+ }
+ }
+ }
+ }
+
+ return $result;
+ }
+
+ /** adds tags and prevents newline to be replaced by
by __post */
+ function pre($str) {
+ return ''.$this->escape_newline($str).'
';
+ }
+
+ /** prevents newline to be replaced by
by __post */
+ function unwrap($str) {
+ return $this->escape_newline($str);
+ }
+
+ /** adds tags */
+ function bold($str) {
+ return ''.$str.'';
+ }
+
+ /** adds tags */
+ function italic($str) {
+ return ''.$str.'';
+ }
+
+ function table($str) {
+ $result = '';
+ foreach(preg_split('/\n/',$str) as $line) {
+ if (strlen(trim($line))>0) {
+ $result .= '';
+ foreach(preg_split('/&&/',$line) as $field) {
+ $result .= ''.$field.' | ';
+
+ }
+ $result .= '
';
+ }
+ }
+
+ return '';
+ }
+
+
+ function __create_anchor($m) {
+ return preg_replace("/[^a-zA-Z]/","",$m);
+ }
+ function h2($str) {
+ $tag = $this->__create_anchor($str);
+ $this->toc[] = "".$str."";
+ return ''.''.$str."
";
+ }
+
+ function h3($str) {
+ $tag = $this->__create_anchor($str);
+ $this->toc[] = " ".$str."";
+ return ''.''.$str."
";
+ }
+
+ function monotype($str) {
+ return ''.str_replace('<','<',$str).'
';
+ }
+
+ function link($str) {
+
+ if (preg_match('/(.*)\|(.*)/',$str, $matches)) {
+ $rawurl = $matches[1];
+ $text = $matches[2];
+ } else {$rawurl=$str;$text=$str;}
+
+ $url=$rawurl;
+
+ if (!preg_match("/(#|^http|^mailto)/",$rawurl)) {
+ if (function_exists('logical2url')) {
+ $url=logical2url($rawurl);
+ } else {
+ $url=$rawurl;
+
+ }
+ }
+
+ return ''.trim($text).'';
+ }
+
+ function phpcode($str) {
+ ob_start();
+ eval($str);
+ return $this->escape_newline(ob_get_clean());
+ }
+
+ function phpcode2($str) {
+ return gk_wiki2html($this->phpcode($str));
+ }
+
+ function a($str) {
+ return '';
+ }
+
+ function script($str) {
+ return '';
+ }
+
+ function img($str) {
+ return '
';
+ }
+
+ function img2($str) {
+ return '
';
+ }
+
+
+ function html($str) {
+ return '<'.$str.'>';
+ }
+
+ function iframe($str) {
+ return '