rST rendering is possible now
authorChristian Weiske <cweiske@cweiske.de>
Tue, 3 Apr 2012 19:52:06 +0000 (21:52 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 3 Apr 2012 19:52:06 +0000 (21:52 +0200)
README.rst
data/config.default.php
src/phorkie/File.php
src/phorkie/Renderer/Geshi.php [new file with mode: 0644]
src/phorkie/Renderer/ReStructuredText.php [new file with mode: 0644]

index 4c32a3c..353664c 100644 (file)
@@ -47,14 +47,20 @@ Install geshi
 
 TODO
 ====
-- edit
 - search
 - OpenID-Login to get username+email as authorship information
 - sidebar: history
 - image upload
-- rst rendering
 - document how to keep disk usage low (block size)
 - comments
 - when 2 people edit, merge changes
 - diff changes
 - configurable highlights
+
+
+Features
+========
+- every paste is a git repository
+- rST rendering
+- paste editing
+- multiple files
index 24a133c..d55721d 100644 (file)
@@ -45,6 +45,12 @@ $GLOBALS['phorkie']['languages'] = array(
         'mime'  => 'text/x-php',
         'geshi' => 'php'
     ),
+    'rst' => array(
+        'title' => 'reStructuredText',
+        'mime'  => 'text/x-rst',
+        'geshi' => 'rst',
+        'renderer' => '\\phorkie\\Renderer_ReStructuredText',
+    ),
     'sh' => array(
         'title' => 'Shell script (Bash)',
         'mime'  => 'text/x-shellscript',
index bad21e6..87c3851 100644 (file)
@@ -60,15 +60,14 @@ class File
 
     public function getHighlightedContent()
     {
-        /**
-         * Yes, geshi needs to be in your include path
-         * We use the mediawiki geshi extension package.
-         */
-        require_once 'MediaWiki/geshi/geshi/geshi.php';
-        $geshi = new \GeSHi($this->getContent(), $this->getGeshiType());
-        $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
-        $geshi->set_header_type(GESHI_HEADER_DIV);
-        return $geshi->parse_code();
+        $ext = $this->getExt();
+        if (isset($GLOBALS['phorkie']['languages'][$ext]['renderer'])) {
+            $class = $GLOBALS['phorkie']['languages'][$ext]['renderer'];
+        } else {
+            $class = '\\phorkie\\Renderer_Geshi';
+        }
+        $rend = new $class();
+        return $rend->toHtml($this);
     }
 
     /**
@@ -88,21 +87,6 @@ class File
         throw new Exception('Unknown type');
     }
 
-    /**
-     * Returns the type of the file, as used by Geshi
-     *
-     * @return string
-     */
-    public function getGeshiType()
-    {
-        $ext = $this->getExt();
-        if (isset($GLOBALS['phorkie']['languages'][$ext]['geshi'])) {
-            $ext = $GLOBALS['phorkie']['languages'][$ext]['geshi'];
-        }
-
-        return $ext;
-    }
-
     public function getMimeType()
     {
         $ext = $this->getExt();
diff --git a/src/phorkie/Renderer/Geshi.php b/src/phorkie/Renderer/Geshi.php
new file mode 100644 (file)
index 0000000..1a79e16
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+namespace phorkie;
+
+class Renderer_Geshi
+{
+    /**
+     * Converts the code to HTML
+     *
+     * @param File $file File to render
+     *
+     * @return string HTML
+     */
+    public function toHtml(File $file)
+    {
+        /**
+         * Yes, geshi needs to be in your include path
+         * We use the mediawiki geshi extension package.
+         */
+        require_once 'MediaWiki/geshi/geshi/geshi.php';
+        $geshi = new \GeSHi($file->getContent(), $this->getType($file));
+        $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
+        $geshi->set_header_type(GESHI_HEADER_DIV);
+        return $geshi->parse_code();
+    }
+
+    /**
+     * Returns the type of the file, as used by Geshi
+     *
+     * @return string
+     */
+    public function getType($file)
+    {
+        $ext = $file->getExt();
+        if (isset($GLOBALS['phorkie']['languages'][$ext]['geshi'])) {
+            $ext = $GLOBALS['phorkie']['languages'][$ext]['geshi'];
+        }
+
+        return $ext;
+    }
+
+}
+
+?>
diff --git a/src/phorkie/Renderer/ReStructuredText.php b/src/phorkie/Renderer/ReStructuredText.php
new file mode 100644 (file)
index 0000000..136de16
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+namespace phorkie;
+
+/**
+ * Requires cli program "rst2html" (python docutils) to be installed
+ */
+class Renderer_ReStructuredText
+{
+    /**
+     * Converts the rST to HTML
+     *
+     * @param File $file File to render
+     *
+     * @return string HTML
+     */
+    public function toHtml(File $file)
+    {
+        $descriptorspec = array(
+            0 => array('pipe', 'r'),//stdin
+            1 => array('pipe', 'w'),//stdout
+            2 => array('pipe', 'w') //stderr
+        );
+        $process = proc_open('rst2html', $descriptorspec, $pipes);
+        if (!is_resource($process)) {
+            //FIXME: fallback to geshi
+            return $file->getContent();
+        }
+
+        fwrite($pipes[0], $file->getContent());
+        fclose($pipes[0]);
+
+        $html = stream_get_contents($pipes[1]);
+        fclose($pipes[1]);
+
+        $errors = stream_get_contents($pipes[2]);
+        fclose($pipes[2]);
+
+        $retval = proc_close($process);
+
+        //cheap extraction of the rst html body
+        $html = substr($html, strpos($html, '<body>') + 6);
+        $html = substr($html, 0, strpos($html, '</body>'));
+
+        if ($retval != 0) {
+            $html = '<div class="alert">'
+                . 'rst2html encountered some error; return value ' . $retval . '<br/>'
+                . 'Error message:' . $errors
+                . '</div>'
+                . $html;
+        }
+
+        return $html;
+    }
+}
+
+?>