From d4866cabd3860c9bf00ad520267b935f731eef4d Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Sat, 2 Oct 2010 17:03:29 -0400 Subject: [PATCH] Replace set of existing regex or external-binary based highlighters with SyntaxHighlighter --- .../Mime_Viewer/lib/Horde/Mime/Viewer/Css.php | 119 ------------- .../Mime_Viewer/lib/Horde/Mime/Viewer/Php.php | 59 ------ .../Mime_Viewer/lib/Horde/Mime/Viewer/Source.php | 34 ---- .../lib/Horde/Mime/Viewer/Srchighlite.php | 197 --------------------- framework/Mime_Viewer/package.xml | 17 +- .../test/Horde/Mime/Viewer/viewer_php.phpt | 27 --- horde/config/mime_drivers.php.dist | 74 +++----- 7 files changed, 32 insertions(+), 495 deletions(-) delete mode 100644 framework/Mime_Viewer/lib/Horde/Mime/Viewer/Css.php delete mode 100644 framework/Mime_Viewer/lib/Horde/Mime/Viewer/Php.php delete mode 100644 framework/Mime_Viewer/lib/Horde/Mime/Viewer/Source.php delete mode 100644 framework/Mime_Viewer/lib/Horde/Mime/Viewer/Srchighlite.php delete mode 100644 framework/Mime_Viewer/test/Horde/Mime/Viewer/viewer_php.phpt diff --git a/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Css.php b/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Css.php deleted file mode 100644 index e58c5ff99..000000000 --- a/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Css.php +++ /dev/null @@ -1,119 +0,0 @@ - - * @category Horde - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @package Mime_Viewer - */ -class Horde_Mime_Viewer_Css extends Horde_Mime_Viewer_Source -{ - /** - * This driver's display capabilities. - * - * @var array - */ - protected $_capability = array( - 'full' => true, - 'info' => false, - 'inline' => true, - 'raw' => false - ); - - /** - * Attribute preg patterns. - * - * @var array - */ - protected $_attrPatterns = array( - // Attributes - '!([-\w]+\s*):!s' => '\\1:', - // Values - '!:(\s*)(.+?)(\s*;)!s' => ':\\1\\2\\3', - // URLs - '!(url\([\'"]?)(.*?)([\'"]?\))!s' => '\\1\\2\\3', - // Colors - '!(#[[:xdigit:]]{3,6})!s' => '\\1', - // Parentheses - '!({|})!s' => '\\1', - // Unity - '!(em|px|%)\b!s' => '\\1' - ); - - /** - * Handles preg patterns. - * - * @var array - */ - protected $_handlesPatterns = array( - // HTML Tags - '!\b(body|h\d|a|span|div|acronym|small|strong|em|pre|ul|ol|li|p)\b!s' => '\\1\\2', - // IDs - '!(#[-\w]+)!s' => '\\1', - // Class - '!(\.[-\w]+)\b!s' => '\\1', - // METAs - '!(:link|:visited|:hover|:active|:first-letter)!s' => '\\1' - ); - - /** - * 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() - { - $css = preg_replace_callback('!(}|\*/).*?({|/\*)!s', array($this, '_handles'), htmlspecialchars($this->_mimepart->getContents(), ENT_NOQUOTES)); - $css = preg_replace_callback('!{[^}]*}!s', array($this, '_attributes'), $css); - $css = preg_replace_callback('!/\*.*?\*/!s', array($this, '_comments'), $css); - - return $this->_renderReturn( - $this->_lineNumber(trim($css)), - 'text/html; charset=' . $GLOBALS['registry']->getCharset() - ); - } - - /** - * TODO - */ - protected function _comments($matches) - { - return '' . - preg_replace('!(http://[/\w-.]+)!s', '\\1', $matches[0]) . - ''; - } - - /** - * TODO - */ - protected function _attributes($matches) - { - return preg_replace(array_keys($this->_attrPatterns), array_values($this->_attrPatterns), $matches[0]); - } - - /** - * TODO - */ - protected function _handles($matches) - { - return preg_replace(array_keys($this->_handlesPatterns), array_values($this->_handlesPatterns), $matches[0]); - } - -} diff --git a/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Php.php b/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Php.php deleted file mode 100644 index 722ee2c31..000000000 --- a/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Php.php +++ /dev/null @@ -1,59 +0,0 @@ - - * @category Horde - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @package Mime_Viewer - */ -class Horde_Mime_Viewer_Php extends Horde_Mime_Viewer_Source -{ - /** - * 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() - { - $code = $this->_mimepart->getContents(); - - $text = (strpos($code, '_renderReturn( - $this->_lineNumber(trim(str_replace(array("\n", '
'), array('', "\n"), $text))), - 'text/html; charset=' . $this->getConfigParam('charset') - ); - } - -} diff --git a/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Source.php b/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Source.php deleted file mode 100644 index 7976e5348..000000000 --- a/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Source.php +++ /dev/null @@ -1,34 +0,0 @@ - - * @category Horde - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @package Mime_Viewer - */ -class Horde_Mime_Viewer_Source extends Horde_Mime_Viewer_Base -{ - /** - * Add line numbers to a block of code. - * - * @param string $code The code to number. - * - * @return string The code with line numbers added. - */ - protected function _lineNumber($code, $linebreak = "\n") - { - $html = array('
'); - for ($l = 1, $lines = substr_count($code, $linebreak) + 1; $l <= $lines; ++$l) { - $html[] = sprintf('%s
', $l, $l, $l); - } - return implode("\n", $html) . '
' . $code . '
'; - } - -} diff --git a/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Srchighlite.php b/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Srchighlite.php deleted file mode 100644 index 48244ee4f..000000000 --- a/framework/Mime_Viewer/lib/Horde/Mime/Viewer/Srchighlite.php +++ /dev/null @@ -1,197 +0,0 @@ - - * @author Michael Slusarz - * @category Horde - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @package Mime_Viewer - */ -class Horde_Mime_Viewer_Srchighlite extends Horde_Mime_Viewer_Base -{ - /** - * This driver's display capabilities. - * - * @var array - */ - protected $_capability = array( - 'full' => true, - 'info' => false, - 'inline' => true, - 'raw' => false - ); - - /** - * Constructor. - * - * @param Horde_Mime_Part $mime_part The object with the data to be - * rendered. - * @param array $conf Configuration: - *
-     * 'location' - (string) Location of the source-highlight binary.
-     * 
- * - * @throws InvalidArgumentException - */ - public function __construct(Horde_Mime_Part $part, array $conf = array()) - { - $this->_required = array_merge($this->_required, array( - 'location' - )); - - parent::__construct($part, $conf); - } - - /** - * 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() - { - /* Check to make sure the viewer program exists. */ - if (!($location = $this->getConfigParam('location')) || - !file_exists($location)) { - return array(); - } - - $tmpin = $this->_getTempFile(); - $tmpout = $this->_getTempFile(); - - /* Write the contents of our buffer to the temporary input file. */ - file_put_contents($tmpin, $this->_mimepart->getContents()); - - /* Determine the language from the mime type. */ - $lang = $this->_typeToLang($this->_mimepart->getType()); - - /* Execute Source-Highlite. */ - exec($location . ' --src-lang ' . escapeshellarg($lang) . ' --out-format html --input ' . $tmpin . ' --output ' . $tmpout); - $results = file_get_contents($tmpout); - unlink($tmpout); - - return $this->_renderReturn( - $results, - 'text/html; charset=' . $this->getConfigParam('charset') - ); - } - - /** - * 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 _typeToLang($type) - { - switch ($type) { - case 'application/x-javascript': - return 'js'; - - case 'application/x-perl': - return 'perl'; - - case 'application/x-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': - return 'sh'; - - case 'application/x-tcl': - return 'tcl'; - - case 'application/xml': - case 'text/xml': - return 'xml'; - - case 'text/cpp': - case 'text/x-c++': - case 'text/x-c++src': - case 'text/x-c++hdr': - return 'cpp'; - - case 'text/css': - case 'x-extension/css': - return 'css'; - - case 'text/diff': - case 'text/x-diff': - case 'text/x-patch': - return 'diff'; - - case 'text/x-c': - case 'text/x-chdr': - case 'text/x-csrc': - return 'c'; - - case 'text/x-emacs-lisp': - return 'lisp'; - - case 'text/x-fortran': - case 'x-extension/f77': - case 'x-extension/f90': - case 'x-extension/for': - case 'x-extension/ftn': - return 'fortran'; - - case 'text/x-java': - return 'java'; - - case 'text/x-pascal': - return 'pascal'; - - case 'text/x-sql': - return 'sql'; - - case 'text/x-tex': - return 'tex'; - - case 'x-extension/asm': - return 'asm'; - - case 'x-extension/bat': - return 'batch'; - - case 'x-extension/cs': - return 'csharp'; - - case 'x-extension/vb': - case 'x-extension/vba': - return 'vbs'; - } - } - -} diff --git a/framework/Mime_Viewer/package.xml b/framework/Mime_Viewer/package.xml index c0d445c28..98506baeb 100644 --- a/framework/Mime_Viewer/package.xml +++ b/framework/Mime_Viewer/package.xml @@ -14,9 +14,9 @@ http://pear.php.net/dtd/package-2.0.xsd"> slusarz@horde.org yes - 2010-07-25 + 2010-10-02 - 0.1.0 + 0.2.0 0.1.0 @@ -24,7 +24,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> beta LGPL - * Initial package. + Replaced external or PHP-based syntax highlighters with javascript SyntaxHighlighter @@ -47,7 +47,6 @@ http://pear.php.net/dtd/package-2.0.xsd"> - @@ -59,7 +58,6 @@ http://pear.php.net/dtd/package-2.0.xsd"> - @@ -70,8 +68,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - - + @@ -86,7 +83,6 @@ http://pear.php.net/dtd/package-2.0.xsd"> - @@ -149,7 +145,6 @@ http://pear.php.net/dtd/package-2.0.xsd"> - @@ -161,7 +156,6 @@ http://pear.php.net/dtd/package-2.0.xsd"> - @@ -172,8 +166,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> - - + diff --git a/framework/Mime_Viewer/test/Horde/Mime/Viewer/viewer_php.phpt b/framework/Mime_Viewer/test/Horde/Mime/Viewer/viewer_php.phpt deleted file mode 100644 index 6314c708e..000000000 --- a/framework/Mime_Viewer/test/Horde/Mime/Viewer/viewer_php.phpt +++ /dev/null @@ -1,27 +0,0 @@ ---TEST-- -PHP source viewer ---SKIPIF-- -skip: Horde_Mime_Viewer has too many dependencies. ---FILE-- -setType('application/x-php'); -$part->setContents(str_replace('<?php ', '', highlight_string('render(); -?> ---EXPECT-- -
    -
  1. highlight_file(__FILE__);
  2. -
diff --git a/horde/config/mime_drivers.php.dist b/horde/config/mime_drivers.php.dist index 474d37aff..5a345ad1a 100644 --- a/horde/config/mime_drivers.php.dist +++ b/horde/config/mime_drivers.php.dist @@ -79,34 +79,6 @@ $mime_drivers = array( ) ), - /* PHP pretty-print code display. */ - 'php' => array( - 'inline' => true, - 'handles' => array( - 'application/x-php', - 'x-extension/phps', - 'x-extension/php3s', - 'application/x-httpd-php', - 'application/x-httpd-php3', - 'application/x-httpd-phps' - ), - 'icons' => array( - 'default' => 'php.png' - ) - ), - - /* CSS pretty-print display. */ - 'css' => array( - 'inline' => true, - 'handles' => array( - 'text/css', - 'x-extension/css' - ), - 'icons' => array( - 'default' => 'html.png' - ) - ), - /* HTML display. */ 'html' => array( // NOTE: Inline HTML viewing is DISABLED by default. @@ -144,11 +116,9 @@ $mime_drivers = array( ) ), - /* Source-highlight driver. - * http://www.gnu.org/software/src-highlite/ */ - 'srchighlite' => array( - // Disabled by default - 'disable' => true, + /* SyntaxHighlighter driver. + * http://alexgorbatchev.com/SyntaxHighlighter/ */ + 'syntaxhighlighter' => array( 'inline' => true, 'handles' => array( 'application/x-httpd-php', @@ -173,27 +143,30 @@ $mime_drivers = array( 'text/x-chdr', 'text/x-csrc', 'text/x-diff', - 'text/x-emacs-lisp', - 'text/x-fortran', 'text/x-java', - 'text/x-pascal', 'text/x-patch', 'text/x-sql', 'text/x-tex', 'text/xml', - 'x-extension/asm', 'x-extension/bat', + 'x-extension/c', + 'x-extension/cpp', 'x-extension/cs', 'x-extension/css', - 'x-extension/f77', - 'x-extension/f90', - 'x-extension/for', - 'x-extension/ftn', + 'x-extension/html', + 'x-extension/js', + 'x-extension/perl', + 'x-extension/php', 'x-extension/php3s', 'x-extension/phps', - 'x-extension/rc', + 'x-extension/pl', + 'x-extension/py', + 'x-extension/python', + 'x-extension/rb', + 'x-extension/ruby', 'x-extension/vb', - 'x-extension/vba' + 'x-extension/vba', + 'x-extension/xml', ), 'icons' => array( 'default' => 'text.png', @@ -217,14 +190,21 @@ $mime_drivers = array( 'text/x-java' => 'source-java.png', 'text/x-sql' => 'sql.png', 'text/xml' => 'xml.png', + 'x-extension/bash' => 'shell.png', + 'x-extension/c' => 'source-c.png', + 'x-extension/cpp' => 'source-c.png', + 'x-extension/cs' => 'source-c.png', 'x-extension/css' => 'html.png', + 'x-extension/html' => 'html.png', + 'x-extension/js' => 'script-js.png', + 'x-extension/php' => 'php.png', 'x-extension/php3s' => 'php.png', 'x-extension/phps' => 'php.png', - 'x-extension/rc' => 'xml.png' + 'x-extension/py' => 'source-python.png', + 'x-extension/python' => 'source-python.png', + 'x-extension/sh' => 'shell.png', + 'x-extension/xml' => 'xml.png' ), - - // REQUIRED: Location of the source-highlight binary - 'location' => '/usr/bin/source-highlight' ), /* Tar file display. -- 2.11.0