cvs: phd / config.php /formats xhtml.php /include PhDBuildOptions.class.php PhDHelper.class.php /themes/pear peartheme.php
| From: | Christian Weiske | Date: | Sun, 25 Jan 2009 19:59:29 +0000 |
| Subject: | cvs: phd / config.php /formats xhtml.php /include PhDBuildOptions.class.php PhDHelper.class.php /themes/pear peartheme.php | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-3084@lists.php.net to get a copy of this message | ||
cweiske Sun Jan 25 19:59:29 2009 UTC
Modified files:
/phd config.php
/phd/formats xhtml.php
/phd/include PhDBuildOptions.class.php PhDHelper.class.php
/phd/themes/pear peartheme.php
Log:
Implement request #47201: Allow custom source code highlighter (e.g. Geshi) (Christian)
http://cvs.php.net/viewvc.cgi/phd/config.php?r1=1.55&r2=1.56&diff_format=u Index: phd/config.php diff -u phd/config.php:1.55 phd/config.php:1.56 --- phd/config.php:1.55 Tue Nov 25 06:23:55 2008 +++ phd/config.php Sun Jan 25 19:59:26 2009 @@ -1,5 +1,5 @@ <?php -/* $Id: config.php,v 1.55 2008/11/25 06:23:55 cweiske Exp $ */ +/* $Id: config.php,v 1.56 2009/01/25 19:59:26 cweiske Exp $ */ require $ROOT . "/include/PhDErrors.php"; @@ -54,6 +54,7 @@ 'user_error_color' => false, 'phd_info_output' => NULL, 'phd_info_color' => false, + 'highlighter' => 'PhDHighlighter', ); public static function __callStatic($name, $params) http://cvs.php.net/viewvc.cgi/phd/formats/xhtml.php?r1=1.85&r2=1.86&diff_format=u Index: phd/formats/xhtml.php diff -u phd/formats/xhtml.php:1.85 phd/formats/xhtml.php:1.86 --- phd/formats/xhtml.php:1.85 Wed Jan 7 18:05:36 2009 +++ phd/formats/xhtml.php Sun Jan 25 19:59:27 2009 @@ -1,5 +1,5 @@ <?php -/* $Id: xhtml.php,v 1.85 2009/01/07 18:05:36 bjori Exp $ */ +/* $Id: xhtml.php,v 1.86 2009/01/25 19:59:27 cweiske Exp $ */ class XHTMLPhDFormat extends PhDFormat { protected $elementmap = array( /* {{{ */ @@ -427,11 +427,14 @@ } public function CDATA($str) { switch($this->role) { - case "php": - return '<div class="phpcode">' .(highlight_string(trim($str), 1)). '</div>'; - break; + case '': + return '<div class="cdata"><pre>' + . htmlspecialchars($str, ENT_QUOTES, "UTF-8") + . '</pre></div>'; default: - return '<div class="cdata"><pre>' .(htmlspecialchars($str, ENT_QUOTES, "UTF-8")). '</pre></div>'; + return '<div class="' . $this->role . 'code">' + . $this->highlight(trim($str), $this->role, 'xhtml') + . '</div>'; } } public function TEXT($str) { http://cvs.php.net/viewvc.cgi/phd/include/PhDBuildOptions.class.php?r1=1.9&r2=1.10&diff_format=u Index: phd/include/PhDBuildOptions.class.php diff -u phd/include/PhDBuildOptions.class.php:1.9 phd/include/PhDBuildOptions.class.php:1.10 --- phd/include/PhDBuildOptions.class.php:1.9 Wed Dec 3 20:01:04 2008 +++ phd/include/PhDBuildOptions.class.php Sun Jan 25 19:59:28 2009 @@ -1,5 +1,5 @@ <?php -/* $Id: PhDBuildOptions.class.php,v 1.9 2008/12/03 20:01:04 bjori Exp $ */ +/* $Id: PhDBuildOptions.class.php,v 1.10 2009/01/25 19:59:28 cweiske Exp $ */ class PhDBuildOptionsParser extends PhDOptionParser { @@ -9,19 +9,20 @@ public function getOptionList() { return array( - "format:" => "f:", // The format to render (xhtml, pdf...) - "theme:" => "t:", // The theme to render (phpweb, bightml..) - "noindex" => "I", // Re-index or load from cache - "docbook:" => "d:", // The Docbook XML file to render from (.manual.xml) - "output:" => "o:", // The output directory - "partial:" => "p:", // The ID to render (optionally ignoring its children) - "skip:" => "s:", // The ID to skip (optionally skipping its children too) - "verbose:" => "v", // Adjust the verbosity level - "list::" => "l::", // List supported themes/formats - "lang::" => "L:", // Language hint (used by the CHM) - "color:" => "c:", // Use color output if possible - "version" => "V", // Print out version information - "help" => "h", // Print out help + "format:" => "f:", // The format to render (xhtml, pdf...) + "theme:" => "t:", // The theme to render (phpweb, bightml..) + "noindex" => "I", // Re-index or load from cache + "docbook:" => "d:", // The Docbook XML file to render from (.manual.xml) + "output:" => "o:", // The output directory + "partial:" => "p:", // The ID to render (optionally ignoring its children) + "skip:" => "s:", // The ID to skip (optionally skipping its children too) + "verbose:" => "v", // Adjust the verbosity level + "list::" => "l::", // List supported themes/formats + "lang::" => "L:", // Language hint (used by the CHM) + "color:" => "c:", // Use color output if possible + 'highlighter:' => 'h:', // Class used as source code highlighter + "version" => "V", // Print out version information + "help" => "h", // Print out help ); } @@ -48,6 +49,15 @@ PhDConfig::set_output_format($formats); } + public function option_h($k, $v) + { + $this->option_highlighter($k, $v); + } + public function option_highlighter($k, $v) + { + PhDConfig::setHighlighter($v); + } + public function option_t($k, $v) { $this->option_theme($k, $v); @@ -355,6 +365,8 @@ --lang <language> The language of the source file (used by the CHM theme). (default: en -c <bool> --color <bool> Enable color output when output is to a terminal (default: false) + -h <classname> + --highlighter <classname> Use custom source code highlighting php class -V --version Print the PhD version information -h http://cvs.php.net/viewvc.cgi/phd/include/PhDHelper.class.php?r1=1.12&r2=1.13&diff_format=u Index: phd/include/PhDHelper.class.php diff -u phd/include/PhDHelper.class.php:1.12 phd/include/PhDHelper.class.php:1.13 --- phd/include/PhDHelper.class.php:1.12 Sun Oct 19 15:01:23 2008 +++ phd/include/PhDHelper.class.php Sun Jan 25 19:59:28 2009 @@ -1,5 +1,5 @@ <?php -/* $Id: PhDHelper.class.php,v 1.12 2008/10/19 15:01:23 bjori Exp $ */ +/* $Id: PhDHelper.class.php,v 1.13 2009/01/25 19:59:28 cweiske Exp $ */ /** * Base class for PhDTheme @@ -32,6 +32,16 @@ private static $autogen = array(); /** + * Array of highlighter objects + * Key is the format, value the object + * + * @var array + */ + private static $highlighters = array(); + + + + /** * Creates the helper object. * * @param array $a Array with ID array as first value, ref array as second. @@ -182,6 +192,45 @@ PhDHelper::$autogen[$lang] = $autogen; return PhDHelper::$autogen[$lang][$text]; } + + /** + * Highlight (color) the given piece of source code + * + * @param string $text Text to highlight + * @param string $role Source code role to use (php, xml, html, ...) + * @param string $format Format to highlight (pdf, html, man, ...) + * + * @return string Highlighted code + */ + public function highlight($text, $role = 'php', $format = 'xhtml') + { + if (!isset(self::$highlighters[$format])) { + $class = PhdConfig::highlighter(); + + if (!class_exists($class, true)) { + if ($class == 'PhDHighlighter') { + //hard coded path for phdhighlighter, since phd doesn't follow + // pear file conventions + require_once dirname(__FILE__) . '/PhDHighlighter.class.php'; + } else { + //simple autoload replacement for pear-style classes + require_once str_replace('_', '/', $class) . '.php'; + } + + if (!class_exists($class, false)) { + trigger_error( + 'Highlighter class ' . $class . ' is not available', + E_USER_ERROR + ); + } + } + self::$highlighters[$format] = $class::factory($format); + } + + return self::$highlighters[$format]->highlight( + $text, $role, $format + ); + }//public function highlight(..) } /* http://cvs.php.net/viewvc.cgi/phd/themes/pear/peartheme.php?r1=1.22&r2=1.23&diff_format=u Index: phd/themes/pear/peartheme.php diff -u phd/themes/pear/peartheme.php:1.22 phd/themes/pear/peartheme.php:1.23 --- phd/themes/pear/peartheme.php:1.22 Sat Jan 24 21:40:20 2009 +++ phd/themes/pear/peartheme.php Sun Jan 25 19:59:28 2009 @@ -580,7 +580,7 @@ * Format the text within a program listing section. * * @param string $value Value of the text to format. - * @param unknown_type $tag + * @param string $tag Tag name * * @return string */ @@ -588,12 +588,14 @@ { switch($this->role) { case 'php': - if ( strrpos($value, '<?php') || strrpos($value, '?>') ) - return highlight_string(trim($value), 1); - else return highlight_string("<?php\n" . trim($value) . "\n?>", 1); + if (strrpos($value, '<?php') || strrpos($value, '?>')) { + return $this->highlight(trim($value), 'php', 'xhtml'); + } else { + return $this->highlight("<?php\n" . trim($value) . "\n?>", 'php', 'xhtml'); + } break; default: - return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); + return $this->highlight(trim($value), $this->role, 'xhtml'); } } @@ -642,14 +644,17 @@ switch ($this->role) { case 'php': if (strrpos($str, '<?php') || strrpos($str, '?>')) { - $str = highlight_string(trim($str), 1); + $str = $this->highlight(trim($str), $this->role, 'xhtml'); } else { - $str = highlight_string("<?php\n" . trim($str) . "\n?>", 1); + $str = $this->highlight("<?php\n" . trim($str) . "\n?>", $this->role, 'xhtml'); } break; - default: + case '': $str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); break; + default: + $str = $this->highlight($str, $this->role, 'xhtml'); + break; } return $str;
http://cvs.php.net/viewvc.cgi/phd/config.php?r1=1.55&r2=1.56&diff_format=u Index: phd/config.php diff -u phd/config.php:1.55 phd/config.php:1.56 --- phd/config.php:1.55 Tue Nov 25 06:23:55 2008 +++ phd/config.php Sun Jan 25 19:59:26 2009 @@ -1,5 +1,5 @@ <?php -/* $Id: config.php,v 1.55 2008/11/25 06:23:55 cweiske Exp $ */ +/* $Id: config.php,v 1.56 2009/01/25 19:59:26 cweiske Exp $ */ require $ROOT . "/include/PhDErrors.php"; @@ -54,6 +54,7 @@ 'user_error_color' => false, 'phd_info_output' => NULL, 'phd_info_color' => false, + 'highlighter' => 'PhDHighlighter', ); public static function __callStatic($name, $params) http://cvs.php.net/viewvc.cgi/phd/formats/xhtml.php?r1=1.85&r2=1.86&diff_format=u Index: phd/formats/xhtml.php diff -u phd/formats/xhtml.php:1.85 phd/formats/xhtml.php:1.86 --- phd/formats/xhtml.php:1.85 Wed Jan 7 18:05:36 2009 +++ phd/formats/xhtml.php Sun Jan 25 19:59:27 2009 @@ -1,5 +1,5 @@ <?php -/* $Id: xhtml.php,v 1.85 2009/01/07 18:05:36 bjori Exp $ */ +/* $Id: xhtml.php,v 1.86 2009/01/25 19:59:27 cweiske Exp $ */ class XHTMLPhDFormat extends PhDFormat { protected $elementmap = array( /* {{{ */ @@ -427,11 +427,14 @@ } public function CDATA($str) { switch($this->role) { - case "php": - return '<div class="phpcode">' .(highlight_string(trim($str), 1)). '</div>'; - break; + case '': + return '<div class="cdata"><pre>' + . htmlspecialchars($str, ENT_QUOTES, "UTF-8") + . '</pre></div>'; default: - return '<div class="cdata"><pre>' .(htmlspecialchars($str, ENT_QUOTES, "UTF-8")). '</pre></div>'; + return '<div class="' . $this->role . 'code">' + . $this->highlight(trim($str), $this->role, 'xhtml') + . '</div>'; } } public function TEXT($str) { http://cvs.php.net/viewvc.cgi/phd/include/PhDBuildOptions.class.php?r1=1.9&r2=1.10&diff_format=u Index: phd/include/PhDBuildOptions.class.php diff -u phd/include/PhDBuildOptions.class.php:1.9 phd/include/PhDBuildOptions.class.php:1.10 --- phd/include/PhDBuildOptions.class.php:1.9 Wed Dec 3 20:01:04 2008 +++ phd/include/PhDBuildOptions.class.php Sun Jan 25 19:59:28 2009 @@ -1,5 +1,5 @@ <?php -/* $Id: PhDBuildOptions.class.php,v 1.9 2008/12/03 20:01:04 bjori Exp $ */ +/* $Id: PhDBuildOptions.class.php,v 1.10 2009/01/25 19:59:28 cweiske Exp $ */ class PhDBuildOptionsParser extends PhDOptionParser { @@ -9,19 +9,20 @@ public function getOptionList() { return array( - "format:" => "f:", // The format to render (xhtml, pdf...) - "theme:" => "t:", // The theme to render (phpweb, bightml..) - "noindex" => "I", // Re-index or load from cache - "docbook:" => "d:", // The Docbook XML file to render from (.manual.xml) - "output:" => "o:", // The output directory - "partial:" => "p:", // The ID to render (optionally ignoring its children) - "skip:" => "s:", // The ID to skip (optionally skipping its children too) - "verbose:" => "v", // Adjust the verbosity level - "list::" => "l::", // List supported themes/formats - "lang::" => "L:", // Language hint (used by the CHM) - "color:" => "c:", // Use color output if possible - "version" => "V", // Print out version information - "help" => "h", // Print out help + "format:" => "f:", // The format to render (xhtml, pdf...) + "theme:" => "t:", // The theme to render (phpweb, bightml..) + "noindex" => "I", // Re-index or load from cache + "docbook:" => "d:", // The Docbook XML file to render from (.manual.xml) + "output:" => "o:", // The output directory + "partial:" => "p:", // The ID to render (optionally ignoring its children) + "skip:" => "s:", // The ID to skip (optionally skipping its children too) + "verbose:" => "v", // Adjust the verbosity level + "list::" => "l::", // List supported themes/formats + "lang::" => "L:", // Language hint (used by the CHM) + "color:" => "c:", // Use color output if possible + 'highlighter:' => 'h:', // Class used as source code highlighter + "version" => "V", // Print out version information + "help" => "h", // Print out help ); } @@ -48,6 +49,15 @@ PhDConfig::set_output_format($formats); } + public function option_h($k, $v) + { + $this->option_highlighter($k, $v); + } + public function option_highlighter($k, $v) + { + PhDConfig::setHighlighter($v); + } + public function option_t($k, $v) { $this->option_theme($k, $v); @@ -355,6 +365,8 @@ --lang <language> The language of the source file (used by the CHM theme). (default: en -c <bool> --color <bool> Enable color output when output is to a terminal (default: false) + -h <classname> + --highlighter <classname> Use custom source code highlighting php class -V --version Print the PhD version information -h http://cvs.php.net/viewvc.cgi/phd/include/PhDHelper.class.php?r1=1.12&r2=1.13&diff_format=u Index: phd/include/PhDHelper.class.php diff -u phd/include/PhDHelper.class.php:1.12 phd/include/PhDHelper.class.php:1.13 --- phd/include/PhDHelper.class.php:1.12 Sun Oct 19 15:01:23 2008 +++ phd/include/PhDHelper.class.php Sun Jan 25 19:59:28 2009 @@ -1,5 +1,5 @@ <?php -/* $Id: PhDHelper.class.php,v 1.12 2008/10/19 15:01:23 bjori Exp $ */ +/* $Id: PhDHelper.class.php,v 1.13 2009/01/25 19:59:28 cweiske Exp $ */ /** * Base class for PhDTheme @@ -32,6 +32,16 @@ private static $autogen = array(); /** + * Array of highlighter objects + * Key is the format, value the object + * + * @var array + */ + private static $highlighters = array(); + + + + /** * Creates the helper object. * * @param array $a Array with ID array as first value, ref array as second. @@ -182,6 +192,45 @@ PhDHelper::$autogen[$lang] = $autogen; return PhDHelper::$autogen[$lang][$text]; } + + /** + * Highlight (color) the given piece of source code + * + * @param string $text Text to highlight + * @param string $role Source code role to use (php, xml, html, ...) + * @param string $format Format to highlight (pdf, html, man, ...) + * + * @return string Highlighted code + */ + public function highlight($text, $role = 'php', $format = 'xhtml') + { + if (!isset(self::$highlighters[$format])) { + $class = PhdConfig::highlighter(); + + if (!class_exists($class, true)) { + if ($class == 'PhDHighlighter') { + //hard coded path for phdhighlighter, since phd doesn't follow + // pear file conventions + require_once dirname(__FILE__) . '/PhDHighlighter.class.php'; + } else { + //simple autoload replacement for pear-style classes + require_once str_replace('_', '/', $class) . '.php'; + } + + if (!class_exists($class, false)) { + trigger_error( + 'Highlighter class ' . $class . ' is not available', + E_USER_ERROR + ); + } + } + self::$highlighters[$format] = $class::factory($format); + } + + return self::$highlighters[$format]->highlight( + $text, $role, $format + ); + }//public function highlight(..) } /* http://cvs.php.net/viewvc.cgi/phd/themes/pear/peartheme.php?r1=1.22&r2=1.23&diff_format=u Index: phd/themes/pear/peartheme.php diff -u phd/themes/pear/peartheme.php:1.22 phd/themes/pear/peartheme.php:1.23 --- phd/themes/pear/peartheme.php:1.22 Sat Jan 24 21:40:20 2009 +++ phd/themes/pear/peartheme.php Sun Jan 25 19:59:28 2009 @@ -580,7 +580,7 @@ * Format the text within a program listing section. * * @param string $value Value of the text to format. - * @param unknown_type $tag + * @param string $tag Tag name * * @return string */ @@ -588,12 +588,14 @@ { switch($this->role) { case 'php': - if ( strrpos($value, '<?php') || strrpos($value, '?>') ) - return highlight_string(trim($value), 1); - else return highlight_string("<?php\n" . trim($value) . "\n?>", 1); + if (strrpos($value, '<?php') || strrpos($value, '?>')) { + return $this->highlight(trim($value), 'php', 'xhtml'); + } else { + return $this->highlight("<?php\n" . trim($value) . "\n?>", 'php', 'xhtml'); + } break; default: - return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); + return $this->highlight(trim($value), $this->role, 'xhtml'); } } @@ -642,14 +644,17 @@ switch ($this->role) { case 'php': if (strrpos($str, '<?php') || strrpos($str, '?>')) { - $str = highlight_string(trim($str), 1); + $str = $this->highlight(trim($str), $this->role, 'xhtml'); } else { - $str = highlight_string("<?php\n" . trim($str) . "\n?>", 1); + $str = $this->highlight("<?php\n" . trim($str) . "\n?>", $this->role, 'xhtml'); } break; - default: + case '': $str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8'); break; + default: + $str = $this->highlight($str, $this->role, 'xhtml'); + break; } return $str;