* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version CVS: $Id: Code.php,v 1.13 2006/02/10 23:07:03 toggg Exp $ * @link http://pear.php.net/package/Text_Wiki */ /** * This class renders code blocks in XHTML. * * @category Text * @package Text_Wiki * @author Paul M. Jones * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1 * @version Release: @package_version@ * @link http://pear.php.net/package/Text_Wiki */ class Text_Wiki_Render_Xhtml_Code extends Text_Wiki_Render { var $conf = array( 'css' => null, // class for
        'css_code' => null, // class for generic 
        'css_php'  => null, // class for PHP 
        'css_html' => null, // class for HTML 
        'css_filename' => null // class for optional filename 
); /** * * Renders a token into text matching the requested format. * * @access public * * @param array $options The "options" portion of the token (second * element). * * @return string The text rendered from the token options. * */ function token($options) { $text = $options['text']; $attr = $options['attr']; $type = strtolower($attr['type']); $css = $this->formatConf(' class="%s"', 'css'); $css_code = $this->formatConf(' class="%s"', 'css_code'); $css_php = $this->formatConf(' class="%s"', 'css_php'); $css_html = $this->formatConf(' class="%s"', 'css_html'); $css_filename = $this->formatConf(' class="%s"', 'css_filename'); if ($type == 'php') { if (substr($options['text'], 0, 5) != '"; // ... tags) ob_start(); highlight_string($text); $text = ob_get_contents(); ob_end_clean(); // replace
tags with simple newlines. // replace non-breaking space with simple spaces. // translate HTML and color to XHTML and style. // courtesy of research by A. Kalin :-). $map = array( '
' => "\n", ' ' => ' ', ' '' => '
', 'color="' => 'style="color:' ); $text = strtr($text, $map); // get rid of the last newline inside the code block // (becuase higlight_string puts one there) if (substr($text, -8) == "\n
") { $text = substr($text, 0, -8) . "
"; } // replace all tags with classed tags if ($css_php) { $text = str_replace('', "", $text); } // done $text = "$text
"; } elseif ($type == 'html' || $type == 'xhtml') { // HTML code example: // add opening and closing tags, // convert tabs to four spaces, // convert entities. $text = str_replace("\t", " ", $text); $text = "\n$text\n"; $text = $this->textEncode($text); $text = "$text"; } else { // generic code example: // convert tabs to four spaces, // convert entities. $text = str_replace("\t", " ", $text); $text = $this->textEncode($text); $text = "$text"; } if ($css_filename && isset($attr['filename'])) { $text = "" . $attr['filename'] . '' . $text; } return "\n$text\n\n"; } } ?>