1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
<?php
/**
* Source code highlighting class for phd.
*
* @author Christian Weiske <cweiske@php.net>
*/
class PhDHighlighter
{
/**
* Create a new highlighter instance for the given format.
*
* We use a factory so you can return different objects/classes
* per format.
*
* @param string $format Output format (pdf, xhtml, troff, ...)
*
* @return PhDHighlighter Highlighter object
*/
public static function factory($format)
{
return new self();
}//public static function factory(..)
/**
* Highlight a given piece of source code.
* Dead simple version that only works for xhtml+php. Returns text as
* it was in all other cases.
*
* @param string $text Text to highlight
* @param string $role Source code role to use (php, xml, html, ...)
* @param string $format Output format (pdf, xhtml, troff, ...)
*
* @return string Highlighted code
*/
public function highlight($text, $role, $format)
{
if ($format == 'troff') {
return "\n.PP\n.nf\n"
. str_replace("\\", "\\\\", trim($text))
. "\n.fi";
} else if ($format != 'xhtml') {
return $text;
}
if ($role == 'php') {
return highlight_string($text, 1);
} else {
return '<pre class="'. ($role ? $role . 'code' : 'programlisting') .'">'
. htmlspecialchars($text, ENT_QUOTES, 'UTF-8')
. "</pre>\n";
}
return $retval;
}//public function highlight(..)
}
?>
|