Index: /applications/nimpad/trunk/html/.htaccess
===================================================================
--- /applications/nimpad/trunk/html/.htaccess	(revision 1483)
+++ /applications/nimpad/trunk/html/.htaccess	(revision 1490)
@@ -1,23 +1,20 @@
-# PHP設定 -------------------------------------------------------------------
-# ログ設定
+# PHP --------------------------------------------------------------------------
+# log settings
 php_value error_log              "../log/error.log"
-# php_value error_log "/usr/local/var/www/nimpad.jp/log/error.log" # 本番時適用
+# php_value error_log "/usr/local/var/www/nimpad.jp/log/error.log" # for deployment
 php_value log_errors             on
 php_value html_errors            off
 
-# Enable display errors in browser
-# 本番ではオフにする
 php_value display_startup_errors on
 php_value display_errors         on
 
-# セッション設定 
+# session 
 php_value session.gc_maxlifetime 604800
 
-# 諸設定 -----------------------------------------------------------------------
+# etc --------------------------------------------------------------------------
 DirectorySlash Off
 Options -Indexes FollowSymLinks
 DirectoryIndex index.php
 
-# ドットファイルへのアクセスは弾く 
 <Files ".*">
     deny from all
@@ -27,5 +24,5 @@
 RewriteEngine on
 
-# メンテナンス時以外はコメントアウトしておく
+# for maintenance
 #RewriteRule ^maintenance/ - [L]
 #RewriteRule ^.*$ maintenance/
Index: /applications/nimpad/trunk/lib/nim/Mailer.php
===================================================================
--- /applications/nimpad/trunk/lib/nim/Mailer.php	(revision 1376)
+++ /applications/nimpad/trunk/lib/nim/Mailer.php	(revision 1490)
@@ -6,5 +6,5 @@
     function __construct($from_author, $from_address)
     {
-        $this->from = 'From: ' . mb_encode_mimeheader(mb_convert_encoding($from_author, 'JIS', 'UTF-8')) . '<' . $from_address . '>';
+        $this->from = 'From: ' . mb_encode_mimeheader(mb_convert_encoding($from_author, 'JIS', 'UTF-8')) . ' <' . $from_address . '>';
     }
     
Index: /applications/nimpad/trunk/lib/nim/locator/Deployment.php
===================================================================
--- /applications/nimpad/trunk/lib/nim/locator/Deployment.php	(revision 1333)
+++ /applications/nimpad/trunk/lib/nim/locator/Deployment.php	(revision 1490)
@@ -12,3 +12,11 @@
         return array(new nim_ExceptionHandler($this->factory, $this->context), 'handleForDeployment');
     }
+    
+    protected function createPdo()
+    {
+        $option = array(/*PDO::ATTR_PERSISTENT => true, */PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
+        $pdo = new PDO('mysql:dbname=nimpad;host=127.0.0.1', 'nimpad', $this->dbPassword, $option);
+        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+        return $pdo;
+    }
 }
Index: /applications/nimpad/trunk/lib/components/Root.php
===================================================================
--- /applications/nimpad/trunk/lib/components/Root.php	(revision 1339)
+++ /applications/nimpad/trunk/lib/components/Root.php	(revision 1490)
@@ -36,5 +36,5 @@
         }
         
-        $name = $this->nimpadAdmin->createNewWiki($text);
+        $name = $this->nimpadAdmin->createNewWiki($text, $this->url('/'));
         return new k_SeeOther($this->url($name . '/'));
     }
Index: /applications/nimpad/trunk/lib/HatenaSyntax.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax.php	(revision 1490)
@@ -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: HatenaSyntax.php 1159 2009-09-06 09:43:29Z anatoo $
+ */
+
+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);
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG.php	(revision 1490)
@@ -0,0 +1,689 @@
+<?php
+/**
+ * このクラスは、静的メソッドから様々なパーサやコンテキスト等を生成するのに使われる。
+ * 
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: PEG.php 1044 2009-08-01 06:52:10Z anatoo $
+ */
+
+include_once dirname(__FILE__) . '/PEG/IContext.php';
+include_once dirname(__FILE__) . '/PEG/IParser.php';
+
+include_once dirname(__FILE__) . '/PEG/Action.php';
+include_once dirname(__FILE__) . '/PEG/And.php';
+include_once dirname(__FILE__) . '/PEG/Anything.php';
+include_once dirname(__FILE__) . '/PEG/ArrayContext.php';
+include_once dirname(__FILE__) . '/PEG/CallbackAction.php';
+include_once dirname(__FILE__) . '/PEG/Cache.php';
+include_once dirname(__FILE__) . '/PEG/Char.php';
+include_once dirname(__FILE__) . '/PEG/Choice.php';
+include_once dirname(__FILE__) . '/PEG/Curry.php';
+include_once dirname(__FILE__) . '/PEG/EOS.php';
+include_once dirname(__FILE__) . '/PEG/Failure.php';
+include_once dirname(__FILE__) . '/PEG/Lookahead.php';
+include_once dirname(__FILE__) . '/PEG/Many.php';
+include_once dirname(__FILE__) . '/PEG/Memoize.php';
+include_once dirname(__FILE__) . '/PEG/Not.php';
+include_once dirname(__FILE__) . '/PEG/Optional.php';
+include_once dirname(__FILE__) . '/PEG/Ref.php';
+include_once dirname(__FILE__) . '/PEG/Sequence.php';
+include_once dirname(__FILE__) . '/PEG/StringContext.php';
+include_once dirname(__FILE__) . '/PEG/Token.php';
+include_once dirname(__FILE__) . '/PEG/Util.php';
+
+class PEG
+{
+    protected static function parser($val)
+    {
+        return is_string($val) ?  self::token($val) : $val;
+    }
+    
+    protected static function parserArray(Array $arr)
+    {
+        foreach ($arr as &$val) $val = self::parser($val);
+        return $arr;
+    }
+    
+    /**
+     * 引数に応じて適切なPEG_IContextインスタンスを生成する。
+     * 
+     * @param string|Array $str
+     * @return PEG_IContext
+     * @see PEG_IContext, PEG_StringContext, PEG_ArrayContext
+     */
+    static function context($val)
+    {
+        if (is_string($val)) return new PEG_StringContext($val);
+        if (is_array($val)) return new PEG_ArrayContext($val);
+        throw new InvalidArgumentException();
+    }
+    
+    /**
+     * PEG_CallbackActionインスタンスを生成する。
+     * PEG::callbackAction($callback, PEG::seq($a, $b, $c)), PEG::callbackAction($callback, $a, $b, $c) は同等
+     * 
+     * @param callback $callback
+     * @param $p
+     * @return PEG_CallbackAction
+     * @see PEG_CallbackAction
+     */
+    static function callbackAction($callback, $p)
+    {
+        if (func_num_args() > 2) {
+            $args = func_get_args();
+            array_shift($args);
+            $p = new PEG_Sequence(self::parserArray($args));
+        }
+        return new PEG_CallbackAction($callback, self::parser($p));
+    }
+    
+    /**
+     * PEG_Anythingインスタンスを得る。
+     * このパーサはどのような文字でもパースに成功する
+     * 
+     * @return PEG_Anything
+     * @see PEG_Anything
+     */
+    static function anything()
+    {
+        static $obj = null;
+        return $obj ? $obj : $obj = new PEG_Anything;
+    }
+    
+    /**
+     * PEG_Choiceインスタンスを生成する。
+     * このパーサは、パース時に与えられたパーサを順に試していき、初めに成功したパーサの結果をそのまま返す
+     * 全てのパーサが失敗したならば、このパーサは失敗する。
+     * 
+     * @return PEG_Choice
+     * @param ...
+     * @see PEG_Choice
+     */
+    static function choice()
+    {
+        return new PEG_Choice(self::parserArray(func_get_args()));
+    }
+    
+    /**
+     * PEG_EOSインスタンスを得る。
+     * このパーサは、パース時に対象が終端に来た、
+     * つまり$aContext->eos() === trueの時の
+     * PEG_IContextインスタンスを与えられたときのみ成功する。
+     * 
+     * @return PEG_EOS
+     */
+    static function eos()
+    {
+        static $obj = null;
+        return $obj ? $obj : $obj = new PEG_EOS;
+    }
+    
+    /**
+     * PEG_Notインスタンスを得る。
+     * このパーサは、$pパーサが成功したならば失敗し、$pパーサが失敗したならば成功する。
+     * PEG_Notパーサは文字列を消費しない
+     * PEG::not(PEG::seq($a, $b, $c)), PEG::not($a, $c, $c) は同等
+     * 
+     * @param $p
+     * @return PEG_Not
+     */
+    static function not($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequence(self::parserArray($args));
+        }
+        return new PEG_Not(self::parser($p));
+    }
+    
+    /**
+     * 与えられたパーサが失敗した場合でもfalseを返すパーサを返す
+     * 正規表現でいう"?"
+     * PEG::optional(PEG::seq($a, $b, $c)), PEG::optional($a, $b, $c) は同等。
+     * 
+     * @param $p
+     * @return PEG_Parser
+     */
+    static function optional($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequence(self::parserArray($args));
+        }
+        return new PEG_Optional(self::parser($p));
+    }
+    
+    
+    /**
+     * 複数のパーサを一つにまとめる
+     * 
+     * @return PEG_Sequence
+     */
+    static function seq()
+    {
+        return new PEG_Sequence(self::parserArray(func_get_args()));
+    }
+    
+    /**
+     * 与えられたパーサを失敗するまで繰り返し、配列を返すパーサを得る
+     * PEG::many(PEG::seq($a, $b, $c)), PEG::many($a, $b, $c) は同等
+     * 
+     * @param $p
+     * @return PEG_Many
+     */
+    static function many($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequence(self::parserArray($args));
+        }
+        return new PEG_Many(self::parser($p));
+    }
+    
+    /**
+     * 与えられたパーサを失敗するまで繰り返し、配列を返すパーサを得る
+     * パーサが一度も成功しない場合は失敗する
+     * PEG::many1(PEG::seq($a, $b, $c)), PEG::many1($a, $b, $c) は同等
+     * 
+     * @param $p
+     * @return PEG_Many1
+     */
+    static function many1($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequense(self::parserArray($args));
+        }
+        return self::callbackAction(array('PEG_Util', 'cons'), self::seq($p, self::many($p)));
+    }
+    
+    /**
+     * 与えられた文字列をそのまま返すパーサを得る
+     * 
+     * @param string $s
+     * @return PEG_Token
+     */
+    static function token($s)
+    {
+        return PEG_Token::get($s);
+    }
+    
+    /**
+     * 与えられたパーサを実行した後、PEG_IContextの読み込み位置を元に戻すパーサを得る
+     * PEG::amp($parser)と同等
+     * PEG::lookahead(PEG::seq($a, $b, $c)), PEG::lookahead($a, $b, $c) は同等
+     * 非推奨。代わりにPEG::ampを使う
+     * 
+     * @param $p
+     * @return PEG_Lookahead
+     * @deprecated 
+     */
+    static function lookahead($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequense(self::parserArray($args));
+        }
+        return new PEG_Lookahead(self::parser($p));
+    }
+    
+    /**
+     * PEG::not($parser)と同等
+     * PEG::lookaheadNot(PEG::seq($a, $b, $c)), PEG::lookaheadNot($a, $b, $c) は同等
+     * 非推奨。代わりにPEG::notを使う
+     * 
+     * @param $p
+     * @return PEG_Not
+     * @deprecated 
+     */
+    static function lookaheadNot($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequense(self::parserArray($args));
+        }
+        return self::not(self::parser($p));
+    }
+
+    /**
+     * 
+     * @return PEG_And
+     */
+    static function andalso()
+    {
+        return new PEG_And(self::parserArray(func_get_args()));
+    }
+
+    /**
+     * 与えたリファレンスをパース時にパーサとして実行するパーサを得る
+     * 
+     * @return PEG_Ref
+     */
+    static function ref(&$parser)
+    {
+        return new PEG_Ref($parser);
+    }
+
+    /**
+     * 与えた文字列に含まれる文字にヒットするパーサを得る
+     * 
+     * @param string $str
+     * @return PEG_Char
+     */
+    static function char($str)
+    {
+        return new PEG_Char($str);
+    }
+
+    /**
+     * 数字にヒットするパーサを得る
+     * 
+     * @return PEG_Char
+     */
+    static function digit()
+    {
+        static $obj = null;
+        return $obj ? $obj : $obj = self::char('0123456789');
+    }
+    
+    /**
+     * 改行にヒットするパーサを得る
+     * 
+     * @return PEG_Choice
+     */
+    static function newLine()
+    {
+        static $obj = null;
+        return $obj ? $obj : $obj = self::choice(self::token("\r\n"), self::char("\r\n"));
+    }
+    
+    /**
+     * 行の終わりにヒットするパーサを返す
+     * 
+     * @return PEG_Choice
+     */
+    static function lineEnd()
+    {
+        static $p = null;
+        return $p ? $p : $p = self::choice(self::newLine(), self::eos());
+    }
+    
+    /**
+     * アルファベットの大文字にヒットするパーサを得る
+     * 
+     * @return PEG_Char
+     */
+    static function upper()
+    {
+        static $obj = null;
+        return $obj ? $obj : $obj = self::char('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
+    }
+    
+    /**
+     * アルファベットの小文字にヒットするパーサを得る
+     * 
+     * @return PEG_Char
+     */
+    static function lower()
+    {
+        static $obj = null;
+        return $obj ? $obj : $obj = self::char('abcdefghijklmnopqrstuvwxyz');
+    }
+    
+    /**
+     * アルファベットにヒットするパーサを得る
+     * 
+     * @return PEG_Choice
+     */
+    static function alphabet()
+    {
+        static $obj = null;
+        return $obj ? $obj : $obj = self::choice(self::lower(), self::upper());
+    }
+
+    /**
+     * 
+     * 
+     * @param $key
+     * @param $p
+     * @return PEG_At
+     */
+    static function at($key, $p)
+    {
+        $curry = PEG_Curry::make(array('PEG_Util', 'at'), $key);
+        return self::callbackAction($curry, $p);
+    }
+
+    /**
+     * 与えられたパーサが何か値を返したとき、その値の最初の要素を返すパーサを得る
+     * PEG::first(PEG::seq($a, $b, $c)), PEG::first($a, $b, $c) は同等
+     * 
+     * @param $p
+     * @return PEG_At
+     */
+    static function first($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequense(self::parserArray($args));
+        }
+        return self::at(0, self::parser($p));
+    }
+
+    /**
+     * 与えられたパーサが何か値を返したとき、その値の二番目の要素を返すパーサを得る
+     * PEG::second(PEG::seq($a, $b, $c)), PEG::second($a, $b, $c) は同等
+     * 
+     * @param $p
+     * @return PEG_At
+     */
+    static function second($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequense(self::parserArray($args));
+        }
+        return self::at(1, self::parser($p));
+    }
+
+    /**
+     * 与えられたパーサが何か値を返したとき、その値の三番目の要素を返すパーサを得る
+     * PEG::third(PEG::seq($a, $b, $c)), PEG::third($a, $b, $c) は同等
+     * 
+     * @param $p
+     * @return PEG_At
+     */
+    static function third($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequense(self::parserArray($args));
+        }
+        return self::at(2, self::parser($p));
+    }
+
+    /**
+     * $start, $body, $endの三つのパーサを一つにまとめて、$bodyの返す値のみを返すパーサを得る
+     * 
+     * @param $start
+     * @param $body
+     * @param $end
+     * @return PEG_At
+     */
+    static function pack($start, $body, $end)
+    {
+        return self::second(self::seq(self::parser($start), self::parser($body), self::parser($end)));
+    }
+
+    /**
+     * 与えられたパーサが返す配列を平らにするパーサを得る
+     * PEG::flatten(PEG::seq($a, $b, $c)), PEG::flatten($a, $b, $c) と同等
+     * 
+     * @param $p
+     */
+    static function flatten($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequence(self::parserArray($args));
+        }
+        return self::callbackAction(array('PEG_Util', 'flatten'), $p);
+    }
+
+
+    /**
+     * 与えられたパーサがパース時に何を返そうともnullを返すパーサを得る
+     * PEG::seqの引数に使うと、自動的に抜かされる
+     * PEG::drop(PEG::seq($a, $b, $c), PEG::drop($a, $b, $c) は同等
+     * 
+     * @param $p
+     * @return PEG_Drop 
+     */
+    static function drop($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequence(self::parserArray($args));
+        }
+        return self::callbackAction(array('PEG_Util', 'drop'), $p);
+    }
+
+    /**
+     * PEG::create('Klass', PEG::seq($a, $b, $c)), PEG::create('Klass', $a, $b, $c) は同等
+     * 
+     * @param string $klass
+     * @param $p
+     */
+    static function create($klass, $p)
+    {
+        if (func_num_args() > 2) {
+            $args = func_get_args();
+            array_shift($args);
+            $p = new PEG_Sequence(self::parserArray($args));
+        }
+        $curry = PEG_Curry::make(array('PEG_Util', 'create'), $klass);
+        return self::callbackAction($curry, $p);
+    }
+    
+    /**
+     * 与えれたパーサがパース時に配列を返すとして、その配列をjoinして返すパーサを得る
+     * 
+     * @param $p
+     * @param string $glue
+     */
+    static function join($p, $glue = '')
+    {
+        $curry = PEG_Curry::make(array('PEG_Util', 'join'), $glue);
+        return self::callbackAction($curry, $p);
+    }
+
+    /**
+     * 与えられたパーサがパース時に何か返す時、その値をcount()した値を返すパーサを得る
+     * 
+     * @param $p
+     * @return PEG_CallbackAction
+     */
+    static function count($p)
+    {
+        return self::callbackAction(array('PEG_Util', 'count'), $p);
+    }
+    
+    /**
+     * 
+     *
+     * @param $item
+     * @param $glue
+     * @return PEG_CallbackAction
+     */
+    static function listof($item, $glue)
+    {
+        $parser = self::seq($item, self::many(self::secondSeq($glue, $item)));
+        return self::callbackAction(array('PEG_Util', 'cons'), $parser);
+    }
+
+    /**
+     * 半角空白かタブにヒットするパーサを得る
+     *
+     * @return PEG_Char
+     */
+    static function blank()
+    {
+        static $obj = null;
+        return $obj ? $obj : $obj = self::char(" \t");
+    }
+    
+    /**
+     * PEG::first(PEG::seq($a, $b, ...)), PEG::first($a, $b, $c) 等と同等
+     * 非推奨。PEG::firstを使う
+     *
+     * @return PEG_At
+     * @deprecated 
+     */
+    static function firstSeq()
+    {
+        return self::first(new PEG_Sequence(self::parserArray(func_get_args())));
+    }
+    
+    /**
+     * PEG::second(PEG::seq($a, $b, ...)), PEG::second($a, $b, $c) 等と同等
+     * 非推奨。 PEG::secondを使う
+     * 
+     * @deprecated 
+     * @return PEG_At
+     */
+    static function secondSeq()
+    {
+        return self::second(new PEG_Sequence(self::parserArray(func_get_args())));
+    }
+    
+    /**
+     * PEG::third(PEG::seq($a, $b, ...)), PEG::third($a, $b, $c) 等と同等
+     * 非推奨。PEG::thirdを使う
+     * 
+     * @return PEG_At
+     * @deprecated 
+     */
+    static function thirdSeq()
+    {
+        return self::third(new PEG_Sequence(self::parserArray(func_get_args())));
+    }
+    
+    /**
+     * 渡されたパーサがパース時に返す値の最後の値を返すパーサを得る
+     * PEG::tail(PEG::seq($a, $b, $c)), PEG::tail($a, $b, $c) は同等
+     *
+     * @param unknown_type $p
+     * @return unknown
+     */
+    static function tail($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequense(self::parserArray($args));
+        }
+        return self::callbackAction(array('PEG_Util', 'tail'), self::parser($p));
+    }
+    
+    /**
+     * PEG::tail(PEG::seq($a, $b, ...)), PEG::tail($a, $b, $c) 等と同等
+     * 非推奨。PEG::tailを使う
+     *
+     * @return PEG_CallbackAction
+     * @deprecated
+     */
+    static function tailSeq()
+    {
+        return self::tail(new PEG_Sequence(self::parserArray(func_get_args())));
+    }
+    
+    /**
+     * 与えられたパーサを先読みパーサにする
+     * PEG::lookaheadの代わりにこれを使う
+     * PEG::amp(PEG::seq($a, $b, $c), PEG::amp($a, $b, $c) は同等
+     *
+     * @param $p
+     * @return PEG_Lookahead
+     */
+    static function amp($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequense(self::parserArray($args));
+        }
+        return new PEG_Lookahead(self::parser($p));
+    }
+    
+    /**
+     * PEG::subtract($a, $b, $c), PEG::tailSeq(PEG::not($a), PEG::not($b), $c) は同等
+     *
+     * @param unknown_type $p
+     * @return unknown
+     */
+    static function subtract($p)
+    {
+        $args = func_get_args();
+        array_shift($args);
+        foreach ($args as &$elt) {
+            $elt = self::not(self::parser($elt));
+        }
+        $args[] = self::parser($p);
+        return call_user_func_array(array('PEG', 'tailSeq'), self::parserArray($args));
+    }
+    
+    /**
+     * PEG_Failureインスタンスを返す
+     *
+     * @return PEG_Failure
+     */
+    static function failure()
+    {
+        return PEG_Failure::it();
+    }
+    
+    /**
+     * パーサをメモ化する
+     * PEG::memo(PEG::seq($a, $b, $c)), PEG::memo($a, $b, $c) は同等
+     *
+     * @param $p
+     * @return PEG_Memoize
+     */
+    static function memo($p)
+    {
+        if (func_num_args() > 1) {
+            $args = func_get_args();
+            $p = new PEG_Sequense(self::parserArray($args));
+        }
+        return new PEG_Memoize(self::parser($p));
+    }
+
+    /**
+     * パーサが最初にヒットした時に返した値を返す
+     *
+     * @param PEG_IParser $parser
+     * @param $subject
+     * @return unknown
+     */
+    static function match(PEG_IParser $parser, $subject)
+    {
+        return self::_match($parser, self::context($subject));
+    }
+    
+    static function _match(PEG_IParser $parser, PEG_IContext $context, $need_matching_start = false)
+    {
+        while(!$context->eos()) {
+            $start = $context->tell();
+            $result = $parser->parse($context);
+            $end = $context->tell();
+            if ($result instanceof PEG_Failure) {
+                $context->seek($start + 1);
+            }
+            else {
+                return $need_matching_start ? array($result, $start) : $result;
+            }
+        }
+        return $need_matching_start ? array(self::failure(), null) : self::failure();
+    }
+    
+    /**
+     * パーサがヒットした時の値を全て返す
+     * 
+     * @param PEG_IParser
+     * @param string
+     * @return array
+     */
+    static function matchAll(PEG_IParser $parser, $subject)
+    {
+        $context = self::context($subject);
+        $matches = array();
+        while (!$context->eos()) {
+            $result = self::_match($parser, $context);
+            if (!$result instanceof PEG_Failure) {
+                $matches[] = $result;
+            }
+        }
+        
+        return $matches;
+    }
+}
Index: /applications/nimpad/trunk/lib/dao/Wiki.php
===================================================================
--- /applications/nimpad/trunk/lib/dao/Wiki.php	(revision 1344)
+++ /applications/nimpad/trunk/lib/dao/Wiki.php	(revision 1490)
@@ -153,5 +153,5 @@
     {
         $smt = $this->pdo->prepare('
-            UPDATE nim_Wiki
+            UPDATE nim_wiki
             SET status = ? 
             WHERE id = ? 
Index: /applications/nimpad/trunk/lib/HatenaSyntax/Tree/INode.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/Tree/INode.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/Tree/INode.php	(revision 1490)
@@ -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: INode.php 1159 2009-09-06 09:43:29Z anatoo $
+ */
+
+interface HatenaSyntax_Tree_INode
+{
+    function hasValue();
+    function getValue();
+    function hasChildren();
+    
+    // HatenaSyntax_Tree_INodeの配列を返す
+    function getChildren();
+    
+    function getType();
+}
Index: /applications/nimpad/trunk/lib/HatenaSyntax/Tree/Root.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/Tree/Root.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/Tree/Root.php	(revision 1490)
@@ -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: Root.php 1159 2009-09-06 09:43:29Z anatoo $
+ */
+
+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';
+    }
+}
Index: /applications/nimpad/trunk/lib/HatenaSyntax/Tree/Node.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/Tree/Node.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/Tree/Node.php	(revision 1490)
@@ -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: Node.php 1159 2009-09-06 09:43:29Z anatoo $
+ */
+
+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';
+    }
+}
Index: /applications/nimpad/trunk/lib/HatenaSyntax/Tree/Leaf.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/Tree/Leaf.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/Tree/Leaf.php	(revision 1490)
@@ -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: Leaf.php 1159 2009-09-06 09:43:29Z anatoo $
+ */
+
+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';
+    }
+}
Index: /applications/nimpad/trunk/lib/HatenaSyntax/NodeCreater.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/NodeCreater.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/NodeCreater.php	(revision 1490)
@@ -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: NodeCreater.php 658 2009-04-12 07:47:13Z anatoo $
+ */
+
+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);
+    }
+}
Index: /applications/nimpad/trunk/lib/HatenaSyntax/TOCRenderer.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/TOCRenderer.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/TOCRenderer.php	(revision 1490)
@@ -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: TOCRenderer.php 1163 2009-09-07 17:16:34Z anatoo $
+ */
+
+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);
+    }
+
+    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];
+    }
+}
Index: /applications/nimpad/trunk/lib/HatenaSyntax/Util.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/Util.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/Util.php	(revision 1490)
@@ -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: Util.php 1159 2009-09-06 09:43:29Z anatoo $
+ */
+
+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;
+    }
+}
Index: /applications/nimpad/trunk/lib/HatenaSyntax/Tree.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/Tree.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/Tree.php	(revision 1490)
@@ -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: Tree.php 1159 2009-09-06 09:43:29Z anatoo $
+ */
+
+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;
+    }
+}
Index: /applications/nimpad/trunk/lib/HatenaSyntax/Locator.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/Locator.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/Locator.php	(revision 1490)
@@ -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: Locator.php 1159 2009-09-06 09:43:29Z anatoo $
+ */
+
+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));
+    }
+}
Index: /applications/nimpad/trunk/lib/HatenaSyntax/Node.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/Node.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/Node.php	(revision 1490)
@@ -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: Node.php 658 2009-04-12 07:47:13Z anatoo $
+ */
+
+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;
+    }
+}
Index: /applications/nimpad/trunk/lib/HatenaSyntax/Renderer.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/Renderer.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/Renderer.php	(revision 1490)
@@ -0,0 +1,231 @@
+<?php
+/**
+ * @package HatenaSyntax
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Renderer.php 1166 2009-09-07 17:34:11Z anatoo $
+ */
+
+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)
+    {
+        if ($rootnode->getType() !== 'root') throw new InvalidArgumentException();
+        
+        $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[] = '<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);
+    }
+}
Index: /applications/nimpad/trunk/lib/HatenaSyntax/TreeRenderer.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/TreeRenderer.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/TreeRenderer.php	(revision 1490)
@@ -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: TreeRenderer.php 1159 2009-09-06 09:43:29Z anatoo $
+ */
+
+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>';
+    }
+}
Index: /applications/nimpad/trunk/lib/HatenaSyntax/Factory.php
===================================================================
--- /applications/nimpad/trunk/lib/HatenaSyntax/Factory.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/HatenaSyntax/Factory.php	(revision 1490)
@@ -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: Factory.php 1159 2009-09-06 09:43:29Z anatoo $
+ */
+
+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);
+    }
+}
Index: /applications/nimpad/trunk/lib/NimpadAdmin.php
===================================================================
--- /applications/nimpad/trunk/lib/NimpadAdmin.php	(revision 1292)
+++ /applications/nimpad/trunk/lib/NimpadAdmin.php	(revision 1490)
@@ -24,10 +24,16 @@
     
     /**
+     * @var string
+     */
+    public $baseurl;
+    
+    /**
      * ランダムな名前を持つwikiの作成
      *
      * @param string
+     * @param string $baseurl ex. http://hoge.com/
      * @return string ランダムな名前
      */
-    function createNewWiki($text)
+    function createNewWiki($text, $baseurl)
     {
         $this->pdo->beginTransaction();
@@ -35,5 +41,7 @@
             $name = $this->findNewRandomName();
             $wiki_id = $this->daoWiki->create($name, array('home' => 'README'));
-            $this->daoPage->create($wiki_id, 'README', array('html' => $this->textProcessor->render('hatena', $text, 'README'), 'text' => $text));
+            $this->daoPage->create($wiki_id, 'README', 
+                array('html' => $this->textProcessor->render('hatena', $text, 'README', $baseurl . $name . '/'), 
+                      'text' => $text));
             $this->pdo->commit();
         } catch (PDOException $e) {
Index: /applications/nimpad/trunk/lib/PEG/CallbackAction.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/CallbackAction.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/CallbackAction.php	(revision 1490)
@@ -0,0 +1,22 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: CallbackAction.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+class PEG_CallbackAction extends PEG_Action
+{
+    protected $callback;
+    function __construct($callback, PEG_IParser $parser)
+    {
+        if (!is_callable($callback) && !function_exists($callback)) throw new InvalidArgumentException('first argument must be callable');
+        $this->callback = $callback;
+        parent::__construct($parser);
+    }
+    protected function process($result)
+    {
+        return call_user_func($this->callback, $result);
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Choice.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Choice.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Choice.php	(revision 1490)
@@ -0,0 +1,38 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Choice.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+/**
+ * 選択。正規表現でいう"|"。
+ *
+ */
+class PEG_Choice implements PEG_IParser
+{
+    protected $parsers = array();
+    
+    function __construct(Array $parsers = array())
+    {
+        foreach ($parsers as $parser) $this->with($parser);
+    }
+
+    protected function with(PEG_IParser $p)
+    {
+        $this->parsers[] = $p;
+    }
+    
+    function parse(PEG_IContext $c)
+    {
+        $offset = $c->tell();
+        foreach ($this->parsers as $p) {
+            $result = $p->parse($c);
+            
+            if ($result instanceof PEG_Failure) $c->seek($offset);
+            else return $result;
+        }
+        return PEG::failure();
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/IContext.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/IContext.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/IContext.php	(revision 1490)
@@ -0,0 +1,69 @@
+<?php
+/**
+ * PEG_IParserが必要とするコンテキスト
+ * 
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: IContext.php 822 2009-05-12 16:27:55Z anatoo $
+ */
+
+interface PEG_IContext
+{
+    /**
+     * 対象の現在の位置を得る。
+     * 
+     * @return int
+     */
+    function tell();
+    
+    /**
+     * 対象の現在の位置を設定する。
+     *
+     * @param int $i
+     */
+    function seek($i);
+    
+    /**
+     * 対象の一部を$i分返す。その際に$iだけ現在位置も変更する。
+     *
+     * @param int $i
+     * @return string
+     */
+    function read($i);
+    
+    /**
+     * 対象の要素を一つ返す。その際に現在位置も変更する。
+     */
+    function readElement();
+    
+    /**
+     * 読み込むべきものが無い場合trueを返す。
+     * @return bool
+     */
+    function eos();
+    
+    /**
+     * コンテキストが持つ対象全体を返す
+     * 実装クラスは例外を投げることでこれを拒否できる
+     * @return ?
+     */
+    function get();
+    
+    /**
+     * このメソッドの実装にはPEG_Cacheクラスの使用を推奨する
+     * @param PEG_IParser 
+     * @param int 
+     * @param ? 
+     */
+    function save(PEG_IParser $parser, $start, $end, $val);
+    
+    /**
+     * このメソッドの実装にはPEG_Cacheクラスの使用を推奨する
+     * array(hit, array(end, val))を返す
+     * @param PEG_IParser
+     * @param int
+     * @return array
+     */
+    function cache(PEG_IParser $parser);
+}
Index: /applications/nimpad/trunk/lib/PEG/Not.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Not.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Not.php	(revision 1490)
@@ -0,0 +1,31 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Not.php 987 2009-06-29 19:50:54Z anatoo $
+ */
+
+class PEG_Not implements PEG_IParser
+{
+    protected $parser;
+    function __construct(PEG_IParser $p)
+    {
+        $this->parser = $p;
+    }
+    function parse(PEG_IContext $context)
+    {
+        if ($context->eos()) return PEG::failure();
+        $offset = $context->tell();
+
+        $result = $this->parser->parse($context);
+        if ($result instanceof PEG_Failure) {
+            $i = $context->tell() - $offset;
+            $context->seek($offset);
+            $ret =  $context->read($i);
+            $context->seek($offset);
+            return $ret;
+        }
+        return PEG::failure();
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/And.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/And.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/And.php	(revision 1490)
@@ -0,0 +1,34 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: And.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+class PEG_And implements PEG_IParser
+{
+    protected $arr = array();
+    
+    function __construct(Array $arr)
+    {
+        foreach ($arr as $p) $this->with($p);
+    }
+    
+    protected function with(PEG_IParser $p)
+    {
+        $this->arr[] = $p;
+    }
+    
+    function parse(PEG_IContext $c)
+    {
+        $arr = $this->arr;
+        if (!$arr) return PEG::failure();
+        for ($i = 0; $i < count($arr) - 1 ; $i++) {
+            $offset = $c->tell();
+            if ($arr[$i]->parse($c) instanceof PEG_Failure) return PEG::failure();
+            $c->seek($offset);
+        }
+        return $arr[$i]->parse($c);
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Cache.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Cache.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Cache.php	(revision 1490)
@@ -0,0 +1,28 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Cache.php 785 2009-05-03 09:02:28Z anatoo $
+ */
+
+class PEG_Cache
+{
+    protected $data = array();
+    
+    function save(PEG_IParser $parser, $start, $end, $val)
+    {
+        $this->data[$this->genkey($parser, $start)] = array($end, $val);
+    }
+    
+    protected function genkey($parser, $start)
+    {
+        return spl_object_hash($parser) . ':' . $start;
+    }
+    
+    function cache(PEG_IParser $parser, $start)
+    {
+        $key = $this->genkey($parser, $start);
+        return isset($this->data[$key]) ? array(true, $this->data[$key]) : array(false, false);
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Curry.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Curry.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Curry.php	(revision 1490)
@@ -0,0 +1,31 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Curry.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+class PEG_Curry
+{
+    protected $args, $callback;
+    
+    protected function __construct($callback, Array $args)
+    {
+        $this->callback = $callback;
+        $this->args = $args;
+    }
+    function invoke()
+    {
+        $args = func_get_args();
+        return call_user_func_array($this->callback, array_merge($this->args, $args));
+    }
+    
+    static function make($callback)
+    {
+        $args = func_get_args();
+        array_shift($args);
+        $curry = new self($callback, $args);
+        return array($curry, 'invoke');
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Many.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Many.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Many.php	(revision 1490)
@@ -0,0 +1,33 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Many.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+class PEG_Many implements PEG_IParser
+{
+    protected $parser;
+    function __construct(PEG_IParser $p)
+    {
+        $this->parser = $p;
+    }
+    function parse(PEG_IContext $context)
+    {
+        $ret = array();
+        do {
+            $offset = $context->tell();
+            $result = $this->parser->parse($context);
+            
+            if ($result instanceof PEG_Failure) {
+                $context->seek($offset);
+                return $ret;
+            }
+            elseif (!is_null($result)) {
+                $ret[] = $result;
+            }
+        } while (!$context->eos());
+        return $ret;
+    }
+}   
Index: /applications/nimpad/trunk/lib/PEG/IParser.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/IParser.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/IParser.php	(revision 1490)
@@ -0,0 +1,19 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: IParser.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+interface PEG_IParser
+{
+    /**
+     * パースに失敗した場合はPEG_Failureを返すこと。
+     * 成功した場合はなんらかの値を返すこと。
+     * 
+     * @param PEG_IContext $c
+     * @return mixed
+     */
+    function parse(PEG_IContext $c);
+}
Index: /applications/nimpad/trunk/lib/PEG/Memoize.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Memoize.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Memoize.php	(revision 1490)
@@ -0,0 +1,31 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Memoize.php 782 2009-05-02 18:35:18Z anatoo $
+ */
+
+class PEG_Memoize implements PEG_IParser
+{
+    protected $parser;
+    
+    function __construct(PEG_IParser $p)
+    {
+        $this->parser = $p;
+    }
+    
+    function parse(PEG_IContext $context)
+    {
+        list($hit, list($end, $val)) = $context->cache($this);
+        if ($hit) {
+            $context->seek($end);
+            return $val;
+        }
+        $start = $context->tell();
+        $val = $this->parser->parse($context);
+        $end = $context->tell();
+        $context->save($this, $start, $end, $val);
+        return $val;
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Sequence.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Sequence.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Sequence.php	(revision 1490)
@@ -0,0 +1,32 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Sequence.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+class PEG_Sequence implements PEG_IParser
+{
+    protected $parsers = array();
+    function __construct(Array $parsers = array())
+    {
+        foreach ($parsers as $p) $this->with($p);
+    }
+    protected function with(PEG_IParser $p)
+    {
+        $this->parsers[] = $p;
+        return $this;
+    }
+    function parse(PEG_IContext $context)
+    {
+        $ret = array();
+        foreach ($this->parsers as $parser) {
+            $offset = $context->tell();
+            $result = $parser->parse($context);
+            if ($result instanceof PEG_Failure) return $result;
+            elseif ($result !== null) $ret[] = $result;
+        }
+        return $ret;
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/StringContext.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/StringContext.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/StringContext.php	(revision 1490)
@@ -0,0 +1,81 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: StringContext.php 822 2009-05-12 16:27:55Z anatoo $
+ */
+
+class PEG_StringContext implements PEG_IContext
+{
+    protected $str, $i = 0, $len, $cache;
+    
+    /**
+     * 与えられた文字列とその位置を保持するPEG_Contextインスタンスを生成する。
+     *
+     * @param string $s 文字列
+     */
+    function __construct($str) { 
+        $this->str = $str; 
+        $this->len = strlen($str);
+        $this->cache = new PEG_Cache;
+    }
+
+    /**
+     * @param int $i
+     * @return string
+     */
+    function read($i)
+    {
+        if ($this->eos() && $i > 0) return false;
+        $this->i += $i;
+        return substr($this->str, $this->i - $i, $i);
+    }
+    
+    function readElement()
+    {
+        return $this->read(1);
+    }
+    
+    /**
+     * @param int $i
+     * @return bool
+     */
+    function seek($i)
+    {
+        if ($this->len < $i) return false;
+        $this->i = $i;
+        return true;
+    }
+    
+    /**
+     * @return int
+     */
+    function tell()
+    {
+        return $this->i;
+    }
+
+    /**
+     * @return bool
+     */
+    function eos()
+    {
+        return $this->len <= $this->i;
+    }
+
+    function get()
+    {
+         return $this->str;   
+    }
+    
+    function save(PEG_IParser $parser, $start, $end, $val)
+    {
+        $this->cache->save($parser, $start, $end, $val);
+    }
+    
+    function cache(PEG_IParser $parser)
+    {
+        return $this->cache->cache($parser, $this->tell());
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Ref.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Ref.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Ref.php	(revision 1490)
@@ -0,0 +1,33 @@
+<?php
+/**
+ * PEG_Refクラスはパーサ同士がお互いに依存しているときに使われる
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Ref.php 959 2009-06-14 16:57:43Z anatoo $
+ */
+
+class PEG_Ref implements PEG_IParser
+{
+    protected $parser;
+    
+    function __construct(&$parser)
+    {
+        $this->parser = &$parser;
+    }
+    
+    function parse(PEG_IContext $c)
+    {
+        return $this->parser->parse($c);
+    }
+    
+    function &getRef()
+    {
+        return $this->parser;
+    }
+
+    function is(PEG_IParser $p)
+    {
+        $this->parser = $p;
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Util.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Util.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Util.php	(revision 1490)
@@ -0,0 +1,64 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Util.php 989 2009-06-29 23:52:57Z anatoo $
+ */
+
+class PEG_Util
+{
+    static function concat(Array $arr)
+    {
+        $ret = array();
+        foreach ($arr as $elt) foreach($elt as $val) $ret[] = $val;
+        return $ret;
+    }
+    
+    static function count(Array $result)
+    {
+        return count($result);
+    }
+    
+    static function drop($result)
+    {
+        return null;
+    }
+    
+    static function cons($result)
+    {
+        array_unshift($result[1], $result[0]);
+        return $result[1];
+    }
+    
+    static function flatten(Array $result)
+    {
+        $ret = array();
+        foreach ($result as $elt) {
+            if (is_array($elt)) $ret = array_merge($ret, self::flatten($elt));
+            else $ret[] = $elt;
+        }
+        return $ret;
+    }
+    
+    static function at($key, $result)
+    {
+        return $result[$key];
+    }
+    
+    static function create($klass, $result)
+    {
+        return new $klass($result);
+    }
+    
+    static function join($glue, Array $result)
+    {
+        $result = self::flatten($result);
+        return join($glue, $result);
+    }
+    
+    static function tail(Array $result)
+    {
+        return end($result);
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Action.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Action.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Action.php	(revision 1490)
@@ -0,0 +1,22 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Action.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+abstract class PEG_Action implements PEG_IParser
+{
+    protected $parser;
+    function __construct(PEG_IParser $p)
+    {
+        $this->parser = $p;
+    }
+    function parse(PEG_IContext $context)
+    {
+        $result = $this->parser->parse($context);
+        return $result instanceof PEG_Failure ? $result : $this->process($result);
+    }
+    abstract protected function process($result);
+}
Index: /applications/nimpad/trunk/lib/PEG/Char.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Char.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Char.php	(revision 1490)
@@ -0,0 +1,23 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Char.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+class PEG_Char implements PEG_IParser
+{
+    protected $dict = array();
+    function __construct($str)
+    {
+        foreach (str_split($str) as $c) {
+            $this->dict[$c] = true;
+        }
+    }
+    function parse(PEG_IContext $context)
+    {
+        if (isset($this->dict[$char = $context->read(1)])) return $char;
+        return PEG::failure();
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Token.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Token.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Token.php	(revision 1490)
@@ -0,0 +1,28 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Token.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+class PEG_Token implements PEG_IParser
+{
+    protected $str;
+    protected function __construct($str)
+    {
+        $this->str = $str;
+    }
+    function parse(PEG_IContext $c)
+    {
+        if ($c->read(strlen($this->str)) === $this->str)
+            return $this->str;
+        else 
+            return PEG::failure();
+    }
+    static function get($token)
+    {
+        static $dict = array();
+        return isset($dict[$token]) ? $dict[$token] : $dict[$token] = new self($token);
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Anything.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Anything.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Anything.php	(revision 1490)
@@ -0,0 +1,21 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Anything.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+/**
+ * どのような文字にでもヒットするパーサ
+ *
+ */
+class PEG_Anything implements PEG_IParser
+{
+    function __construct() { }
+    function parse(PEG_IContext $context)
+    {
+        if ($context->eos()) return PEG_Failure;
+        return $context->read(1);
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/ArrayContext.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/ArrayContext.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/ArrayContext.php	(revision 1490)
@@ -0,0 +1,89 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: ArrayContext.php 822 2009-05-12 16:27:55Z anatoo $
+ */
+
+/**
+ * PEG_IContextの実装クラス
+ * 配列をパースするためのもの
+ */
+class PEG_ArrayContext implements PEG_IContext
+{
+    protected $arr, $i = 0, $len, $cache;
+    
+    /**
+     *
+     * @param Array $arr 配列
+     */
+    function __construct(Array $arr) { 
+        $this->arr = array_values($arr); 
+        $this->len = count($arr);
+        $this->cache = new PEG_Cache;
+    }
+
+    /**
+     * @param int $i
+     * @return Array
+     */
+    function read($i)
+    {
+        if ($this->eos() && $i > 0) return false;
+        $this->i += $i;
+        return array_slice($this->arr, $this->i - $i, $i);
+    }
+    
+    function readElement()
+    {
+        list($elt) = $this->read(1);
+        return $elt;
+    }
+    
+    /**
+     * @param int $i
+     * @return bool
+     */
+    function seek($i)
+    {
+        if ($this->len < $i) return false;
+        $this->i = $i;
+        return true;
+    }
+    
+    /**
+     * @return int
+     */
+    function tell()
+    {
+        return $this->i;
+    }
+
+    /**
+     * @return bool
+     */
+    function eos()
+    {
+        return $this->len <= $this->i;
+    }
+    
+    /**
+     * @return array
+     */
+    function get()
+    {
+        return $this->arr;
+    }
+
+    
+    function save(PEG_IParser $parser, $start, $end, $val)
+    {
+        $this->cache->save($parser, $start, $end, $val);
+    }
+    
+    function cache(PEG_IParser $parser)
+    {
+        return $this->cache->cache($parser, $this->tell());
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Optional.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Optional.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Optional.php	(revision 1490)
@@ -0,0 +1,26 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Optional.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+class PEG_Optional implements PEG_IParser
+{
+    protected $parser;
+    function __construct(PEG_IParser $p)
+    {
+        $this->parser = $p;
+    }
+    function parse(PEG_IContext $context)
+    {
+        $offset = $context->tell();
+        $result = $this->parser->parse($context);
+        if ($result instanceof PEG_Failure) {
+            $context->seek($offset);
+            return false;
+        }
+        return $result;
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/EOS.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/EOS.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/EOS.php	(revision 1490)
@@ -0,0 +1,20 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: EOS.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+/**
+ * 文字列の終端にヒットするパーサ。
+ *
+ */
+class PEG_EOS implements PEG_IParser
+{
+    function parse(PEG_IContext $c)
+    {
+        if ($c->eos()) return false;
+        return PEG::failure();
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Lookahead.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Lookahead.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Lookahead.php	(revision 1490)
@@ -0,0 +1,23 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Lookahead.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+class PEG_Lookahead implements PEG_IParser
+{
+    protected $parser;
+    function __construct(PEG_IParser $p)
+    {
+        $this->parser = $p;
+    }
+    function parse(PEG_IContext $context)
+    {
+        $offset = $context->tell();
+        $result = $this->parser->parse($context);
+        $context->seek($offset);
+        return $result;
+    }
+}
Index: /applications/nimpad/trunk/lib/PEG/Failure.php
===================================================================
--- /applications/nimpad/trunk/lib/PEG/Failure.php	(revision 1490)
+++ /applications/nimpad/trunk/lib/PEG/Failure.php	(revision 1490)
@@ -0,0 +1,18 @@
+<?php
+/**
+ * @package PEG
+ * @author anatoo<anatoo@nequal.jp>
+ * @license http://www.opensource.org/licenses/mit-license.php MIT License
+ * @version $Id: Failure.php 655 2009-04-12 07:26:42Z anatoo $
+ */
+
+class PEG_Failure
+{
+    private function __construct(){ } 
+    
+    static function it()
+    {
+        static $o = null;
+        return $o ? $o : $o = new self;
+    }
+}
