Diffs
HatenaSyntax/trunk/test/List.php
@@ -7,13 +7,15 @@
//--
$context = PEG::context('-h');
-list(list($type, $body)) = $result = $p->parse($context)->getData();
-$lime->is($type, '-');
-$lime->is($body, array('h'));
+list($item) = $p->parse($context)->getData()->getChildren();
+$data = $item->getValue();
+$lime->is($data[0], '-');
+$lime->is($data[1], array('h'));
//--
-$context = PEG::context("-a\n+-b");
-list(, list(list($type, $body))) = $p->parse($context)->getData();
-$lime->is($type, '-');
-$lime->is($body, array('b'));
\ No newline at end of file
+$context = PEG::context("-a\n-+b");
+list($a) = $p->parse($context)->getData()->getChildren();
+$lime->is($a->getValue(), array('-', array('a')));
+list($b) = $a->getChildren();
+$lime->is($b->getValue(), array('+', array('b')));
\ No newline at end of file
HatenaSyntax/trunk/test/HttpLink.php
@@ -2,7 +2,7 @@
include_once dirname(__FILE__) . '/t/t.php';
$lime = new lime_test;
-$link = HatenaSyntax_Locator::it()->link;
+$link = HatenaSyntax_Locator::it()->bracket;
//--
HatenaSyntax/trunk/test/Tree.php
@@ -0,0 +1,34 @@
+<?php
+include_once dirname(__FILE__) . '/t/t.php';
+$t = new lime_test;
+
+//--
+
+$arr = array(
+ array('level' => 1, 'value' => 'a'),
+ array('level' => 2, 'value' => 'b'),
+ array('level' => 2, 'value' => 'c')
+);
+
+$root = HatenaSyntax_Tree::make($arr);
+$t->is($root->hasChildren(), true);
+$children = $root->getChildren();
+$t->is($children[0]->getValue(), 'a');
+$t->is($children[0]->hasChildren(), true);
+$children = $children[0]->getChildren();
+$t->is($children[0]->getValue(), 'b');
+$t->is($children[1]->getValue(), 'c');
+
+//--
+
+$arr = array(
+ array('level' => 2, 'value' => 'a'),
+ array('level' => 1, 'value' => 'b')
+);
+
+$root = HatenaSyntax_Tree::make($arr);
+$children = $root->getChildren();
+$t->is($children[1]->getValue(), 'b');
+$t->is($children[1]->hasChildren(), false);
+$children = $children[0]->getChildren();
+$t->is($children[0]->getValue(), 'a');
\ No newline at end of file
HatenaSyntax/trunk/test/ImageLink.php
@@ -2,7 +2,7 @@
include_once dirname(__FILE__) . '/t/t.php';
$lime = new lime_test;
-$parser = HatenaSyntax_Locator::it()->link;
+$parser = HatenaSyntax_Locator::it()->bracket;
//--
HatenaSyntax/trunk/test/KeywordLink.php
@@ -2,7 +2,7 @@
include_once dirname(__FILE__) . '/t/t.php';
$lime = new lime_test;
-$parser = HatenaSyntax_Locator::it()->link;
+$parser = HatenaSyntax_Locator::it()->bracket;
//--
HatenaSyntax/trunk/test/NullLink.php
@@ -2,7 +2,7 @@
include_once dirname(__FILE__) . '/t/t.php';
$lime = new lime_test;
-$parser = HatenaSyntax_Locator::it()->link;
+$parser = HatenaSyntax_Locator::it()->bracket;
//--
HatenaSyntax/trunk/sample/sample1.php
@@ -3,6 +3,7 @@
$str = '*header1
+[:contents]
**header2
@@ -65,68 +66,78 @@
'superprehandler' => 'sprehandler')); // superpre記法の中身を処理するコールバック
/* 結果
<div class="section">
- <h3>header1</h3>
+<h3><a name="hoge_header_0" id="hoge_header_0"></a>header1</h3>
+<div class="toc"><ol>
+<li>
+<a href="#hoge_header_0">header1</a><ol>
+<li>
+<a href="#hoge_header_1">header2</a><ol>
+<li><a href="#hoge_header_2">blockquote header</a></li></ol>
+</li>
+</ol>
+</li>
+</ol>
+</div>
- <h4>header2</h4>
+<h4><a name="hoge_header_1" id="hoge_header_1"></a>header2</h4>
- <dl>
- <dt>definition term</dt>
- <dd>definition description</dd>
- <dd>description2</dd>
- </dl>
+<dl>
+<dt>definition term</dt>
+<dd>definition description</dd>
+<dd>description2</dd>
+</dl>
- <ul>
- <li>list1</li>
- <ol>
- <li>fuga2</li>
- <li>hoge3</li>
- <ul>
- <li>list4</li>
- <li>list5</li>
- </ul>
- </ol>
- <li>list6</li>
- </ul>
+<ul>
+<li>
+list1<ol>
+<li>fuga2</li><li>
+hoge3<ul>
+<li>list4</li><li>list5</li></ul>
+</li>
+</ol>
+</li>
+<li>list6</li></ul>
- <p>paragraph(<a href="#hoge_footnote_1" name="hoge_1" title="footnote">*1</a>)</p>
- <p><a href="./keywordlink">keywordlink</a></p>
- <p>[[nulllink]]</p>
- <table>
- <tr>
- <th>table header </th>
- <th>table header2 </th>
- </tr>
- <tr>
- <td>apple </td>
- <td>1 </td>
- </tr>
- <tr>
- <td>orange </td>
- <td>2 </td>
- </tr>
- </table>
+<p>paragraph(<a href="#hoge_footnote_1" name="hoge_footnotelink_1" id="hoge_footnotelink_1" title="footnote">*1</a>)</p>
+<p><a href="./keywordlink">keywordlink</a></p>
+<p>[[nulllink]]</p>
- <pre>
+<table>
+<tr>
+<th>table header </th>
+<th>table header2 </th>
+</tr>
+<tr>
+<td>apple </td>
+<td>1 </td>
+</tr>
+<tr>
+<td>orange </td>
+<td>2 </td>
+</tr>
+</table>
+
+<pre>
hoge
fuga</pre>
- <p><a href="http://example.com/example.gif"><img src="http://example.com/example.gif" /></a></p>
+<p><a href="http://example.com/example.gif"><img src="http://example.com/example.gif" /></a></p>
- <blockquote>
- <h5>blockquote header</h5>
- <p>fuga</p>
- </blockquote>
+<blockquote>
+<h5><a name="hoge_header_2" id="hoge_header_2"></a>blockquote header</h5>
+<p>fuga</p>
+</blockquote>
<pre class="superpre php">
<?php
echo "hogehoge";</pre>
- <p><a href="http://google.com">http://google.com</a></p>
+<p><a href="http://google.com">http://google.com</a></p>
</div>
<div class="footnote">
- <p><a href="#hoge_1" name="hoge_footnote_1">*1</a>: footnote</p>
+<p><a href="#hoge_footnotelink_1" name="hoge_footnote_1" id="hoge_footnote_1">*1</a>: footnote</p>
</div>
*/
HatenaSyntax/trunk/code/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();
+}
\ No newline at end of file
属性に変更があったパス: HatenaSyntax/trunk/code/HatenaSyntax/Tree/INode.php
___________________________________________________________________
追加: svn:keywords
+ Id
HatenaSyntax/trunk/code/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';
+ }
+}
\ No newline at end of file
属性に変更があったパス: HatenaSyntax/trunk/code/HatenaSyntax/Tree/Root.php
___________________________________________________________________
追加: svn:keywords
+ Id
HatenaSyntax/trunk/code/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';
+ }
+}
\ No newline at end of file
属性に変更があったパス: HatenaSyntax/trunk/code/HatenaSyntax/Tree/Node.php
___________________________________________________________________
追加: svn:keywords
+ Id
HatenaSyntax/trunk/code/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';
+ }
+}
\ No newline at end of file
属性に変更があったパス: HatenaSyntax/trunk/code/HatenaSyntax/Tree/Leaf.php
___________________________________________________________________
追加: svn:keywords
+ Id
HatenaSyntax/trunk/code/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];
+ }
+}
\ No newline at end of file
属性に変更があったパス: HatenaSyntax/trunk/code/HatenaSyntax/TOCRenderer.php
___________________________________________________________________
追加: svn:keywords
+ Id
HatenaSyntax/trunk/code/HatenaSyntax/Util.php
@@ -10,7 +10,7 @@
{
static function normalizeList(Array $data)
{
- return self::filterLevel(self::lower(self::levels($data), $data));
+ return HatenaSyntax_Tree::make($data);
}
static function segment(PEG_IParser $p)
@@ -38,47 +38,9 @@
static function processListItem(Array $li)
{
$ret = array();
- $ret[] = end($li[0]);
- $ret[] = count($li[0]) - 1;
- $ret[] = $li[1];
+ $ret['level'] = count($li[0]) - 1;
+ $ret['value'] = array(end($li[0]), $li[1]);
-
return $ret;
}
-
- static protected function filterLevel($struct)
- {
- foreach ($struct as &$node)
- $node = is_string($node[0]) ? array($node[0], $node[2]) : self::filterLevel($node);
- return $struct;
- }
-
- static protected function lower(Array $levels, Array $data)
- {
- $level = array_pop($levels);
-
- for ($i = 0, $ret = array(), $len = count($data); $i < $len; $i++) {
- if ($data[$i][1] <= $level) {
- $ret[] = $data[$i];
- }
- else {
- for ($arr = array($data[$i++]); $i < $len && $data[$i][1] > $level; $i++) {
- $arr[] = $data[$i];
- }
- $ret[] = self::lower($levels, $arr);
- if ($i < $len) $ret[] = $data[$i];
- }
- }
-
- return $ret;
- }
-
- static protected function levels(Array $data)
- {
- $levels = array();
- foreach ($data as $li) $levels[$li[1]] = true;
- $levels = array_keys($levels);
- rsort($levels, SORT_NUMERIC);
- return $levels;
- }
}
\ No newline at end of file
HatenaSyntax/trunk/code/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;
+ }
+}
\ No newline at end of file
属性に変更があったパス: HatenaSyntax/trunk/code/HatenaSyntax/Tree.php
___________________________________________________________________
追加: svn:keywords
+ Id
HatenaSyntax/trunk/code/HatenaSyntax/Locator.php
@@ -48,7 +48,7 @@
{
$close = '))';
$elt = PEG::andalso(PEG::not($close),
- PEG::choice($this->link, $this->lineChar));
+ PEG::choice($this->bracket, $this->lineChar));
$parser = PEG::pack('((',
HatenaSyntax_Util::segment(PEG::many1($elt)),
@@ -113,11 +113,24 @@
return $parser;
}
- protected function createLink()
+ protected function createTableOfContents()
{
- return PEG::pack('[', PEG::choice($this->nullLink, $this->keywordLink, $this->imageLink, $this->httpLink), ']');
+ $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(':');
@@ -262,6 +275,7 @@
$this->list,
$this->pre,
$this->superpre,
+ $this->tableOfContents,
$this->emptyParagraph,
$this->paragraph));
}
HatenaSyntax/trunk/code/HatenaSyntax/Renderer.php
@@ -8,7 +8,7 @@
class HatenaSyntax_Renderer
{
- protected $config, $footnote, $fncount, $padding, $encoding;
+ protected $config, $footnote, $fncount, $root, $treeRenderer, $headerCount;
function __construct(Array $config = array())
{
@@ -21,15 +21,36 @@
'keywordlinkhandler' => array($this, 'keywordLinkHandler'),
'superprehandler' => array($this, 'superPreHandler')
);
+
+ $this->treeRenderer = new HatenaSyntax_TreeRenderer(array($this, 'listItemCallback'), array($this, 'isOrderedCallback'));
}
- function render(HatenaSyntax_Node $node)
+ 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->padding = 0;
+ $this->root = $rootnode;
+ $this->headerCount = 0;
- $ret = $this->renderNode($node);
+ $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'] . '">' .
@@ -50,26 +71,30 @@
return './' . $path;
}
+ protected function renderTableOfContents()
+ {
+ $tocRenderer = new HatenaSyntax_TOCRenderer();
+ return $tocRenderer->render($this->root, $this->config['id']);
+ }
+
protected function renderNode(HatenaSyntax_Node $node)
{
- $this->padding++;
$ret = $this->{'render' . $node->getType()}($node->getData());
- $this->padding--;
return $ret;
}
protected function renderRoot(Array $arr)
{
- $this->padding--;
foreach ($arr as &$elt) $elt = $this->renderNode($elt);
- $this->padding++;
return join(PHP_EOL, $arr);
}
protected function renderHeader(Array $data)
{
- $level = $data['level'] + $this->config['headerlevel'];
- return $this->line("<h{$level}>" . $this->renderLineSegment($data['body']) . "</h{$level}>");
+ $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)
@@ -91,8 +116,10 @@
$title = $this->escape($title);
}
- $this->footnote .= sprintf(' <p><a href="#%s_%d" name="%s_footnote_%d">*%d</a>: %s</p>' . PHP_EOL, $id, $n, $id , $n, $n, $body);
- return sprintf('(<a href="#%s_footnote_%d" name="%s_%d" title="%s">*%d</a>)', $id, $n, $id, $n, $title, $n);
+ $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)
@@ -120,22 +147,22 @@
protected function renderDefinitionList(Array $data)
{
foreach ($data as &$elt) $elt = $this->renderDefinition($elt);
- return join(PHP_EOL, array($this->line('<dl>'), join(PHP_EOL, $data), $this->line('</dl>')));
+ 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[] = $this->line('<dt>' . $this->renderLineSegment($dt) . '</dt>', 1);
- $ret[] = $this->line('<dd>' . $this->renderLineSegment($dd) . '</dd>', 1);
+ 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[] = $this->line('<pre>');
+ $ret[] = '<pre>';
foreach ($data as &$elt) $elt = $this->renderLineSegment($elt);
$ret[] = join(PHP_EOL, $data) . '</pre>';
return join(PHP_EOL, $ret);
@@ -152,78 +179,49 @@
protected function renderTable(Array $data)
{
$ret = array();
- $ret[] = $this->line('<table>');
- $this->padding++;
+ $ret[] = '<table>';
foreach ($data as $tr) {
- $ret[] = $this->line('<tr>');
+ $ret[] = '<tr>';
foreach ($tr as $td) $ret[] = $this->renderTableCell($td[0], $td[1]);
- $ret[] = $this->line('</tr>');
+ $ret[] = '</tr>';
}
- $this->padding--;
- $ret[] = $this->line('</table>');
+ $ret[] = '</table>';
return join(PHP_EOL, $ret);
}
protected function renderTableCell($header, $segment)
{
$tag = $header ? 'th' : 'td';
- $ret = $this->line("<{$tag}>" . $this->renderLineSegment($segment) . "</{$tag}>", 1);
+ $ret = "<{$tag}>" . $this->renderLineSegment($segment) . "</{$tag}>";
return $ret;
}
protected function renderBlockQuote(Array $arr)
{
$ret = array();
- $ret[] = $this->line('<blockquote>');
+ $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[] = $this->line('</blockquote>');
+ $ret[] = '</blockquote>';
return join(PHP_EOL, $ret);
}
protected function renderParagraph(Array $data)
{
- return $this->line('<p>' . $this->renderLineSegment($data) . '</p>');
+ return '<p>' . $this->renderLineSegment($data) . '</p>';
}
protected function renderEmptyParagraph($data)
{
- $ret = array();
- for ($data--; $data > 0; $data--) $ret[] = $this->line('<br>');
- return join(PHP_EOL, $ret);
+ return str_repeat('<br/ >' . PHP_EOL, max($data - 1, 0));
}
- protected function renderList(Array $data)
+ protected function renderList(HatenaSyntax_Tree_Root $root)
{
- $this->padding--;
- $ret = $this->renderListItem($data);
- $this->padding++;
- return $ret;
+ return $this->treeRenderer->render($root);
}
- protected function renderListItem(Array $data)
- {
- $this->padding++;
- if (is_string($data[0])) { // leaf case
- $result = $this->line('<li>' . $this->renderLineSegment($data[1]) . '</li>');
- }
- else {
- $buf = array();
- $name = $data[0][0] === '+' ? 'ol' : 'ul';
- $buf[] = $this->line("<{$name}>");
- foreach ($data as $elt) $buf[] = $this->renderListItem($elt);
- $buf[] = $this->line("</{$name}>");
- $result = join(PHP_EOL, $buf);
- }
- $this->padding--;
- return $result;
- }
- protected function line($str = '', $padding = 0)
- {
- return str_repeat(' ', max($this->padding + $padding, 0)) . $str;
- }
-
protected static function escape($str)
{
return htmlspecialchars($str, ENT_QUOTES);
HatenaSyntax/trunk/code/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>';
+ }
+}
\ No newline at end of file
属性に変更があったパス: HatenaSyntax/trunk/code/HatenaSyntax/TreeRenderer.php
___________________________________________________________________
追加: svn:keywords
+ Id
HatenaSyntax/trunk/code/HatenaSyntax/Factory.php
@@ -24,7 +24,7 @@
{
$locator = $this->locator;
- $item = PEG::choice($locator->link, $locator->footnote, $locator->lineChar);
+ $item = PEG::choice($locator->bracket, $locator->footnote, $locator->lineChar);
$parser = is_null($cond_parser) ? $item : PEG::secondSeq(PEG::lookaheadNot($cond_parser), $item);
return $parser;
HatenaSyntax/trunk/code/HatenaSyntax.php
@@ -12,7 +12,14 @@
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
{