powered by nequal
Home » HatenaSyntax » Timeline » 1160

Changeset 1160 -- 2009-09-06 19:06:03

Comment
[Package Release] HatenaSyntax

Diffs

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Tree/INode.php

@@ -0,0 +1,19 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+interface HatenaSyntax_Tree_INode
+{
+    function hasValue();
+    function getValue();
+    function hasChildren();
+
+    // HatenaSyntax_Tree_INodeの配列を返す
+    function getChildren();
+
+    function getType();
+}
\ ファイルの末尾に改行がありません
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Tree/INode.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Tree/Root.php

@@ -0,0 +1,44 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+include_once dirname(__FILE__) . '/INode.php';
+
+class HatenaSyntax_Tree_Root implements HatenaSyntax_Tree_INode
+{
+    protected $children;
+
+    function __construct(Array $children)
+    {
+        $this->children = $children;
+    }
+
+    function hasValue()
+    {
+        return false;
+    }
+
+    function getValue()
+    {
+        return null;
+    }
+
+    function hasChildren()
+    {
+        return true;
+    }
+
+    function getChildren()
+    {
+        return $this->children;
+    }
+
+    function getType()
+    {
+        return 'root';
+    }
+}
\ ファイルの末尾に改行がありません
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Tree/Root.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Tree/Node.php

@@ -0,0 +1,44 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+include_once dirname(__FILE__) . '/INode.php';
+
+class HatenaSyntax_Tree_Node implements HatenaSyntax_Tree_INode
+{
+    protected $value, $children;
+
+    function __construct(Array $children, $value = null)
+    {
+        list($this->children, $this->value) = array($children, $value);
+    }
+
+    function hasValue()
+    {
+        return isset($this->value);
+    }
+
+    function getValue()
+    {
+        return $this->value;
+    }
+
+    function hasChildren()
+    {
+        return true;
+    }
+
+    function getChildren()
+    {
+        return $this->children;
+    }
+
+    function getType()
+    {
+        return 'node';
+    }
+}
\ ファイルの末尾に改行がありません
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Tree/Node.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Tree/Leaf.php

@@ -0,0 +1,44 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+include_once dirname(__FILE__) . '/INode.php';
+
+class HatenaSyntax_Tree_Leaf implements HatenaSyntax_Tree_INode
+{
+    protected $value;
+
+    function __construct($value)
+    {
+        $this->value = $value;
+    }
+
+    function hasValue()
+    {
+        return true;
+    }
+
+    function getValue()
+    {
+        return $this->value;
+    }
+
+    function hasChildren()
+    {
+        return false;
+    }
+
+    function getChildren()
+    {
+        return array();
+    }
+
+    function getType()
+    {
+        return 'leaf';
+    }
+}
\ ファイルの末尾に改行がありません
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Tree/Leaf.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/NodeCreater.php

@@ -0,0 +1,33 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+class HatenaSyntax_NodeCreater implements PEG_IParser
+{
+    protected $keys, $parser;
+    function __construct($type, PEG_IParser $parser, Array $keys = array())
+    {
+        $this->type = $type;
+        $this->keys = $keys;
+        $this->parser = $parser;
+    }
+    function parse(PEG_IContext $context)
+    {
+        $result = $this->parser->parse($context);
+        if ($result instanceof PEG_Failure) return $result;
+
+        $data = array();
+        if (count($this->keys) > 0) foreach ($this->keys as $i => $key) {
+            $data[$key] = $result[$i];
+        }
+        else {
+            $data = $result;
+        }
+
+        return new HatenaSyntax_Node($this->type, $data);
+    }
+}
\ ファイルの末尾に改行がありません
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/NodeCreater.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/TOCRenderer.php

@@ -0,0 +1,105 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+class HatenaSyntax_TOCRenderer
+{
+    protected $headerCount;
+
+    function render(HatenaSyntax_Node $rootnode, $id)
+    {
+        $treeroot = HatenaSyntax_Tree::make($this->filter($rootnode));
+        $this->headerCount = 0;
+        $this->id = $id;
+        $renderer = new HatenaSyntax_TreeRenderer(array($this, 'renderHeader'));
+        return '<div class="toc">' . $renderer->render($treeroot) . '</div>';
+    }
+
+    protected function escape($str)
+    {
+        return htmlspecialchars($str, ENT_QUOTES);
+    }
+
+    function renderHeader($node)
+    {
+        $count = $this->headerCount++;
+        $buf = array();
+        $data = $node->getData();
+        foreach ($data['body'] as $leaf) {
+            if (is_string($leaf)) {
+                $buf[] = $leaf;
+            }
+            else {
+                $buf [] = $this->{'render' . $leaf->getType()}($lead->getData());
+            }
+        }
+        return '<a href="#' . $this->id . '_header_' . $count . '">' . $this->escape(join('', $buf)) . '</a>';
+    }
+
+    protected function renderFootnote($data)
+    {
+        return '';
+    }
+
+    function filter(HatenaSyntax_Node $rootnode)
+    {
+        if ($rootnode->getType() !== 'root') throw new Exception;
+        $header_arr = $this->fetchHeader($rootnode);
+        $ret = array();
+        foreach ($header_arr as $header) {
+            $buf = $header->getData();
+            $ret[] = array('level' => $buf['level'], 'value' => $header);
+        }
+        return $ret;
+    }
+
+    protected function fetchHeader($node)
+    {
+        return $this->{'fetchHeaderIn' . $node->getType()}($node);
+    }
+
+    protected function fetchHeaderInRoot($node)
+    {
+        $buf = array();
+        foreach ($node->getData() as $node) {
+            $buf[] = $this->fetchHeader($node);
+        }
+
+        return $this->concat($buf);
+    }
+
+    protected function fetchHeaderInHeader($node)
+    {
+        return array($node);
+    }
+
+    protected function fetchHeaderInBlockQuote($node)
+    {
+        $buf = array();
+        $data = $node->getData();
+        foreach ($data['body'] as $node) {
+            $buf[] = $this->fetchHeader($node);
+        }
+        return $this->concat($buf);
+    }
+
+    protected function __call($name, $args)
+    {
+        if (preg_match('#^fetchHeaderIn\w+$#i', $name)) return array();
+        throw new Exception(sprintf('method(%s) not found', $name));
+    }
+
+    protected function concat(Array $target)
+    {
+        if (!$target) return array();
+        $target = array_reverse($target);
+        while (1 < count($target)) {
+            array_push($target, array_merge(array_pop($target), array_pop($target)));
+        }
+        return $target[0];
+    }
+}
\ ファイルの末尾に改行がありません
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/TOCRenderer.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Util.php

@@ -0,0 +1,46 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+class HatenaSyntax_Util
+{
+    static function normalizeList(Array $data)
+    {
+        return HatenaSyntax_Tree::make($data);
+    }
+
+    static function segment(PEG_IParser $p)
+    {
+        return PEG::callbackAction(array('HatenaSyntax_Util', 'normalizeLineSegment'), $p);
+    }
+
+    static function normalizeLineSegment(Array $data)
+    {
+        for ($ret = array(), $i = 0, $len = count($data); $i < $len; $i++) {
+            if (is_string($data[$i])) {
+                for ($str = $data[$i++]; $i < $len && is_string($data[$i]); $i++) {
+                    $str .= $data[$i];
+                }
+                $ret[] = $str;
+                if ($i < $len) $ret[] = $data[$i];
+            }
+            else {
+                $ret[] = $data[$i];
+            }
+        }
+        return $ret;
+    }
+
+    static function processListItem(Array $li)
+    {
+        $ret = array();
+        $ret['level'] = count($li[0]) - 1;
+        $ret['value'] = array(end($li[0]), $li[1]);
+
+        return $ret;
+    }
+}
\ ファイルの末尾に改行がありません
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Util.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Tree.php

@@ -0,0 +1,66 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+class HatenaSyntax_Tree
+{
+    /**
+     * array('level' => ?, 'value' => ?)の配列を渡す
+     *
+     * @param array $arr
+     */
+    static function make(Array $arr)
+    {
+        return new HatenaSyntax_Tree_Root(self::makeNodeArray($arr));
+    }
+
+    static protected function makeNodeArray(Array $arr)
+    {
+        $i = 0;
+        $len = count($arr);
+        $tree_arr = array();
+        $min_level = self::fetchMinLevel($arr);
+        while ($i < $len) {
+            list($tree_arr[], $i) = self::makeNode($arr, $i, $min_level);
+        }
+        return $tree_arr;
+    }
+
+    static protected function makeNode(Array $arr, $i, $min_level)
+    {
+        $children = array();
+        $len = count($arr);
+        if ($min_level < $arr[$i]['level']) {
+            // Node
+            for (; $i < $len && $min_level < $arr[$i]['level']; $i++) {
+                $children[] = $arr[$i];
+            }
+            return array(new HatenaSyntax_Tree_Node(self::makeNodeArray($children)), $i);
+        }
+        else {
+            // NodeかLeaf
+            $value = $arr[$i]['value'];
+            $i++;
+            for (; $i < $len && $min_level < $arr[$i]['level']; $i++) {
+                $children[] = $arr[$i];
+            }
+            $node = $children ? new HatenaSyntax_Tree_Node(self::makeNodeArray($children), $value) :
+                                new HatenaSyntax_Tree_Leaf($value);
+            return array($node, $i);
+        }
+    }
+
+    static protected function fetchMinLevel(Array $arr)
+    {
+        foreach ($arr as $elt) {
+            if (!isset($level) || $level > $elt['level']) {
+                $level = $elt['level'];
+            }
+        }
+        return $level;
+    }
+}
\ ファイルの末尾に改行がありません
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Tree.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Locator.php

@@ -0,0 +1,282 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+class HatenaSyntax_Locator
+{
+    protected $elt_ref = null;
+    protected $objects = array();
+
+    private function __construct()
+    {
+        $this->setup();
+    }
+
+    static function it()
+    {
+        static $obj = null;
+        return $obj ? $obj : $obj = new self;
+    }
+
+    public function __get($name)
+    {
+        return isset($this->objects[$name]) ?
+            $this->objects[$name] :
+            $this->objects[$name] = $this->{'create' . $name}();
+    }
+
+    protected function createFactory()
+    {
+        return new HatenaSyntax_Factory($this);
+    }
+
+    protected function createLineChar()
+    {
+        return PEG::second(PEG::not(PEG::char("\n\r")), PEG::anything());
+    }
+
+    protected function createEndOfLine()
+    {
+        return PEG::choice(PEG::newLine(), PEG::eos());
+    }
+
+    protected function createFootnote()
+    {
+        $close = '))';
+        $elt = PEG::andalso(PEG::not($close),
+                            PEG::choice($this->bracket, $this->lineChar));
+
+        $parser = PEG::pack('((',
+                            HatenaSyntax_Util::segment(PEG::many1($elt)),
+                            $close);
+
+        return $this->factory->createNodeCreater('footnote', $parser);
+    }
+
+    protected function createLineElement()
+    {
+        return $this->factory->createLineElement();
+    }
+
+    protected function createLineSegment()
+    {
+        return HatenaSyntax_Util::segment(PEG::many($this->lineElement));
+    }
+
+    protected function createHttpLink()
+    {
+        $title_char = PEG::andalso(PEG::not(']'),
+                                   $this->lineChar);
+
+        $title = PEG::second(':title=', PEG::join(PEG::many1($title_char)));
+
+        $url_char = PEG::andalso(PEG::not(PEG::choice(']', ':title=')),
+                                 $this->lineChar);
+
+        $url = PEG::join(PEG::seq(PEG::choice('http://', 'https://'),
+                                  PEG::many1($url_char)));
+        $parser = PEG::seq($url, PEG::optional($title));
+
+        return $this->factory->createNodeCreater('httplink', $parser, array('href', 'title'));
+    }
+
+    protected function createImageLink()
+    {
+        $url_char = PEG::subtract(PEG::anything(), ']', ':image]');
+
+        $url = PEG::join(PEG::seq(PEG::choice('http://', 'https://'),
+                                  PEG::many1($url_char)));
+
+        $parser = PEG::first($url, ':image');
+
+        return $this->factory->createNodeCreater('imagelink', $parser);
+    }
+
+    protected function createKeywordLink()
+    {
+        $body = PEG::join(PEG::many1(PEG::subtract(PEG::anything(), PEG::newLine(), ']]')));
+        $body = PEG::subtract($body, 'javascript:', ' ', "\t");
+        $parser = PEG::pack('[', $body, ']');
+
+        return $this->factory->createNodeCreater('keywordlink', $parser);
+    }
+
+    protected function createNullLink()
+    {
+        $body = PEG::join(PEG::many1(PEG::subtract(PEG::anything(), '[]', PEG::newLine())));
+        $parser = PEG::pack(']', $body, '[');
+
+        return $parser;
+    }
+
+    protected function createTableOfContents()
+    {
+        $parser = PEG::seq(PEG::token('[:contents]'), $this->endOfLine);
+
+        return $this->factory->createNodeCreater('tableofcontents', $parser);
+    }
+
+    protected function createInlineTableOfContents()
+    {
+        $parser = PEG::token(':contents');
+        return $this->factory->createNodeCreater('tableofcontents', $parser);
+    }
+
+    protected function createBracket()
+    {
+        return PEG::pack('[', PEG::choice($this->inlineTableOfContents, $this->nullLink, $this->keywordLink, $this->imageLink, $this->httpLink), ']');
+    }
+
+    protected function createDefinition()
+    {
+        $c = PEG::token(':');
+        $sep = PEG::drop($c);
+        $factory = $this->factory;
+        $parser = PEG::seq($sep,
+                           $factory->createLineSegment($c, true),
+                           $sep,
+                           $factory->createLineSegment($c),
+                           PEG::drop($this->endOfLine));
+        return $parser;
+    }
+
+    protected function createDefinitionList()
+    {
+        $parser = PEG::many1($this->definition);
+        return $this->factory->createNodeCreater('definitionlist', $parser);
+    }
+
+    protected function createPre()
+    {
+        $nl = PEG::newLine();
+        $closing = PEG::seq(PEG::optional($nl), '|<', $this->endOfLine);
+        $line = PEG::second($nl, $this->factory->createLineSegment($closing));
+        $parser = PEG::pack('>|', PEG::many1($line), $closing);
+
+        return $this->factory->createNodeCreater('pre', $parser);
+    }
+
+    protected function createSuperPreElement()
+    {
+        $cond = PEG::not(PEG::seq('||<', $this->endOfLine));
+        $elt = PEG::second($cond, $this->lineChar);
+        $parser = PEG::third(PEG::newLine(), $cond, PEG::join(PEG::many($elt)));
+
+        return $parser;
+
+    }
+
+    protected function createHeader()
+    {
+        $parser = PEG::seq(PEG::drop('*'),
+                           PEG::count(PEG::many('*')),
+                           HatenaSyntax_Util::segment(PEG::many(PEG::choice($this->lineChar, $this->footnote))),
+                           PEG::drop($this->endOfLine));
+
+        return $this->factory->createNodeCreater('header', $parser, array('level', 'body'));
+    }
+
+    protected function createSuperPre()
+    {
+        $open = PEG::pack('>|',
+                          PEG::join(PEG::many(PEG::secondSeq(PEG::lookaheadNot(PEG::char("\r\n|")), PEG::anything()))),
+                          '|');
+        $body = PEG::many1($this->superPreElement);
+
+        $close = PEG::drop(PEG::optional(PEG::newLine()),
+                           '||<',
+                           $this->endOfLine);
+
+        $parser = PEG::seq($open, $body, $close);
+
+        return $this->factory->createNodeCreater('superpre', $parser, array('type', 'body'));
+    }
+
+    protected function createList()
+    {
+        $item = PEG::callbackAction(array('HatenaSyntax_Util', 'processListItem'), PEG::many1(PEG::char('-+')),
+                                                                                   $this->lineSegment,
+                                                                                   PEG::drop($this->endOfLine));
+        $list = PEG::callbackAction(array('HatenaSyntax_Util', 'normalizeList'), PEG::many1($item));
+
+        return $this->factory->createNodeCreater('list', $list);
+    }
+
+    protected function createTableCell()
+    {
+        $parser = PEG::seq(PEG::drop('|', PEG::lookaheadNot($this->endOfLine)),
+                           PEG::optional('*'),
+                           $this->factory->createLineSegment(PEG::token('|'), true));
+        return $parser;
+    }
+
+    protected function createTable()
+    {
+        $line = PEG::first(PEG::many1($this->tableCell),
+                           '|',
+                           $this->endOfLine);
+        $parser = PEG::many1($line);
+
+        return $this->factory->createNodeCreater('table', $parser);
+    }
+
+    protected function createBlockQuote()
+    {
+        $url = PEG::join(PEG::seq(PEG::choice('http://', 'https://'),
+                                  PEG::many1(PEG::subtract(PEG::anything(),
+                                                           PEG::seq('>', PEG::newLine()),
+                                                           PEG::newLine()))));
+
+        $header = PEG::pack('>', PEG::optional($url), PEG::seq('>', PEG::newLine()));
+
+        $elt = PEG::second(PEG::not('<<', $this->endOfLine), $this->element);
+
+        $parser = PEG::seq($header, PEG::many1($elt), PEG::drop('<<', $this->endOfLine));
+
+        return $this->factory->createNodeCreater('blockquote', $parser, array('url', 'body'));
+    }
+
+    protected function createParagraph()
+    {
+        $parser = PEG::first($this->lineSegment, $this->endOfLine);
+
+        return $this->factory->createNodeCreater('paragraph', $parser);
+    }
+
+    protected function createEmptyParagraph()
+    {
+        $parser = PEG::count(PEG::many1(PEG::newLine()));
+        return $this->factory->createNodeCreater('emptyparagraph', $parser);
+    }
+
+    protected function createElement()
+    {
+        $parser = PEG::ref($ref);
+        $this->elt_ref = &$ref;
+        return $parser;
+    }
+
+    protected function createParser()
+    {
+        return $this->factory->createNodeCreater('root', PEG::many($this->element));
+    }
+
+    protected function setup()
+    {
+        $this->element;
+        $this->elt_ref = PEG::memo(PEG::choice($this->header,
+                                               $this->blockQuote,
+                                               $this->definitionList,
+                                               $this->table,
+                                               $this->list,
+                                               $this->pre,
+                                               $this->superpre,
+                                               $this->tableOfContents,
+                                               $this->emptyParagraph,
+                                               $this->paragraph));
+    }
+}
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Locator.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Node.php

@@ -0,0 +1,27 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+class HatenaSyntax_Node
+{
+    protected $type, $data = array();
+    function __construct($type, $data)
+    {
+        $this->type = $type;
+        $this->data = $data;
+    }
+
+    function getType()
+    {
+        return $this->type;
+    }
+
+    function getData()
+    {
+        return $this->data;
+    }
+}
\ ファイルの末尾に改行がありません
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Node.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Renderer.php

@@ -0,0 +1,229 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+class HatenaSyntax_Renderer
+{
+    protected $config, $footnote, $fncount, $root, $treeRenderer, $headerCount;
+
+    function __construct(Array $config = array())
+    {
+        $this->config = $config + array(
+            'headerlevel' => 1,
+            'htmlescape' => true,
+            'id' => uniqid('sec'),
+            'sectionclass' => 'section',
+            'footnoteclass' => 'footnote',
+            'keywordlinkhandler' => array($this, 'keywordLinkHandler'),
+            'superprehandler' => array($this, 'superPreHandler')
+        );
+
+        $this->treeRenderer = new HatenaSyntax_TreeRenderer(array($this, 'listItemCallback'), array($this, 'isOrderedCallback'));
+    }
+
+    function listItemCallback(Array $data)
+    {
+        list(, $lineSegment) = $data;
+        return $this->renderLineSegment($lineSegment);
+    }
+
+    function isOrderedCallback(HatenaSyntax_Tree_INode $node)
+    {
+        $children = $node->getChildren();
+        foreach ($children as $child) {
+            if ($child->hasValue()) {
+                $buf = $child->getValue();
+                return $buf[0] === '+';
+            }
+        }
+        return false;
+    }
+
+    function render(HatenaSyntax_Node $rootnode)
+    {
+        $this->footnote = '';
+        $this->fncount = 0;
+        $this->root = $rootnode;
+        $this->headerCount = 0;
+
+        $ret = $this->renderNode($rootnode);
+        $ret = '<div class="' . $this->config['sectionclass'] . '">' . PHP_EOL . $ret . PHP_EOL . '</div>' . PHP_EOL;
+        if ($this->fncount > 0) {
+            $ret .= PHP_EOL . PHP_EOL . '<div class="' . $this->config['footnoteclass'] . '">' .
+                    PHP_EOL . $this->footnote .  '</div>';
+        }
+
+        return $ret;
+    }
+
+    static function superPreHandler($type, $lines)
+    {
+        $body = join(PHP_EOL, array_map(array('HatenaSyntax_Renderer', 'escape'), $lines));
+        return '<pre class="superpre">' . PHP_EOL . $body . '</pre>';
+    }
+
+    static function keywordLinkHandler($path)
+    {
+        return './' . $path;
+    }
+
+    protected function renderTableOfContents()
+    {
+        $tocRenderer = new HatenaSyntax_TOCRenderer();
+        return $tocRenderer->render($this->root, $this->config['id']);
+    }
+
+    protected function renderNode(HatenaSyntax_Node $node)
+    {
+        $ret = $this->{'render' . $node->getType()}($node->getData());
+        return $ret;
+    }
+
+    protected function renderRoot(Array $arr)
+    {
+        foreach ($arr as &$elt) $elt = $this->renderNode($elt);
+        return join(PHP_EOL, $arr);
+    }
+
+    protected function renderHeader(Array $data)
+    {
+        $level = $data['level'] + $this->config['headerlevel'];
+        $name = $this->config['id'] . '_header_' . $this->headerCount++;
+        $anchor = '<a name="' . $name . '" id="' . $name . '"></a>';
+        return "<h{$level}>" . $anchor . $this->renderLineSegment($data['body']) . "</h{$level}>";
+    }
+
+    protected function renderLineSegment(Array $data)
+    {
+        foreach ($data as &$elt)
+            $elt = !$elt instanceof HatenaSyntax_Node ? ($this->config['htmlescape'] ? $this->escape($elt) : $elt)
+                                                      : $this->renderNode($elt);
+        return join('', $data);
+    }
+
+    protected function renderFootnote(Array $data)
+    {
+        $this->fncount++;
+        $id = $this->config['id'];
+        $n = $this->fncount;
+        $title = $body = $this->renderLineSegment($data);
+        $title = strip_tags($body);
+        if (!$this->config['htmlescape']) {
+            $title = $this->escape($title);
+        }
+
+        $fnname = sprintf('%s_footnote_%d', $id, $n);
+        $fnlinkname = sprintf('%s_footnotelink_%d', $id, $n);
+        $this->footnote .= sprintf('<p><a href="#%s" name="%s" id="%s">*%d</a>: %s</p>' . PHP_EOL, $fnlinkname, $fnname, $fnname, $n, $body);
+        return sprintf('(<a href="#%s" name="%s" id="%s" title="%s">*%d</a>)', $fnname, $fnlinkname, $fnlinkname, $title, $n);
+    }
+
+    protected function renderHttpLink(Array $data)
+    {
+        list($href, $title) = array($data['href'], $data['title']);
+        $title = $title ? $title : $href;
+        if ($this->config['htmlescape']) $title = $this->escape($title);
+        $href = $this->escape($href);
+        return sprintf('<a href="%s">%s</a>', $href, $title);
+    }
+
+    protected function renderImageLink($url)
+    {
+        $url = self::escape($url);
+        return '<a href="' . $url . '"><img src="' . $url . '" /></a>';
+    }
+
+    protected function renderKeywordLink($path)
+    {
+        $path = self::escape($path);
+        $href = call_user_func($this->config['keywordlinkhandler'], $path);
+        return '<a href="' . $href . '">' . $path . '</a>';
+    }
+
+    protected function renderDefinitionList(Array $data)
+    {
+        foreach ($data as &$elt) $elt = $this->renderDefinition($elt);
+        return join(PHP_EOL, array('<dl>', join(PHP_EOL, $data), '</dl>'));
+    }
+
+    protected function renderDefinition(Array $data)
+    {
+        list($dt, $dd) = $data;
+        $ret = array();
+        if ($dt) $ret[] = '<dt>' . $this->renderLineSegment($dt) . '</dt>';
+        $ret[] = '<dd>' . $this->renderLineSegment($dd) . '</dd>';
+        return join(PHP_EOL, $ret);
+    }
+
+    protected function renderPre(Array $data)
+    {
+        $ret = array();
+        $ret[] = '<pre>';
+        foreach ($data as &$elt) $elt = $this->renderLineSegment($elt);
+        $ret[] = join(PHP_EOL, $data) . '</pre>';
+        return join(PHP_EOL, $ret);
+    }
+
+    protected function renderSuperPre(Array $data)
+    {
+        $ret = array();
+        list($type, $lines) = array($data['type'], $data['body']);
+        $ret[] = call_user_func($this->config['superprehandler'], $type, $lines);
+        return join(PHP_EOL, $ret);
+    }
+
+    protected function renderTable(Array $data)
+    {
+        $ret = array();
+        $ret[] = '<table>';
+        foreach ($data as $tr) {
+            $ret[] = '<tr>';
+            foreach ($tr as $td) $ret[] = $this->renderTableCell($td[0], $td[1]);
+            $ret[] = '</tr>';
+        }
+        $ret[] = '</table>';
+        return join(PHP_EOL, $ret);
+    }
+
+    protected function renderTableCell($header, $segment)
+    {
+        $tag = $header ? 'th' : 'td';
+        $ret = "<{$tag}>" . $this->renderLineSegment($segment) . "</{$tag}>";
+        return $ret;
+    }
+
+    protected function renderBlockQuote(Array $arr)
+    {
+        $ret = array();
+        $ret[] = '<blockquote>';
+        foreach ($arr['body'] as $elt) $ret[] = $this->renderNode($elt);
+        if ($arr['url']) $ret[] = $this->line('<cite><a href="' . self::escape($arr['url']) . '">' . self::escape($arr['url']) . '</a></cite>');
+        $ret[] = '</blockquote>';
+        return join(PHP_EOL, $ret);
+    }
+
+    protected function renderParagraph(Array $data)
+    {
+        return '<p>' . $this->renderLineSegment($data) . '</p>';
+    }
+
+    protected function renderEmptyParagraph($data)
+    {
+        return str_repeat('<br/ >' . PHP_EOL, max($data - 1, 0));
+    }
+
+    protected function renderList(HatenaSyntax_Tree_Root $root)
+    {
+        return $this->treeRenderer->render($root);
+    }
+
+
+    protected static function escape($str)
+    {
+        return htmlspecialchars($str, ENT_QUOTES);
+    }
+}
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Renderer.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/TreeRenderer.php

@@ -0,0 +1,78 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+class HatenaSyntax_TreeRenderer
+{
+    protected $valueCallback, $isOrderedCallback;
+
+    function __construct($valueCallback, $isOrderedCallback = false)
+    {
+        $this->valueCallback = $valueCallback;
+        $this->isOrderedCallback = $isOrderedCallback ? $isOrderedCallback : array($this, 'isOrderedDefaultCallback');
+    }
+
+    function isOrderedDefaultCallback($node)
+    {
+        return true;
+    }
+
+    protected function renderValue($value)
+    {
+        return call_user_func($this->valueCallback, $value);
+    }
+
+    protected function isOrdered($node)
+    {
+        return call_user_func($this->isOrderedCallback, $node);
+    }
+
+    protected function listOpenTag($bool)
+    {
+        return ($bool ? '<ol>' : '<ul>') . PHP_EOL;
+    }
+
+    protected function listCloseTag($bool)
+    {
+        return ($bool ? '</ol>' : '</ul>') . PHP_EOL;
+    }
+
+    function render(HatenaSyntax_Tree_Root $root)
+    {
+        $ordered = $this->isOrdered($root);
+        $ret = $this->listOpenTag($ordered);
+        foreach ($root->getChildren() as $child) {
+            $ret .= $this->_render($child);
+        }
+        $ret .= $this->listCloseTag($ordered);
+        return $ret;
+    }
+
+    protected function _render($node)
+    {
+        return $this->{'render' . $node->getType()}($node);
+    }
+
+    protected function renderNode($node)
+    {
+        $ret = '<li>' . PHP_EOL;
+        if ($node->hasValue()) $ret .= $this->renderValue($node->getValue());
+        $ordered = $this->isOrdered($node);
+        $ret .= $this->listOpenTag($ordered);
+        foreach ($node->getChildren() as $child) {
+            $ret .= $this->_render($child);
+        }
+        $ret .= $this->listCloseTag($ordered);
+        $ret .= '</li>' . PHP_EOL;
+        return $ret;
+    }
+
+    protected function renderLeaf($node)
+    {
+        return '<li>' . $this->renderValue($node->getValue()) . '</li>';
+    }
+}
\ ファイルの末尾に改行がありません
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/TreeRenderer.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Factory.php

@@ -0,0 +1,44 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+class HatenaSyntax_Factory
+{
+    protected $locator;
+
+    function __construct(HatenaSyntax_Locator $locator)
+    {
+        $this->locator = $locator;
+    }
+
+    public function __get($name)
+    {
+        return strtolower($name) === 'locator' ? $this->locator : $this->locator->$name;
+    }
+
+    function createLineElement(PEG_IParser $cond_parser = null)
+    {
+        $locator = $this->locator;
+
+        $item = PEG::choice($locator->bracket, $locator->footnote, $locator->lineChar);
+        $parser = is_null($cond_parser) ? $item : PEG::secondSeq(PEG::lookaheadNot($cond_parser), $item);
+
+        return $parser;
+    }
+
+    function createLineSegment(PEG_IParser $cond_parser, $optional = false)
+    {
+        $elt = $this->createLineElement($cond_parser);
+        $parser = $optional ? PEG::many($elt) : PEG::many1($elt);
+        return HatenaSyntax_Util::segment($parser);
+    }
+
+    function createNodeCreater($type, PEG_IParser $parser, Array $keys = array())
+    {
+        return new HatenaSyntax_NodeCreater($type, $parser, $keys);
+    }
+}
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax/Factory.php
___________________________________________________________________
名前: svn:keywords
+ Id

HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax.php

@@ -0,0 +1,37 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id$
+ */
+
+include_once 'PEG.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/Node.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/Locator.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/Factory.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/NodeCreater.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/Renderer.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/TOCRenderer.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/Util.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/TreeRenderer.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/Tree.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/Tree/INode.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/Tree/Node.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/Tree/Root.php';
+include_once dirname(__FILE__) . '/HatenaSyntax/Tree/Leaf.php';
+
+class HatenaSyntax
+{
+    static function parse($str)
+    {
+        return HatenaSyntax_Locator::it()->parser->parse(PEG::context($str));
+    }
+
+    static function render($str, $config = array())
+    {
+        $node = self::parse($str);
+        $renderer = new HatenaSyntax_Renderer($config);
+        return $renderer->render($node);
+    }
+}
\ ファイルの末尾に改行がありません
属性に変更があったパス: HatenaSyntax/tags/release-0.9.8-20090906190603/HatenaSyntax.php
___________________________________________________________________
名前: svn:keywords
+ Id