diff options
author | redmatrix <git@macgirvin.com> | 2016-06-15 19:44:15 -0700 |
---|---|---|
committer | redmatrix <git@macgirvin.com> | 2016-06-15 19:44:15 -0700 |
commit | fa48de33c2f6cefbac8bfec7cde75b75390d5f39 (patch) | |
tree | 63440977ec1d802850c7b8c21496f01c9a44e7fd /library/Text_Highlighter/Text/Highlighter/Renderer | |
parent | 476116a972c0f8b8ade495de557b8fc8d3097964 (diff) | |
download | volse-hubzilla-fa48de33c2f6cefbac8bfec7cde75b75390d5f39.tar.gz volse-hubzilla-fa48de33c2f6cefbac8bfec7cde75b75390d5f39.tar.bz2 volse-hubzilla-fa48de33c2f6cefbac8bfec7cde75b75390d5f39.zip |
provide syntax based [colour] highlighting on code blocks for popular languages. I'm not happy with the line height on the list elements but couldn't see where this was defaulted. This uses the syntax [code=xxx]some code snippet[/code], where xxx represents a code/language style - with about 18 builtins.
Diffstat (limited to 'library/Text_Highlighter/Text/Highlighter/Renderer')
7 files changed, 1488 insertions, 0 deletions
diff --git a/library/Text_Highlighter/Text/Highlighter/Renderer/Array.php b/library/Text_Highlighter/Text/Highlighter/Renderer/Array.php new file mode 100644 index 000000000..fd238a4d1 --- /dev/null +++ b/library/Text_Highlighter/Text/Highlighter/Renderer/Array.php @@ -0,0 +1,200 @@ +<?php +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ +/** + * Array renderer. + * + * Produces an array that contains class names and content pairs. + * The array can be enumerated or associative. Associative means + * <code>class => content</code> pairs. + * Based on the HTML renderer by Andrey Demenev. + * + * LICENSE: This source file is subject to version 3.0 of the PHP license + * that is available through the world-wide-web at the following URI: + * http://www.php.net/license/3_0.txt. If you did not receive a copy of + * the PHP License and are unable to obtain it through the web, please + * send a note to license@php.net so we can mail you a copy immediately. + * + * @category Text + * @package Text_Highlighter + * @author Stoyan Stefanov <ssttoo@gmail.com> + * @copyright 2006 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'; + +/** + * Array renderer, based on Andrey Demenev's HTML renderer. + * + * In addition to the options supported by the HTML renderer, + * the following options were also introduced: + * <ul><li>htmlspecialchars - whether or not htmlspecialchars() will + * be called on the content, default TRUE</li> + * <li>enumerated - type of array produced, default FALSE, + * meaning associative array</li> + * </ul> + * + * + * @author Stoyan Stefanov <ssttoo@gmail.com> + * @category Text + * @package Text_Highlighter + * @copyright 2006 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_Array extends Text_Highlighter_Renderer +{ + + /**#@+ + * @access private + */ + + /** + * Tab size + * + * @var integer + */ + var $_tabsize = 4; + + /** + * Should htmlentities() will be called + * + * @var boolean + */ + var $_htmlspecialchars = true; + + /** + * Enumerated or associative array + * + * @var integer + */ + var $_enumerated = false; + + /** + * Array containing highlighting rules + * + * @var array + */ + var $_output = array(); + + /**#@-*/ + + /** + * Preprocesses code + * + * @access public + * + * @param string $str Code to preprocess + * @return string Preprocessed code + */ + function preprocess($str) + { + // normalize whitespace and tabs + $str = str_replace("\r\n","\n", $str); + $str = str_replace("\r","\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 + * + * Descendents of Text_Highlighter call this method from the constructor, + * passing $options they get as parameter. + * + * @access protected + */ + function reset() + { + $this->_output = array(); + $this->_lastClass = 'default'; + if (isset($this->_options['tabsize'])) { + $this->_tabsize = $this->_options['tabsize']; + } + if (isset($this->_options['htmlspecialchars'])) { + $this->_htmlspecialchars = $this->_options['htmlspecialchars']; + } + if (isset($this->_options['enumerated'])) { + $this->_enumerated = $this->_options['enumerated']; + } + } + + + + /** + * Accepts next token + * + * @abstract + * @access public + * @param string $class Token class + * @param string $content Token content + */ + function acceptToken($class, $content) + { + + + $theClass = $this->_getFullClassName($class); + if ($this->_htmlspecialchars) { + $content = htmlspecialchars($content); + } + if ($this->_enumerated) { + $this->_output[] = array($class, $content); + } else { + $this->_output[][$class] = $content; + } + $this->_lastClass = $class; + + } + + + /** + * Given a CSS class name, returns the class name + * with language name prepended, if necessary + * + * @access private + * + * @param string $class Token class + */ + function _getFullClassName($class) + { + if (!empty($this->_options['use_language'])) { + $theClass = $this->_language . '-' . $class; + } else { + $theClass = $class; + } + return $theClass; + } + + /** + * Get generated output + * + * @abstract + * @return array Highlighted code as an array + * @access public + */ + function getOutput() + { + return $this->_output; + } +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * c-hanging-comment-ender-p: nil + * End: + */ + +?>
\ No newline at end of file diff --git a/library/Text_Highlighter/Text/Highlighter/Renderer/BB.php b/library/Text_Highlighter/Text/Highlighter/Renderer/BB.php new file mode 100644 index 000000000..abd77cfd8 --- /dev/null +++ b/library/Text_Highlighter/Text/Highlighter/Renderer/BB.php @@ -0,0 +1,238 @@ +<?php +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ +/** + * BB code renderer. + * + * This BB renderer produces BB code, ready to be pasted in bulletin boards and + * other applications that accept BB code. Based on the HTML renderer by Andrey Demenev. + * + * LICENSE: This source file is subject to version 3.0 of the PHP license + * that is available through the world-wide-web at the following URI: + * http://www.php.net/license/3_0.txt. If you did not receive a copy of + * the PHP License and are unable to obtain it through the web, please + * send a note to license@php.net so we can mail you a copy immediately. + * + * @category Text + * @package Text_Highlighter + * @author Stoyan Stefanov <ssttoo@gmail.com> + * @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'; + +/** + * BB code renderer, based on Andrey Demenev's HTML renderer. + * + * Elements of $options argument of constructor (each being optional): + * + * - 'numbers' - Line numbering TRUE or FALSE + * - 'tabsize' - Tab size, default is 4 + * - 'bb_tags' - An array containing three BB tags, see below + * - 'tag_brackets' - An array that conains opening and closing tags, [ and ] + * - 'colors' - An array with all the colors to be used for highlighting + * + * The default BB tags are: + * - 'color' => 'color' + * - 'list' => 'list' + * - 'list_item' => '*' + * + * The default colors for the highlighter are: + * - 'default' => 'Black', + * - 'code' => 'Gray', + * - 'brackets' => 'Olive', + * - 'comment' => 'Orange', + * - 'mlcomment' => 'Orange', + * - 'quotes' => 'Darkred', + * - 'string' => 'Red', + * - 'identifier' => 'Blue', + * - 'builtin' => 'Teal', + * - 'reserved' => 'Green', + * - 'inlinedoc' => 'Blue', + * - 'var' => 'Darkblue', + * - 'url' => 'Blue', + * - 'special' => 'Navy', + * - 'number' => 'Maroon', + * - 'inlinetags' => 'Blue', + * + * + * @author Stoyan Stefanov <ssttoo@gmail.com> + * @category Text + * @package Text_Highlighter + * @copyright 20045 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_BB extends Text_Highlighter_Renderer_Array +{ + + /**#@+ + * @access private + */ + + /** + * Line numbering - will use the specified BB tag for listings + * + * @var boolean + */ + var $_numbers = false; + + /** + * BB tags to be used + * + * @var array + */ + var $_bb_tags = array ( + 'color' => 'color', + 'list' => 'list', + 'list_item' => '*', + 'code' => 'code', + ); + + /** + * BB brackets - [ and ] + * + * @var array + */ + var $_tag_brackets = array ('start' => '[', 'end' => ']'); + + /** + * Colors map + * + * @var boolean + */ + var $_colors = array( + 'default' => 'Black', + 'code' => 'Gray', + 'brackets' => 'Olive', + 'comment' => 'Orange', + 'mlcomment' => 'Orange', + 'quotes' => 'Darkred', + 'string' => 'Red', + 'identifier' => 'Blue', + 'builtin' => 'Teal', + 'reserved' => 'Green', + 'inlinedoc' => 'Blue', + 'var' => 'Darkblue', + 'url' => 'Blue', + 'special' => 'Navy', + 'number' => 'Maroon', + 'inlinetags' => 'Blue', + ); + + /**#@-*/ + + /** + * Resets renderer state + * + * @access protected + * + * + * Descendents of Text_Highlighter call this method from the constructor, + * passing $options they get as parameter. + */ + function reset() + { + parent::reset(); + if (isset($this->_options['numbers'])) { + $this->_numbers = $this->_options['numbers']; + } + if (isset($this->_options['bb_tags'])) { + $this->_bb_tags = array_merge($this->_bb_tags, $this->_options['bb_tags']); + } + if (isset($this->_options['tag_brackets'])) { + $this->_tag_brackets = array_merge($this->_tag_brackets, $this->_options['tag_brackets']); + } + if (isset($this->_options['colors'])) { + $this->_colors = array_merge($this->_colors, $this->_options['colors']); + } + } + + + /** + * Signals that no more tokens are available + * + * @abstract + * @access public + * + */ + function finalize() + { + + // get parent's output + parent::finalize(); + $output = parent::getOutput(); + + $bb_output = ''; + + $color_start = $this->_tag_brackets['start'] . $this->_bb_tags['color'] . '=%s' . $this->_tag_brackets['end']; + $color_end = $this->_tag_brackets['start'] . '/' . $this->_bb_tags['color'] . $this->_tag_brackets['end']; + + // loop through each class=>content pair + foreach ($output AS $token) { + + if ($this->_enumerated) { + $class = $token[0]; + $content = $token[1]; + } else { + $key = key($token); + $class = $key; + $content = $token[$key]; + } + + $iswhitespace = ctype_space($content); + if (!$iswhitespace && !empty($this->_colors[$class])) { + $bb_output .= sprintf($color_start, $this->_colors[$class]); + $bb_output .= $content; + $bb_output .= $color_end; + } else { + $bb_output .= $content; + } + } + + if ($this->_numbers) { + + $item_tag = $this->_tag_brackets['start'] . + $this->_bb_tags['list_item'] . + $this->_tag_brackets['end']; + $this->_output = $item_tag . str_replace("\n", "\n". $item_tag .' ', $bb_output); + $this->_output = $this->_tag_brackets['start'] . + $this->_bb_tags['list'] . + $this->_tag_brackets['end'] . + $this->_output . + $this->_tag_brackets['start'] . + '/'. + $this->_bb_tags['list'] . + $this->_tag_brackets['end'] + ; + } else { + $this->_output = $this->_tag_brackets['start'] . + $this->_bb_tags['code'] . + $this->_tag_brackets['end'] . + $bb_output . + $this->_tag_brackets['start'] . + '/' . + $this->_bb_tags['code'] . + $this->_tag_brackets['end']; + } + } + +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * c-hanging-comment-ender-p: nil + * End: + */ + +?> diff --git a/library/Text_Highlighter/Text/Highlighter/Renderer/Console.php b/library/Text_Highlighter/Text/Highlighter/Renderer/Console.php new file mode 100644 index 000000000..d7b1fba88 --- /dev/null +++ b/library/Text_Highlighter/Text/Highlighter/Renderer/Console.php @@ -0,0 +1,208 @@ +<?php +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ +/** + * Console renderer + * + * PHP versions 4 and 5 + * + * LICENSE: This source file is subject to version 3.0 of the PHP license + * that is available through the world-wide-web at the following URI: + * http://www.php.net/license/3_0.txt. If you did not receive a copy of + * the PHP License and are unable to obtain it through the web, please + * send a note to license@php.net so we can mail you a copy immediately. + * + * @category Text + * @package Text_Highlighter + * @author Andrey Demenev <demenev@gmail.com> + * @copyright 2004-2006 Andrey Demenev + * @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'; + +define ('HL_CONSOLE_DEFCOLOR', "\033[0m"); + +/** + * Console renderer + * + * Suitable for displaying text on color-capable terminals, directly + * or trough less -r + * + * Elements of $options argument of constructor (each being optional): + * + * - 'numbers' - whether to add line numbers + * - 'tabsize' - Tab size + * - 'colors' - additional colors + * + * @author Andrey Demenev <demenev@gmail.com> + * @category Text + * @package Text_Highlighter + * @copyright 2004-2006 Andrey Demenev + * @license http://www.php.net/license/3_0.txt PHP License + * @version Release: @package_version@ + * @link http://pear.php.net/package/Text_Highlighter + */ + +class Text_Highlighter_Renderer_Console extends Text_Highlighter_Renderer +{ + + /**#@+ + * @access private + */ + + /** + * class of last outputted text chunk + * + * @var string + */ + var $_lastClass; + + /** + * Line numbering + * + * @var boolean + */ + var $_numbers = false; + + /** + * Tab size + * + * @var integer + */ + var $_tabsize = 4; + + /** + * Highlighted code + * + * @var string + */ + var $_output = ''; + + /**#@-*/ + + var $_colors = array(); + + var $_defColors = array( + 'default' => "\033[0m", + 'inlinetags' => "\033[31m", + 'brackets' => "\033[36m", + 'quotes' => "\033[34m", + 'inlinedoc' => "\033[34m", + 'var' => "\033[1m", + 'types' => "\033[32m", + 'number' => "\033[32m", + 'string' => "\033[31m", + 'reserved' => "\033[35m", + 'comment' => "\033[33m", + 'mlcomment' => "\033[33m", + ); + + function preprocess($str) + { + // normalize whitespace and tabs + $str = str_replace("\r\n","\n", $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->_lastClass = ''; + if (isset($this->_options['numbers'])) { + $this->_numbers = (bool)$this->_options['numbers']; + } else { + $this->_numbers = false; + } + if (isset($this->_options['tabsize'])) { + $this->_tabsize = $this->_options['tabsize']; + } else { + $this->_tabsize = 4; + } + if (isset($this->_options['colors'])) { + $this->_colors = array_merge($this->_defColors, $this->_options['colors']); + } else { + $this->_colors = $this->_defColors; + } + $this->_output = ''; + } + + + + /** + * Accepts next token + * + * @access public + * + * @param string $class Token class + * @param string $content Token content + */ + function acceptToken($class, $content) + { + if (isset($this->_colors[$class])) { + $color = $this->_colors[$class]; + } else { + $color = $this->_colors['default']; + } + if ($this->_lastClass != $class) { + $this->_output .= $color; + } + $content = str_replace("\n", $this->_colors['default'] . "\n" . $color, $content); + $content .= $this->_colors['default']; + $this->_output .= $content; + } + + /** + * Signals that no more tokens are available + * + * @access public + * + */ + function finalize() + { + if ($this->_numbers) { + $nlines = substr_count($this->_output, "\n") + 1; + $len = strlen($nlines); + $i = 1; + $this->_output = preg_replace('~^~em', '" " . str_pad($i++, $len, " ", STR_PAD_LEFT) . ": "', $this->_output); + } + $this->_output .= HL_CONSOLE_DEFCOLOR . "\n"; + } + + /** + * Get generated output + * + * @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: + */ + +?> diff --git a/library/Text_Highlighter/Text/Highlighter/Renderer/Html.php b/library/Text_Highlighter/Text/Highlighter/Renderer/Html.php new file mode 100644 index 000000000..5d6e56ae1 --- /dev/null +++ b/library/Text_Highlighter/Text/Highlighter/Renderer/Html.php @@ -0,0 +1,465 @@ +<?php +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ +/** + * HTML renderer + * + * PHP versions 4 and 5 + * + * LICENSE: This source file is subject to version 3.0 of the PHP license + * that is available through the world-wide-web at the following URI: + * http://www.php.net/license/3_0.txt. If you did not receive a copy of + * the PHP License and are unable to obtain it through the web, please + * send a note to license@php.net so we can mail you a copy immediately. + * + * @category Text + * @package Text_Highlighter + * @author Andrey Demenev <demenev@gmail.com> + * @copyright 2004-2006 Andrey Demenev + * @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'; +require_once 'Text/Highlighter/Renderer/Array.php'; + +// BC trick : only define constants if Text/Highlighter.php +// is not yet included +if (!defined('HL_NUMBERS_LI')) { + /**#@+ + * Constant for use with $options['numbers'] + */ + /** + * use numbered list, deprecated, use HL_NUMBERS_OL instaed + * @deprecated + */ + define ('HL_NUMBERS_LI' , 1); + /** + * Use 2-column table with line numbers in left column and code in right column. + */ + define ('HL_NUMBERS_TABLE' , 2); + /**#@-*/ +} + + +/**#@+ + * Constant for use with $options['numbers'] + */ +/** + * Use numbered list + */ +define ('HL_NUMBERS_OL', 1); +/** + * Use non-numbered list + */ +define ('HL_NUMBERS_UL', 3); +/**#@-*/ + + +/** + * HTML renderer + * + * Elements of $options argument of constructor (each being optional): + * + * - 'numbers' - Line numbering style 0 or {@link HL_NUMBERS_TABLE} + * or {@link HL_NUMBERS_UL} or {@link HL_NUMBERS_OL} + * - 'numbers_start' - starting number for numbered lines + * - 'tabsize' - Tab size + * - 'style_map' - Mapping of keywords to formatting rules using inline styles + * - 'class_map' - Mapping of keywords to formatting rules using class names + * - 'doclinks' - array that has keys "url", "target" and "elements", used for + * generating links to online documentation + * - 'use_language' - class names will be prefixed with language, like "php-reserved" or "css-code" + * + * Example of setting documentation links: + * $options['doclinks'] = array( + * 'url' => 'http://php.net/%s', + * 'target' => '_blank', + * 'elements' => array('reserved', 'identifier') + * ); + * + * Example of setting class names map: + * $options['class_map'] = array( + * 'main' => 'my-main', + * 'table' => 'my-table', + * 'gutter' => 'my-gutter', + * 'brackets' => 'my-brackets', + * 'builtin' => 'my-builtin', + * 'code' => 'my-code', + * 'comment' => 'my-comment', + * 'default' => 'my-default', + * 'identifier' => 'my-identifier', + * 'inlinedoc' => 'my-inlinedoc', + * 'inlinetags' => 'my-inlinetags', + * 'mlcomment' => 'my-mlcomment', + * 'number' => 'my-number', + * 'quotes' => 'my-quotes', + * 'reserved' => 'my-reserved', + * 'special' => 'my-special', + * 'string' => 'my-string', + * 'url' => 'my-url', + * 'var' => 'my-var', + * ); + * + * Example of setting styles mapping: + * $options['style_map'] = array( + * 'main' => 'color: black', + * 'table' => 'border: 1px solid black', + * 'gutter' => 'background-color: yellow', + * 'brackets' => 'color: blue', + * 'builtin' => 'color: red', + * 'code' => 'color: green', + * 'comment' => 'color: orange', + * // .... + * ); + * + * + * @author Andrey Demenev <demenev@gmail.com> + * @category Text + * @package Text_Highlighter + * @copyright 2004-2006 Andrey Demenev + * @license http://www.php.net/license/3_0.txt PHP License + * @version Release: @package_version@ + * @link http://pear.php.net/package/Text_Highlighter + */ + +class Text_Highlighter_Renderer_Html extends Text_Highlighter_Renderer_Array +{ + + /**#@+ + * @access private + */ + + /** + * Line numbering style + * + * @var integer + */ + var $_numbers = 0; + + /** + * For numberered lines - where to start + * + * @var integer + */ + var $_numbers_start = 0; + + /** + * Tab size + * + * @var integer + */ + var $_tabsize = 4; + + /** + * Highlighted code + * + * @var string + */ + var $_output = ''; + + /** + * Mapping of keywords to formatting rules using inline styles + * + * @var array + */ + var $_style_map = array(); + + /** + * Mapping of keywords to formatting rules using class names + * + * @var array + */ + var $_class_map = array( + 'main' => 'hl-main', + 'table' => 'hl-table', + 'gutter' => 'hl-gutter', + 'brackets' => 'hl-brackets', + 'builtin' => 'hl-builtin', + 'code' => 'hl-code', + 'comment' => 'hl-comment', + 'default' => 'hl-default', + 'identifier' => 'hl-identifier', + 'inlinedoc' => 'hl-inlinedoc', + 'inlinetags' => 'hl-inlinetags', + 'mlcomment' => 'hl-mlcomment', + 'number' => 'hl-number', + 'prepro' => 'hl-prepro', + 'quotes' => 'hl-quotes', + 'reserved' => 'hl-reserved', + 'special' => 'hl-special', + 'string' => 'hl-string', + 'types' => 'hl-types', + 'url' => 'hl-url', + 'var' => 'hl-var', + ); + + /** + * Setup for links to online documentation + * + * This is an array with keys: + * - url, ex. http://php.net/%s + * - target, ex. _blank, default - no target + * - elements, default is <code>array('reserved', 'identifier')</code> + * + * @var array + */ + var $_doclinks = array(); + + /**#@-*/ + + + /** + * 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 = ''; + if (isset($this->_options['numbers'])) { + $this->_numbers = (int)$this->_options['numbers']; + if ($this->_numbers != HL_NUMBERS_LI + && $this->_numbers != HL_NUMBERS_UL + && $this->_numbers != HL_NUMBERS_OL + && $this->_numbers != HL_NUMBERS_TABLE + ) { + $this->_numbers = 0; + } + } + if (isset($this->_options['tabsize'])) { + $this->_tabsize = $this->_options['tabsize']; + } + if (isset($this->_options['numbers_start'])) { + $this->_numbers_start = intval($this->_options['numbers_start']); + } + if (isset($this->_options['doclinks']) && + is_array($this->_options['doclinks']) && + !empty($this->_options['doclinks']['url']) + ) { + + $this->_doclinks = $this->_options['doclinks']; // keys: url, target, elements array + + if (empty($this->_options['doclinks']['elements'])) { + $this->_doclinks['elements'] = array('reserved', 'identifier'); + } + } + if (isset($this->_options['style_map'])) { + $this->_style_map = $this->_options['style_map']; + } + if (isset($this->_options['class_map'])) { + $this->_class_map = array_merge($this->_class_map, $this->_options['class_map']); + } + $this->_htmlspecialchars = true; + + } + + + /** + * Given a CSS class name, returns the class name + * with language name prepended, if necessary + * + * @access private + * + * @param string $class Token class + */ + function _getFullClassName($class) + { + if (!empty($this->_options['use_language'])) { + $the_class = $this->_language . '-' . $class; + } else { + $the_class = $class; + } + return $the_class; + } + + /** + * Signals that no more tokens are available + * + * @access public + */ + function finalize() + { + + // get parent's output + parent::finalize(); + $output = parent::getOutput(); + + $html_output = ''; + + $numbers_li = false; + + if ( + $this->_numbers == HL_NUMBERS_LI || + $this->_numbers == HL_NUMBERS_UL || + $this->_numbers == HL_NUMBERS_OL + ) + { + $numbers_li = true; + } + + // loop through each class=>content pair + foreach ($output AS $token) { + + if ($this->_enumerated) { + $key = false; + $the_class = $token[0]; + $content = $token[1]; + } else { + $key = key($token); + $the_class = $key; + $content = $token[$key]; + } + + $span = $this->_getStyling($the_class); + $decorated_output = $this->_decorate($content, $key); + + + if ($numbers_li == true) { + // end span tags before end of li, and re-open on next line + $lastSpanTag = str_replace("%s</span>", "", $span); + $span = sprintf($span, $decorated_output); + $span = str_replace("\n", "</span></li>\n<li>$lastSpanTag ", $span); + $html_output .= $span; + } else { + $html_output .= sprintf($span, $decorated_output); + } + + + } + + // format lists + if (!empty($this->_numbers) && $numbers_li == true) { + + + // additional whitespace for browsers that do not display + // empty list items correctly + $this->_output = '<li> ' . $html_output . '</li>'; + + $start = ''; + if ($this->_numbers == HL_NUMBERS_OL && intval($this->_numbers_start) > 0) { + $start = ' start="' . $this->_numbers_start . '"'; + } + + $list_tag = 'ol'; + if ($this->_numbers == HL_NUMBERS_UL) { + $list_tag = 'ul'; + } + + + $this->_output = '<' . $list_tag . $start + . ' ' . $this->_getStyling('main', false) . '>' + . $this->_output . '</'. $list_tag .'>'; + + // render a table + } else if ($this->_numbers == HL_NUMBERS_TABLE) { + + + $start_number = 0; + if (intval($this->_numbers_start)) { + $start_number = $this->_numbers_start - 1; + } + + $numbers = ''; + + $nlines = substr_count($html_output,"\n")+1; + for ($i=1; $i <= $nlines; $i++) { + $numbers .= ($start_number + $i) . "\n"; + } + $this->_output = '<table ' . $this->_getStyling('table', false) . ' width="100%"><tr>' . + '<td '. $this->_getStyling('gutter', false) .' align="right" valign="top">' . + '<pre>' . $numbers . '</pre></td><td '. $this->_getStyling('main', false) . + ' valign="top"><pre>' . + $html_output . '</pre></td></tr></table>'; + } + if (!$this->_numbers) { + $this->_output = '<pre>' . $html_output . '</pre>'; + } + $this->_output = '<div ' . $this->_getStyling('main', false) . '>' . $this->_output . '</div>'; + } + + + /** + * Provides additional formatting to a keyword + * + * @param string $content Keyword + * @return string Keyword with additional formatting + * @access public + * + */ + function _decorate($content, $key = false) + { + // links to online documentation + if (!empty($this->_doclinks) && + !empty($this->_doclinks['url']) && + in_array($key, $this->_doclinks['elements']) + ) { + + $link = '<a href="'. sprintf($this->_doclinks['url'], $content) . '"'; + if (!empty($this->_doclinks['target'])) { + $link.= ' target="' . $this->_doclinks['target'] . '"'; + } + $link .= '>'; + $link.= $content; + $link.= '</a>'; + + $content = $link; + + } + + return $content; + } + + /** + * Returns <code>class</code> and/or <code>style</code> attribute, + * optionally enclosed in a <code>span</code> tag + * + * @param string $class Class name + * @paran boolean $span_tag Whether or not to return styling attributes in a <code>>span<</code> tag + * @return string <code>span</code> tag or just a <code>class</code> and/or <code>style</code> attributes + * @access private + */ + function _getStyling($class, $span_tag = true) + { + $attrib = ''; + if (!empty($this->_style_map) && + !empty($this->_style_map[$class]) + ) { + $attrib = 'style="'. $this->_style_map[$class] .'"'; + } + if (!empty($this->_class_map) && + !empty($this->_class_map[$class]) + ) { + if ($attrib) { + $attrib .= ' '; + } + $attrib .= 'class="'. $this->_getFullClassName($this->_class_map[$class]) .'"'; + } + + if ($span_tag) { + $span = '<span ' . $attrib . '>%s</span>'; + return $span; + } else { + return $attrib; + } + + } +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * c-hanging-comment-ender-p: nil + * End: + */ + +?> diff --git a/library/Text_Highlighter/Text/Highlighter/Renderer/HtmlTags.php b/library/Text_Highlighter/Text/Highlighter/Renderer/HtmlTags.php new file mode 100644 index 000000000..68e01d3e0 --- /dev/null +++ b/library/Text_Highlighter/Text/Highlighter/Renderer/HtmlTags.php @@ -0,0 +1,187 @@ +<?php +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ +/** + * HTML renderer that uses only basic html tags + * + * PHP versions 4 and 5. Based on the "normal" HTML renderer by Andrey Demenev. + * It's designed to work with user agents that support only a limited number of + * HTML tags. Like the iPod which supports only b, i, u and a. + * + * LICENSE: This source file is subject to version 3.0 of the PHP license + * that is available through the world-wide-web at the following URI: + * http://www.php.net/license/3_0.txt. If you did not receive a copy of + * the PHP License and are unable to obtain it through the web, please + * send a note to license@php.net so we can mail you a copy immediately. + * + * @category Text + * @package Text_Highlighter + * @author Stoyan Stefanov <ssttoo@gmail.com> + * @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'; +require_once 'Text/Highlighter/Renderer/Array.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 <ssttoo@gmail.com> + * @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_Array +{ + + /**#@+ + * @access private + */ + + /** + * Line numbering - will use 'ol' tag + * + * @var boolean + */ + var $_numbers = false; + + /** + * HTML tags map + * + * @var array + */ + 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' => '', + ); + + /**#@-*/ + + /** + * Resets renderer state + * + * @access protected + * + * + * Descendents of Text_Highlighter call this method from the constructor, + * passing $options they get as parameter. + */ + function reset() + { + parent::reset(); + if (isset($this->_options['numbers'])) { + $this->_numbers = $this->_options['numbers']; + } + if (isset($this->_options['tags'])) { + $this->_hilite_tags = array_merge($this->_tags, $this->_options['tags']); + } + } + + + /** + * Signals that no more tokens are available + * + * @abstract + * @access public + * + */ + function finalize() + { + + // get parent's output + parent::finalize(); + $output = parent::getOutput(); + + $html_output = ''; + + // loop through each class=>content pair + foreach ($output AS $token) { + + if ($this->_enumerated) { + $class = $token[0]; + $content = $token[1]; + } else { + $key = key($token); + $class = $key; + $content = $token[$key]; + } + + $iswhitespace = ctype_space($content); + if (!$iswhitespace && !empty($this->_hilite_tags[$class])) { + $html_output .= '<'. $this->_hilite_tags[$class] . '>' . $content . '</'. $this->_hilite_tags[$class] . '>'; + } else { + $html_output .= $content; + } + } + + + if ($this->_numbers) { + /* additional whitespace for browsers that do not display + empty list items correctly */ + $html_output = '<li> ' . str_replace("\n", "</li>\n<li> ", $html_output) . '</li>'; + $this->_output = '<ol>' . str_replace(' ', ' ', $html_output) . '</ol>'; + } else { + $this->_output = '<pre>' . $html_output . '</pre>'; + } + } + + +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * c-hanging-comment-ender-p: nil + * End: + */ + +?> diff --git a/library/Text_Highlighter/Text/Highlighter/Renderer/JSON.php b/library/Text_Highlighter/Text/Highlighter/Renderer/JSON.php new file mode 100644 index 000000000..d4c926161 --- /dev/null +++ b/library/Text_Highlighter/Text/Highlighter/Renderer/JSON.php @@ -0,0 +1,86 @@ +<?php +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ +/** + * JSON renderer. + * + * Based on the HTML renderer by Andrey Demenev. + * + * LICENSE: This source file is subject to version 3.0 of the PHP license + * that is available through the world-wide-web at the following URI: + * http://www.php.net/license/3_0.txt. If you did not receive a copy of + * the PHP License and are unable to obtain it through the web, please + * send a note to license@php.net so we can mail you a copy immediately. + * + * @category Text + * @package Text_Highlighter + * @author Stoyan Stefanov <ssttoo@gmail.com> + * @copyright 2006 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'; +require_once 'Text/Highlighter/Renderer/Array.php'; + +/** + * JSON renderer, based on Andrey Demenev's HTML renderer. + * + * @author Stoyan Stefanov <ssttoo@gmail.com> + * @category Text + * @package Text_Highlighter + * @copyright 2006 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_JSON extends Text_Highlighter_Renderer_Array +{ + + /** + * Signals that no more tokens are available + * + * @abstract + * @access public + */ + function finalize() + { + + parent::finalize(); + $output = parent::getOutput(); + + $json_array = array(); + + foreach ($output AS $token) { + + if ($this->_enumerated) { + $json_array[] = '["' . $token[0] . '","' . $token[1] . '"]'; + } else { + $key = key($token); + $json_array[] = '{"class": "' . $key . '","content":"' . $token[$key] . '"}'; + } + + } + + $this->_output = '['. implode(',', $json_array) .']'; + $this->_output = str_replace("\n", '\n', $this->_output); + + } + + +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * c-hanging-comment-ender-p: nil + * End: + */ + +?>
\ No newline at end of file diff --git a/library/Text_Highlighter/Text/Highlighter/Renderer/XML.php b/library/Text_Highlighter/Text/Highlighter/Renderer/XML.php new file mode 100644 index 000000000..7b6fb2106 --- /dev/null +++ b/library/Text_Highlighter/Text/Highlighter/Renderer/XML.php @@ -0,0 +1,104 @@ +<?php +/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ +/** + * XML renderer. + * + * Based on the HTML renderer by Andrey Demenev. + * + * LICENSE: This source file is subject to version 3.0 of the PHP license + * that is available through the world-wide-web at the following URI: + * http://www.php.net/license/3_0.txt. If you did not receive a copy of + * the PHP License and are unable to obtain it through the web, please + * send a note to license@php.net so we can mail you a copy immediately. + * + * @category Text + * @package Text_Highlighter + * @author Stoyan Stefanov <ssttoo@gmail.com> + * @copyright 2006 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'; +require_once 'Text/Highlighter/Renderer/Array.php'; +require_once 'XML/Serializer.php'; + +/** + * XML renderer, based on Andrey Demenev's HTML renderer. + * + * @author Stoyan Stefanov <ssttoo@gmail.com> + * @category Text + * @package Text_Highlighter + * @copyright 2006 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_XML extends Text_Highlighter_Renderer_Array +{ + + + /** + * Options for XML_Serializer + * + * @access private + * @var array + */ + var $_serializer_options = array(); + + + /** + * Resets renderer state + * + * Descendents of Text_Highlighter call this method from the constructor, + * passing $options they get as parameter. + * + * @access protected + */ + function reset() + { + parent::reset(); + if (isset($this->_options['xml_serializer'])) { + $this->_serializer_options = $this->_options['xml_serializer']; + } + } + + + /** + * Signals that no more tokens are available + * + * @abstract + * @access public + */ + function finalize() + { + + // call parent's finalize(), then serialize array into XML + parent::finalize(); + $output = parent::getOutput(); + + $serializer = new XML_Serializer($this->_serializer_options); + $result = $serializer->serialize($output); + if ($result === true) { + $this->_output = $serializer->getSerializedData(); + } + } + + +} + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * c-hanging-comment-ender-p: nil + * End: + */ + +?> |