From: Pierre Habouzit (MadCoder Date: Fri, 28 Jan 2005 21:22:12 +0000 (+0000) Subject: class XOrgWiki* X-Git-Tag: xorg/old~352 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=015a16edf3d2ce3cc68cd7b1e93bbc9678d91365;p=platal.git class XOrgWiki* some classes to parse yawc (yet another wiki code) atm, only blocks are parsed, the syntax is : !foo

foo

!!foo

foo

!!!foo

foo

.foo
foo
*foo #foo
  1. foo
>foo
foo
----
(empty)

foobar

foobar

special thing : [op] foo = bar is understood as : foo
bar
= at the beginning of a line means : stay in the previous block, but with a line break. Now I have to parse line ops like (i think) : **foo** -> foo __foo__ -> foo //foo// -> foo [expl|url] -> expl (expl|url) -> expl + automatic url and mailto detections. all those thing have to be parsed in _parse_line. git-archimport-id: opensource@polytechnique.org--2005/platal--mainline--0.9--patch-406 --- diff --git a/include/wiki.inc.php b/include/wiki.inc.php new file mode 100644 index 0000000..20845da --- /dev/null +++ b/include/wiki.inc.php @@ -0,0 +1,172 @@ +type = $type; + $this->childs = $childs; + } + + // }}} + // {{{ debug function (more or less dunp 2 html) + + function _dump($level = 0) { + echo str_pad('', 2*$level, ' ')."<{$this->type}>\n"; + foreach ($this->childs as $val) { + if (is_string($val)) { + echo str_pad('', 2*$level+2)."$val\n"; + } else { + $val->_dump($level+1); + } + } + echo str_pad('', 2*$level, ' ')."type}>\n"; + } + + // }}} +} + +// }}} +// {{{ class XOrgWiki + +class XOrgWikiParser +{ + // {{{ constructor + + function XOrgWikiParser() + { + } + + // }}} + // {{{ function parse + + function parse($in) + { + $input = str_replace("\r", '', $in); + $input = str_replace("\n=", "\r", $input); + $lines = array_map(Array($this, '_analyse'), split("\n", $input)); + return $this->_share_nests($lines); + } + + // }}} + /* private functions */ + // {{{ function _analyse + + function _analyse(&$line) + { + $modes = Array('!'=>'h1', '!!'=>'h2', '!!!'=>'h3', '>'=>'blockquote', '.'=>'pre', '*'=>'ul', '#'=>'ol'); + $types = Array(); + /* non - nesting blocks */ + if (preg_match('/^(!!?!?|[.>])/', $line, $m)) { + $types[] = $modes[$m[1]]; + return Array($types, substr($line, strlen($m[1]))); + } + + /* nesting blocks */ + $pos = 0; + while ($line{$pos} == '*' || $line{$pos} == '#') { + $types[] = $modes[$line{$pos}]; + $pos ++; + } + + /* nesting blocks ore special lines */ + if ($pos) { + return Array($types, substr($line, $pos)); + } elseif ($line == '----') { + return Array(Array('hr'), ''); + } elseif ($line) { + return Array(Array('p'), $line); + } else { + return Array(Array('br'), ''); + } + } + + // }}} + // {{{ function _merge_nested + + function _share_nests($lines) + { + $res = new XOrgWikiAST('div'); + + while (count($lines)) { + list($types, $line) = array_shift($lines); + $nest = Array(); + while ($types[0] == 'ul' || $types[0] == 'ol') { + $this->_merge_into($types, $line, $nest); + list($types, $line) = array_shift($lines); + } + $nest[] = new XOrgWikiAST($types[0], $this->_parse_line($line)); + $res->childs = array_merge($res->childs, $nest); + } + + return $res; + } + + // }}} + // {{{ function _merge_nest + + function _merge_into($types, $line, &$nest) + { + $ptr =& $nest; + while (($l = count($ptr)) && count($types) && $ptr[$l-1]->type == $types[0]) { + $pos =& $ptr[$l-1]->childs; + $ptr =& $pos[count($pos)-1]->childs; + array_shift($types); + } + + $elt = $this->_parse_line($line); + if (count($types)) { + while (count($types)) { + $elt = Array(new XOrgWikiAST(array_pop($types), Array(new XOrgWikiAST('li', $elt)))); + } + $ptr[] = $elt[0]; + } else { + $pos[] = new XOrgWikiAST('li', $elt); + } + } + + // }}} + // {{{ function _parse_line + + function _parse_line($line) + { + // TODO + return Array($line); + } + + // }}} +} + +// }}} + +/* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker: */ +?>