* @copyright 2005 Stoyan Stefanov * @license http://www.php.net/license/3_0.txt PHP License * @version CVS: $Id$ * @link http://pear.php.net/package/Text_Highlighter */ /** * @ignore */ require_once 'Text/Highlighter/Renderer.php'; /** * HTML basic tags renderer, based on Andrey Demenev's HTML renderer. * * Elements of $options argument of constructor (each being optional): * * - 'numbers' - Line numbering TRUE or FALSE. Default is FALSE. * - 'tabsize' - Tab size, default is 4. * - 'tags' - Array, containing the tags to be used for highlighting * * Here's the listing of the default tags: * - 'default' => '', * - 'code' => '', * - 'brackets' => 'b', * - 'comment' => 'i', * - 'mlcomment' => 'i', * - 'quotes' => '', * - 'string' => 'i', * - 'identifier' => 'b', * - 'builtin' => 'b', * - 'reserved' => 'u', * - 'inlinedoc' => 'i', * - 'var' => 'b', * - 'url' => 'i', * - 'special' => '', * - 'number' => '', * - 'inlinetags' => '' * * @author Stoyan Stefanov * @category Text * @package Text_Highlighter * @copyright 2005 Stoyan Stefanov * @license http://www.php.net/license/3_0.txt PHP License * @version Release: 0.5.0 * @link http://pear.php.net/package/Text_Highlighter */ class Text_Highlighter_Renderer_HtmlTags extends Text_Highlighter_Renderer { /**#@+ * @access private */ /** * Line numbering - will use 'ol' tag * * @var boolean */ var $_numbers = false; /** * Tab size * * @var integer */ var $_tabsize = 4; var $_hilite_tags = array( 'default' => '', 'code' => '', 'brackets' => 'b', 'comment' => 'i', 'mlcomment' => 'i', 'quotes' => '', 'string' => 'i', 'identifier' => 'b', 'builtin' => 'b', 'reserved' => 'u', 'inlinedoc' => 'i', 'var' => 'b', 'url' => 'i', 'special' => '', 'number' => '', 'inlinetags' => '', ); /** * Highlighted code * * @var string */ var $_output = ''; /**#@-*/ function preprocess($str) { // normalize whitespace and tabs $str = str_replace("\r\n","\n", $str); // some browsers refuse to display empty lines $str = preg_replace('~^$~m'," ", $str); $str = str_replace("\t",str_repeat(' ', $this->_tabsize), $str); return rtrim($str); } /** * Resets renderer state * * @access protected * * * Descendents of Text_Highlighter call this method from the constructor, * passing $options they get as parameter. */ function reset() { $this->_output = ''; $this->_lastClass = 'default'; if (isset($this->_options['numbers'])) { $this->_numbers = $this->_options['numbers']; } if (isset($this->_options['tabsize'])) { $this->_tabsize = $this->_options['tabsize']; } if (isset($this->_options['tags'])) { $this->_hilite_tags = array_merge($this->_tags, $this->_options['tags']); } } /** * Accepts next token * * @abstract * @access public * * @param string $class Token class * @param string $content Token content */ function acceptToken($class, $content) { $iswhitespace = ctype_space($content); $content = htmlspecialchars($content); if (!$iswhitespace && !empty($this->_hilite_tags[$class])) { $this->_output .= '<'. $this->_hilite_tags[$class] . '>' . $content . '_hilite_tags[$class] . '>'; } else { $this->_output .= $content; } } /** * Signals that no more tokens are available * * @abstract * @access public * */ function finalize() { if ($this->_numbers) { /* additional whitespace for browsers that do not display empty list items correctly */ $this->_output = '
  •  ' . str_replace("\n", "
  • \n
  •  ", $this->_output) . '
  • '; $this->_output = '
      ' . str_replace(' ', '  ', $this->_output) . '
    '; } else { $this->_output = '
    ' . $this->_output . '
    '; } } /** * Get generated output * * @abstract * @return string Highlighted code * @access public * */ function getOutput() { return $this->_output; } } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * c-hanging-comment-ender-p: nil * End: */ ?>