Revert to older, more colorful, non-line-chopping PHP highlighting
authorChuck Hagenbuch <chuck@bluestatedigital.com>
Tue, 22 Sep 2009 21:31:07 +0000 (17:31 -0400)
committerChuck Hagenbuch <chuck@bluestatedigital.com>
Tue, 22 Sep 2009 21:31:07 +0000 (17:31 -0400)
framework/Mime/lib/Horde/Mime/Viewer/Php.php

index 56b4f21..2799b0a 100644 (file)
@@ -51,16 +51,14 @@ class Horde_Mime_Viewer_Php extends Horde_Mime_Viewer_Source
      */
     protected function _renderInline()
     {
-        ini_set('highlight.comment', 'comment');
-        ini_set('highlight.default', 'default');
-        ini_set('highlight.keyword', 'keyword');
-        ini_set('highlight.string', 'string');
-        ini_set('highlight.html', 'html');
-
         $code = $this->_mimepart->getContents();
-        $text = (strpos($code, '<?php') === false)
-            ? $this->_lineNumber(str_replace('&lt;?php&nbsp;', '', highlight_string('<?php ' . $code, true)))
-            : $this->_lineNumber(highlight_string($code, true));
+        if (strpos($code, '<?php') === false) {
+            $text = str_replace('&lt;?php&nbsp;', '', highlight_string('<?php ' . $code, true));
+        } else {
+            $text = highlight_string($code, true);
+        }
+        $text = trim(str_replace(array("\n", '<br />'), array('', "\n"), $text));
+        $text = $this->_lineNumber($text);
 
         return array(
             $this->_mimepart->getMimeId() => array(
@@ -70,89 +68,4 @@ class Horde_Mime_Viewer_Php extends Horde_Mime_Viewer_Source
             )
         );
     }
-
-    /**
-     * 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)
-    {
-        // Clean up.
-        $code = preg_replace(
-            array(
-                '/<code><span style="color: #?\w+">\s*/',
-                '/<code><font color="#?\w+">\s*/',
-                '/\s*<\/span>\s*<\/span>\s*<\/code>/',
-                '/\s*<\/font>\s*<\/font>\s*<\/code>/'
-            ), '', $code);
-
-        $code = str_replace(
-            array('&nbsp;', '&amp;', '<br />', '<span style="color: ', '<font color="', '</font>',),
-            array(' ', '&#38;', "\n", '<span class="', '<span class="', '</span>',),
-            $code);
-
-        $code = trim($code);
-
-        // Normalize newlines.
-        $code = str_replace("\r", '', $code);
-        $code = preg_replace('/\n\n\n+/', "\n\n", $code);
-
-        $results = array('<ol class="code-listing striped">');
-        $previous = false;
-
-        $lines = explode("\n", $code);
-        reset($lines);
-        while (list($lineno, $line) = each($lines)) {
-            if (substr($line, 0, 7) == '</span>') {
-                $previous = false;
-                $line = substr($line, 7);
-            }
-
-            if (empty($line)) {
-                $line = '&#160;';
-            }
-
-            if ($previous) {
-                $line = "<span class=\"$previous\">" . $line;
-            }
-
-            // Save the previous style.
-            if (strpos($line, '<span') !== false) {
-                switch (substr($line, strrpos($line, '<span') + 13, 1)) {
-                case 'c':
-                    $previous = 'comment';
-                    break;
-
-                case 'd':
-                    $previous = 'default';
-                    break;
-
-                case 'k':
-                    $previous = 'keyword';
-                    break;
-
-                case 's':
-                    $previous = 'string';
-                    break;
-                }
-            }
-
-            // Unset previous style unless the span continues.
-            if (substr($line, -7) == '</span>') {
-                $previous = false;
-            } elseif ($previous) {
-                $line .= '</span>';
-            }
-
-            $results[] = '<li id="l' . ($lineno + 1). '">' . $line . '</li>';
-        }
-
-        $results[] = '</ol>';
-
-        return implode("\n", $results);
-    }
-
 }