From 3c2ef042ab5879f74472eda10c12941e8e8035ca Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Sat, 2 Oct 2010 17:05:56 -0400 Subject: [PATCH] Add SyntaxHighlighter viewer --- .../lib/Horde/Mime/Viewer/SyntaxHighlighter.php | 222 +++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 framework/Mime_Viewer/lib/Horde/Mime/Viewer/SyntaxHighlighter.php 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 index 000000000..ff73b2e71 --- /dev/null +++ b/framework/Mime_Viewer/lib/Horde/Mime/Viewer/SyntaxHighlighter.php @@ -0,0 +1,222 @@ + + * @category Horde + * @license http://www.fsf.org/copyleft/lgpl.html LGPL + * @package Mime_Viewer + */ +class Horde_Mime_Viewer_SyntaxHighlighter extends Horde_Mime_Viewer_Base +{ + protected $_shLoaded = false; + protected $_shBrushes = array(); + + /** + * 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(); + if (strpos($mimeType, '/') === false) { + $mimeType = "application/x-$mimeType"; + } + $language = $this->_mimeTypeToLanguage($mimeType); + $brush = $this->_languageToBrush($language); + + if (!$this->_shLoaded) { + Horde::addScriptFile('syntaxhighlighter/scripts/shCore.js', 'horde', true); + Horde::addInlineScript(array( + 'SyntaxHighlighter.defaults[\'toolbar\'] = false;', + 'SyntaxHighlighter.all();', + ), 'dom'); + $this->_shLoaded = true; + + $sh_js_fs = $GLOBALS['injector']->getInstance('Horde_Registry')->get('jsfs', 'horde') . '/syntaxhighlighter/styles/'; + $sh_js_uri = Horde::url($GLOBALS['injector']->getInstance('Horde_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($this->_shBrushes[$brush])) { + Horde::addScriptFile('syntaxhighlighter/scripts/shBrush' . $brush . '.js', 'horde', true); + $this->_shBrushes[$brush] = true; + } + + $results = '
' . htmlspecialchars($this->_mimepart->getContents(), ENT_QUOTES, $this->getConfigParam('charset')) . '
'; + 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) + { + switch ($type) { + case 'application/x-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'; + } + } +} -- 2.11.0