powered by nequal
Home » WikiHub » Timeline » 1354

Changeset 1354 -- 2009-10-20 01:34:04

Comment
[Package Release] WikiHub

Diffs

WikiHub/tags/release-1.0.0-20091020013404/WikiHub/Exception.php

@@ -0,0 +1,9 @@
+<?php
+/**
+ *
+ *
+ */
+
+class WikiHub_Exception extends Exception
+{
+}
属性に変更があったパス: WikiHub/tags/release-1.0.0-20091020013404/WikiHub/Exception.php
___________________________________________________________________
名前: svn:keywords
+ Id
名前: svn:eol-style
+ native

WikiHub/tags/release-1.0.0-20091020013404/WikiHub.php

@@ -0,0 +1,105 @@
+<?php
+
+require_once 'WikiHub/Exception.php';
+require_once 'HatenaSyntax.php';
+
+class WikiHub {
+
+    const VERSION = "1.0.0";
+    const URL     = "http://openpear.org/package/WikiHub";
+
+    protected $_dir;
+
+    public $browser = 'w3m';
+    public $browser_option = array(
+        'w3m' => '-T "text/html"',
+    );
+
+    public function __construct($dir, $browser = "w3m")
+    {
+        $this->_dir = $dir;
+
+        exec('which ' . $browser, $output, $status);
+        if ($status) {
+            throw new WikiHub_Exception("$browser does not installed!\n");
+        }
+
+        $this->browser = $browser;
+        $this->browser_cmd = trim($output[0]);
+    }
+
+    protected function getPath($path)
+    {
+        $path = ltrim($path, "/");
+        return $this->_dir . "/" . $path;
+    }
+
+    public static function getHeader()
+    {
+        return '<h1>Wikihub :: preview</h1>
+            <p> (This is only a preview, it is little different from web view. Inner link doe\'s not work.)</p><hr />';
+    }
+
+    public static function getFooter()
+    {
+        return '<hr /><em> -- powererd by nequal.</em>';
+    }
+
+    public static function runCommand($args)
+    {
+        $o = new self(getcwd());
+        $path = $o->getPath($args);
+        if (!file_exists($path)) {
+            throw new WikiHub_Exception('file is not exists.');
+        }
+
+        $txt = file_get_contents($path);
+
+        $html = self::getHeader();
+        $html .=  HatenaSyntax::render($o->getTocIfSectionsCount(3, $txt) . $txt, array(
+                'headerlevel' => 3,
+                'htmlescape' => true,
+                'keywordlinkhandler' => array($o, 'keywordLinkHandler'),
+            )
+        );
+        $html .= self::getFooter();
+
+        $w = popen(join(" ", array($o->browser_cmd, $o->browser_option[$o->browser],)), "w");
+        fwrite($w, $html);
+        pclose($w);
+    }
+
+    public function getTocIfSectionsCount($count, $txt)
+    {
+        $c = preg_match_all('!\n\*+?!', "\n" . $txt, $m);
+        if ($c >= $count) {
+            return "[:contents]\n";
+        }
+        return "";
+    }
+
+    public function keywordlinkhandler($path)
+    {
+        return $path;
+    }
+
+    public static function getUsage()
+    {
+        $version = self::VERSION;
+        $url     = self::URL;
+
+        return <<<EEE
+usage: whprev <path>
+
+example:
+    % whprev hoge.html
+    % whprev hoge/fuga.html
+
+whprev - WikiHub preview command.
+    version     $version
+    url         $url
+    author      Sotaro Karasawa
+
+EEE;
+    }
+}
属性に変更があったパス: WikiHub/tags/release-1.0.0-20091020013404/WikiHub.php
___________________________________________________________________
名前: svn:keywords
+ Id
名前: svn:eol-style
+ native

WikiHub/tags/release-1.0.0-20091020013404/bin/whprev

@@ -0,0 +1,30 @@
+#!@php_bin@
+<?php
+/**
+#!/usr/bin/env php
+ *  whprev
+ *
+ *  @package    WikiHub
+ *  @author     sotarok
+ *  @license    The MIT License
+ *  @id         $Id$
+ */
+
+require_once 'WikiHub.php';
+
+try {
+    if (!isset($argv)) {
+        throw new WikiHub_Exception('variable $argv is not setted. please check your setting of "register_argc_argv".');
+    }
+    array_shift($argv);
+
+    if (empty($argv)) {
+        echo WikiHub::getUsage();
+        throw new WikiHub_Exception('--');
+    }
+
+    WikiHub::runCommand(array_shift($argv));
+}
+catch (WikiHub_Exception $e) {
+    echo "Error: ", $e->getMessage(), PHP_EOL;
+}