re-add files with new case
authorChuck Hagenbuch <chuck@horde.org>
Sat, 2 Oct 2010 22:38:42 +0000 (18:38 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Sat, 2 Oct 2010 22:39:07 +0000 (18:39 -0400)
framework/Core/lib/Horde/Core/Mime/Viewer/Syntaxhighlighter.php [new file with mode: 0644]
framework/Mime_Viewer/lib/Horde/Mime/Viewer/Syntaxhighlighter.php [new file with mode: 0644]

diff --git a/framework/Core/lib/Horde/Core/Mime/Viewer/Syntaxhighlighter.php b/framework/Core/lib/Horde/Core/Mime/Viewer/Syntaxhighlighter.php
new file mode 100644 (file)
index 0000000..e290899
--- /dev/null
@@ -0,0 +1,59 @@
+<?php
+/**
+ * The Horde_Mime_Viewer_Syntaxhighlighter class renders source code appropriate
+ * for highlighting with http://alexgorbatchev.com/SyntaxHighlighter/.
+ *
+ * Copyright 2003-2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @category Horde
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package  Mime_Viewer
+ */
+class Horde_Core_Mime_Viewer_Syntaxhighlighter extends Horde_Mime_Viewer_Syntaxhighlighter
+{
+    protected static $_shLoaded = false;
+    protected static $_shBrushes = array();
+
+    /**
+     * Return the rendered inline version of the Horde_Mime_Part object.
+     *
+     * @return array  See parent::render().
+     */
+    protected function _renderInline()
+    {
+        /* Determine the language and brush from the mime type. */
+        $mimeType = $this->_mimepart->getType();
+        $language = $this->_mimeTypeToLanguage($mimeType);
+        $brush = $this->_languageToBrush($language);
+
+        if (!self::$_shLoaded) {
+            Horde::addScriptFile('syntaxhighlighter/scripts/shCore.js', 'horde', true);
+            Horde::addInlineScript(array(
+                'SyntaxHighlighter.defaults[\'toolbar\'] = false',
+                'SyntaxHighlighter.all()',
+            ), 'dom');
+            self::$_shLoaded = true;
+
+            $sh_js_fs = $this->getConfigParam('registry')->get('jsfs', 'horde') . '/syntaxhighlighter/styles/';
+            $sh_js_uri = Horde::url($this->getConfigParam('registry')->get('jsuri', 'horde'), false, -1) . '/syntaxhighlighter/styles/';
+            Horde_Themes::includeStylesheetFiles(array('additional' => array(
+                array('f' => $sh_js_fs . 'shCoreEclipse.css', 'u' => $sh_js_uri . 'shCoreEclipse.css'),
+                array('f' => $sh_js_fs . 'shThemeEclipse.css', 'u' => $sh_js_uri . 'shThemeEclipse.css'),
+            )));
+        }
+        if (empty(self::$_shBrushes[$brush])) {
+            Horde::addScriptFile('syntaxhighlighter/scripts/shBrush' . $brush . '.js', 'horde', true);
+            self::$_shBrushes[$brush] = true;
+        }
+
+        $results = '<pre class="brush: ' . $language . '; toolbar: false;">' . htmlspecialchars($this->_mimepart->getContents(), ENT_QUOTES, $this->getConfigParam('charset')) . '</pre>';
+        return $this->_renderReturn(
+            $results,
+            'text/html; charset=' . $this->getConfigParam('charset')
+        );
+    }
+}
diff --git a/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Syntaxhighlighter.php b/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Syntaxhighlighter.php
new file mode 100644 (file)
index 0000000..6a75dc3
--- /dev/null
@@ -0,0 +1,199 @@
+<?php
+/**
+ * The Horde_Mime_Viewer_Syntaxhighlighter class renders source code appropriate
+ * for highlighting with http://alexgorbatchev.com/SyntaxHighlighter/.
+ *
+ * Copyright 2003-2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @category Horde
+ * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package  Mime_Viewer
+ */
+class Horde_Mime_Viewer_Syntaxhighlighter extends Horde_Mime_Viewer_Base
+{
+    /**
+     * This driver's display capabilities.
+     *
+     * @var array
+     */
+    protected $_capability = array(
+        'full' => true,
+        'info' => false,
+        'inline' => true,
+        'raw' => false,
+    );
+
+    /**
+     * Return the full rendered version of the Horde_Mime_Part object.
+     *
+     * @return array  See parent::render().
+     */
+    protected function _render()
+    {
+        return $this->_renderFullReturn($this->_renderInline());
+    }
+
+    /**
+     * Return the rendered inline version of the Horde_Mime_Part object.
+     *
+     * @return array  See parent::render().
+     */
+    protected function _renderInline()
+    {
+        /* Determine the language and brush from the mime type. */
+        $mimeType = $this->_mimepart->getType();
+        $language = $this->_mimeTypeToLanguage($mimeType);
+        $brush = $this->_languageToBrush($language);
+
+        $results = '<pre class="brush: ' . $language . '">' . htmlspecialchars($this->_mimepart->getContents(), ENT_QUOTES, $this->getConfigParam('charset')) . '</pre>';
+        return $this->_renderReturn(
+            $results,
+            'text/html; charset=' . $this->getConfigParam('charset')
+        );
+    }
+
+    protected function _getLanguageOptions($language)
+    {
+        if ($language == 'php') {
+            return 'html-script: true';
+        }
+    }
+
+    /**
+     * Attempts to determine what mode to use for the source-highlight
+     * program from a MIME type.
+     *
+     * @param string $type  The MIME type.
+     *
+     * @return string  The mode to use.
+     */
+    protected function _mimeTypeToLanguage($type)
+    {
+        $type = str_replace('x-unknown', 'x-extension', $type);
+
+        switch ($type) {
+        case 'application/x-javascript':
+        case 'x-extension/javascript':
+        case 'x-extension/js':
+            return 'js';
+
+        case 'application/x-perl':
+        case 'x-extension/pl':
+            return 'perl';
+
+        case 'application/x-php':
+        case 'x-extension/php':
+        case 'x-extension/phps':
+        case 'x-extension/php3s':
+        case 'application/x-httpd-php':
+        case 'application/x-httpd-php3':
+        case 'application/x-httpd-phps':
+            return 'php';
+
+        case 'application/x-python':
+            return 'python';
+
+        case 'application/x-ruby':
+            return 'ruby';
+
+        case 'application/x-sh':
+        case 'application/x-shellscript':
+        case 'x-extension/bash':
+        case 'x-extension/sh':
+            return 'bash';
+
+        case 'application/xml':
+        case 'text/xml':
+        case 'text/xslt':
+        case 'text/html':
+        case 'text/xhtml':
+        case 'application/xhtml':
+            return 'xml';
+
+        case 'text/css':
+        case 'x-extension/css':
+            return 'css';
+
+        case 'text/diff':
+        case 'text/x-diff':
+        case 'text/x-patch':
+            return 'diff';
+
+        case 'text/cpp':
+        case 'text/x-c++':
+        case 'text/x-c++src':
+        case 'text/x-c++hdr':
+        case 'text/x-c':
+        case 'text/x-chdr':
+        case 'text/x-csrc':
+            return 'cpp';
+
+        case 'text/x-java':
+            return 'java';
+
+        case 'text/x-pascal':
+            return 'pascal';
+
+        case 'text/x-sql':
+            return 'sql';
+
+        case 'x-extension/bat':
+            return 'batch';
+
+        case 'x-extension/cs':
+            return 'csharp';
+
+        case 'x-extension/vb':
+        case 'x-extension/vba':
+            return 'vb';
+
+        default:
+            return 'plain';
+        }
+    }
+
+    protected function _languageToBrush($language)
+    {
+        switch ($language) {
+        case 'php':
+            return 'Php';
+
+        case 'xml':
+        case 'html':
+        case 'xhtml':
+        case 'xslt':
+            return 'Xml';
+
+        case 'bash':
+        case 'shell':
+            return 'Bash';
+
+        case 'diff':
+        case 'patch':
+        case 'pas':
+            return 'Diff';
+
+        case 'css':
+            return 'Css';
+
+        case 'c':
+        case 'cpp':
+            return 'Cpp';
+
+        case 'java':
+            return 'Java';
+
+        case 'js':
+        case 'jscript':
+        case 'javascript':
+            return 'JScript';
+
+        default:
+            return 'Plain';
+        }
+    }
+}