<?php
-
-require_once dirname(__FILE__) . '/source.php';
-
/**
* The Horde_Mime_Viewer_php class renders out syntax-highlighted PHP code in
* HTML format.
* @author Chuck Hagenbuch <chuck@horde.org>
* @package Horde_Mime_Viewer
*/
-class Horde_Mime_Viewer_php extends Horde_Mime_Viewer_source
+class Horde_Mime_Viewer_php extends Horde_Mime_Viewer_Driver
{
/**
- * Renders out the contents.
+ * Can this driver render various views?
*
- * @param array $params Any parameters the Viewer may need.
+ * @var boolean
+ */
+ protected $_capability = array(
+ 'embedded' => false,
+ 'full' => true,
+ 'info' => false,
+ 'inline' => true
+ );
+
+ /**
+ * Return the full rendered version of the Horde_Mime_Part object.
*
- * @return string The rendered contents.
+ * @return array See Horde_Mime_Viewer_Driver::render().
*/
- public function render($params = array())
+ protected function _render()
+ {
+ $ret = $this->_renderInline();
+ $ret['data'] = Util::bufferOutput('require', $GLOBALS['registry']->get('templates', 'horde') . '/common-header.inc') .
+ $ret['data'] .
+ Util::bufferOutput('require', $GLOBALS['registry']->get('templates', 'horde') . '/common-footer.inc');
+
+ return $ret;
+ }
+
+ /**
+ * Return the rendered inline version of the Horde_Mime_Part object.
+ *
+ * @return array See Horde_Mime_Viewer_Driver::render().
+ */
+ protected function _renderInline()
{
ini_set('highlight.comment', 'comment');
ini_set('highlight.default', 'default');
ini_set('highlight.string', 'string');
ini_set('highlight.html', 'html');
- $code = $this->mime_part->getContents();
- if (strpos($code, '<?php') === false) {
- $results = $this->lineNumber(str_replace('<?php ', '', highlight_string('<?php ' . $code, true)));
- } else {
- $results = $this->lineNumber(highlight_string($code, true));
- }
+ $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));
- // Educated guess at whether we are inline or not.
- if (headers_sent() || ob_get_length()) {
- return $results;
- } else {
- return Util::bufferOutput('require', $GLOBALS['registry']->get('templates', 'horde') . '/common-header.inc')
- . $results
- . Util::bufferOutput('require', $GLOBALS['registry']->get('templates', 'horde') . '/common-footer.inc');
- }
+ return array(
+ 'data' => $text,
+ 'type' => 'text/html; charset=' . NLS::getCharset()
+ );
}
/**
* Add line numbers to a block of code.
*
* @param string $code The code to number.
+ *
+ * @return string The code with line numbers added.
*/
- public function lineNumber($code, $linebreak = "\n")
+ protected function _lineNumber($code, $linebreak = "\n")
{
// 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 = 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);
- $lines = explode("\n", $code);
-
$results = array('<ol class="code-listing striped">');
$previous = false;
- foreach ($lines as $lineno => $line) {
+
+ $lines = explode("\n", $code);
+ reset($lines);
+ while (list($lineno, $line) = each($lines)) {
if (substr($line, 0, 7) == '</span>') {
$previous = false;
$line = substr($line, 7);
}
$results[] = '</ol>';
+
return implode("\n", $results);
}
}