From 14e7550de1640180bad222469bbe117319313c74 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Mon, 2 Jul 2007 22:07:15 +0200 Subject: GeSHi update to 1.0.7.20 darcs-hash:20070702200715-7ad00-654e2eb59ffaa2d72e907cd7aea5237a903727fa.gz --- inc/geshi.php | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 96 insertions(+), 15 deletions(-) (limited to 'inc/geshi.php') diff --git a/inc/geshi.php b/inc/geshi.php index 358b7a8e2..832b62efe 100644 --- a/inc/geshi.php +++ b/inc/geshi.php @@ -41,7 +41,7 @@ // /** The version of this GeSHi file */ -define('GESHI_VERSION', '1.0.7.19'); +define('GESHI_VERSION', '1.0.7.20'); // Define the root directory for the GeSHi code tree if (!defined('GESHI_ROOT')) { @@ -272,6 +272,13 @@ class GeSHi { */ var $footer_content_style = ''; + /** + * Tells if a block around the highlighted source should be forced + * if not using line numbering + * @var boolean + */ + var $force_code_block = false; + /** * The styles for hyperlinks in the code * @var array @@ -313,6 +320,14 @@ class GeSHi { */ var $highlight_extra_lines_style = 'color: #cc0; background-color: #ffc;'; + /** + * The line ending + * If null, nl2br() will be used on the result string. + * Otherwise, all instances of \n will be replaced with $line_ending + * @var string + */ + var $line_ending = null; + /** * Number at which line numbers should start at * @var int @@ -373,6 +388,12 @@ class GeSHi { */ var $tab_width = 8; + /** + * Should we use language-defined tab stop widths? + * @var int + */ + var $use_language_tab_width = false; + /** * Default target for keyword links * @var string @@ -973,7 +994,35 @@ class GeSHi { */ function set_tab_width($width) { $this->tab_width = intval($width); - } + //Check if it fit's the constraints: + if($this->tab_width < 1) { + //Return it to the default + $this->tab_width = 8; + } + } + + /** + * Sets whether or not to use tab-stop width specifed by language + * + * @param boolean Whether to use language-specific tab-stop widths + */ + function set_use_language_tab_width($use) { + $this->use_language_tab_width = (bool) $use; + } + + /** + * Returns the tab width to use, based on the current language and user + * preference + * + * @return int Tab width + */ + function get_real_tab_width() { + if (!$this->use_language_tab_width || !isset($this->language_data['TAB_WIDTH'])) { + return $this->tab_width; + } else { + return $this->language_data['TAB_WIDTH']; + } + } /** * Enables/disables strict highlighting. Default is off, calling this @@ -1212,6 +1261,17 @@ class GeSHi { $this->footer_content_style = $style; } + /** + * Sets whether to force a surrounding block around + * the highlighted code or not + * + * @param boolean Tells whether to enable or disable this feature + * @since 1.0.7.20 + */ + function enable_inner_code_block($flag) { + $this->force_code_block = (bool)$flag; + } + /** * Sets the base URL to be used for keywords * @@ -1311,6 +1371,15 @@ class GeSHi { $this->highlight_extra_lines_style = $styles; } + /** + * Sets the line-ending + * + * @param string The new line-ending + */ + function set_line_ending($line_ending) { + $this->line_ending = (string)$line_ending; + } + /** * Sets what number line numbers should start at. Should * be a positive integer, and will be converted to one. @@ -1686,7 +1755,10 @@ class GeSHi { ($this->line_numbers != GESHI_NO_LINE_NUMBERS || count($this->highlight_extra_lines) > 0)) { // strreplace to put close span and open span around multiline newlines - $test_str .= str_replace("\n", "\n", $rest_of_comment); + $test_str .= str_replace( + "\n", "\n", + str_replace("\n ", "\n ", $rest_of_comment) + ); } else { $test_str .= $rest_of_comment; @@ -1699,7 +1771,7 @@ class GeSHi { $test_str .= "\n"; } } - $i = $close_pos + $com_len - 1; + $i = $close_pos + $com_len - 1; // parse the rest $result .= $this->parse_non_string_part($stuff_to_parse); $stuff_to_parse = ''; @@ -1835,6 +1907,7 @@ class GeSHi { /// Replace tabs with the correct number of spaces if (false !== strpos($result, "\t")) { $lines = explode("\n", $result); + $tab_width = $this->get_real_tab_width(); foreach ($lines as $key => $line) { if (false === strpos($line, "\t")) { $lines[$key] = $line; @@ -1842,7 +1915,6 @@ class GeSHi { } $pos = 0; - $tab_width = $this->tab_width; $length = strlen($line); $result_line = ''; @@ -1886,7 +1958,7 @@ class GeSHi { $strs = array(0 => ' ', 1 => ' '); for ($k = 0; $k < ($tab_width - (($i - $pos) % $tab_width)); $k++) $str .= $strs[$k % 2]; $result_line .= $str; - $pos++; + $pos += ($i - $pos) % $tab_width + 1; if (false === strpos($line, "\t", $i + 1)) { $result_line .= substr($line, $i + 1); @@ -1907,13 +1979,17 @@ class GeSHi { $result = implode("\n", $lines); } // Other whitespace - $result = str_replace(' ', '  ', $result); - $result = str_replace(' ', '  ', $result); + // BenBE: Fix to reduce the number of replacements to be done $result = str_replace("\n ", "\n ", $result); + $result = str_replace(' ', '  ', $result); if ($this->line_numbers == GESHI_NO_LINE_NUMBERS) { - $result = nl2br($result); - } + if ($this->line_ending === null) { + $result = nl2br($result); + } else { + $result = str_replace("\n", $this->line_ending, $result); + } + } return $result; } @@ -2422,7 +2498,8 @@ class GeSHi { if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) { return "$header"; } - return $header; + return $header . + ($this->force_code_block ? '
' : ''); } // Work out what to return and do it @@ -2436,10 +2513,12 @@ class GeSHi { } else { if ($this->header_type == GESHI_HEADER_PRE) { - return "$header"; + return "$header" . + ($this->force_code_block ? '
' : ''); } else if ($this->header_type == GESHI_HEADER_DIV) { - return "$header"; + return "$header" . + ($this->force_code_block ? '
' : ''); } } } @@ -2488,13 +2567,15 @@ class GeSHi { if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) { return "$footer_content
"; } - return "$footer_content
"; + return ($this->force_code_block ? '
' : '') . + "$footer_content"; } else { if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) { return "$footer_content"; } - return "$footer_content"; + return ($this->force_code_block ? '' : '') . + "$footer_content"; } } -- cgit v1.2.3