source: applications/nimpad/branches/0.2/lib/HatenaSyntax/TreeRenderer.php @ 1488

SVN
Revision 1488, 2.2 KB checked in by anatoo, 10 months ago (diff)

HatenaSyntax?とPEGをリポジトリの中に置いておくようにした

Line 
1<?php
2/**
3 * @package HatenaSyntax
4 * @author anatoo<anatoo@nequal.jp>
5 * @license http://www.opensource.org/licenses/mit-license.php MIT License
6 * @version $Id: TreeRenderer.php 1159 2009-09-06 09:43:29Z anatoo $
7 */
8
9class HatenaSyntax_TreeRenderer
10{
11    protected $valueCallback, $isOrderedCallback;
12   
13    function __construct($valueCallback, $isOrderedCallback = false)
14    {
15        $this->valueCallback = $valueCallback;
16        $this->isOrderedCallback = $isOrderedCallback ? $isOrderedCallback : array($this, 'isOrderedDefaultCallback');
17    }
18   
19    function isOrderedDefaultCallback($node)
20    {
21        return true;
22    }
23   
24    protected function renderValue($value)
25    {
26        return call_user_func($this->valueCallback, $value);
27    }
28   
29    protected function isOrdered($node)
30    {
31        return call_user_func($this->isOrderedCallback, $node);
32    }
33   
34    protected function listOpenTag($bool)
35    {
36        return ($bool ? '<ol>' : '<ul>') . PHP_EOL;
37    }
38   
39    protected function listCloseTag($bool)
40    {
41        return ($bool ? '</ol>' : '</ul>') . PHP_EOL;
42    }
43   
44    function render(HatenaSyntax_Tree_Root $root)
45    {
46        $ordered = $this->isOrdered($root);
47        $ret = $this->listOpenTag($ordered);
48        foreach ($root->getChildren() as $child) {
49            $ret .= $this->_render($child);
50        }
51        $ret .= $this->listCloseTag($ordered);
52        return $ret;
53    }
54   
55    protected function _render($node)
56    {
57        return $this->{'render' . $node->getType()}($node);
58    }
59   
60    protected function renderNode($node)
61    {
62        $ret = '<li>' . PHP_EOL;
63        if ($node->hasValue()) $ret .= $this->renderValue($node->getValue());
64        $ordered = $this->isOrdered($node);
65        $ret .= $this->listOpenTag($ordered);
66        foreach ($node->getChildren() as $child) {
67            $ret .= $this->_render($child);
68        }
69        $ret .= $this->listCloseTag($ordered);
70        $ret .= '</li>' . PHP_EOL;
71        return $ret;
72    }
73   
74    protected function renderLeaf($node)
75    {
76        return '<li>' . $this->renderValue($node->getValue()) . '</li>';
77    }
78}
Note: See TracBrowser for help on using the repository browser.