*/
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('<?php ', '', highlight_string('<?php ' . $code, true)))
- : $this->_lineNumber(highlight_string($code, true));
+ if (strpos($code, '<?php') === false) {
+ $text = str_replace('<?php ', '', 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(
)
);
}
-
- /**
- * 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(' ', '&', '<br />', '<span style="color: ', '<font color="', '</font>',),
- array(' ', '&', "\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 = ' ';
- }
-
- 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);
- }
-
}