📄 geshi.php
字号:
/** * method: set_footer_content_style * -------------------------------- * Sets the style for the footer content */ function set_footer_content_style ( $style ) { $this->footer_content_style = $style; } /** * method: set_url_for_keyword_group * --------------------------------- * Sets the base URL to be used for keywords */ function set_url_for_keyword_group ( $group, $url ) { $this->language_data['URLS'][$group] = $url; } /** * method: set_link_styles * ----------------------- * Sets styles for links in code */ function set_link_styles ( $type, $styles ) { $this->link_styles[$type] = $styles; } /** * method: set_link_target * ----------------------- * Sets the target for links in code */ function set_link_target ( $target ) { if ( empty( $target ) ) { $this->link_target = ''; } else { $this->link_target = ' target="' . $target . '" '; } } /** * method: set_important_styles * ---------------------------- * Sets styles for important parts of the code */ function set_important_styles ( $styles ) { $this->important_styles = $styles; } /** * method: enable_important_blocks * ------------------------------- * Sets whether context-important blocks are highlighted */ function enable_important_blocks ( $flag ) { $this->enable_important_blocks = ( $flag ) ? true : false; } /** * method: enable_ids * ------------------ * Whether CSS IDs should be added to each line */ function enable_ids ( $flag = true ) { $this->add_ids = ( $flag ) ? true : false; } /** * method: highlight_lines_extra * ----------------------------- * Specifies which lines to highlight extra */ function highlight_lines_extra ( $lines ) { if ( is_array($lines) ) { foreach ( $lines as $line ) { $this->highlight_extra_lines[intval($line)] = intval($line); } } else { $this->highlight_extra_lines[intval($lines)] = intval($lines); } } /** * method: set_highlight_lines_extra_style * --------------------------------------- * Sets the style for extra-highlighted lines */ function set_highlight_lines_extra_style ( $styles ) { $this->highlight_extra_lines_style = $styles; } /** * method: start_line_numbers_at * ----------------------------- * Sets what number line numbers should start at. Should * be a positive integer, and will be converted to one. */ function start_line_numbers_at ( $number ) { $this->line_numbers_start = abs(intval($number)); } /** * method: set_encoding * -------------------- * Sets the encoding used for htmlentities(), for international * support. */ function set_encoding ( $encoding ) { $this->encoding = $encoding; } /** * method: parse_code() * -------------------- * Returns the code in $this->source, highlighted and surrounded by the * nessecary HTML. This should only be called ONCE, cos it's SLOW! * If you want to highlight the same source multiple times, you're better * off doing a whole lot of str_replaces to replace the <span>s */ function parse_code() { // Start the timer $start_time = microtime(); // Firstly, if there is an error, we won't highlight // FUTURE: maybe an option to try to force highlighting anyway? if ( $this->error ) { $result = $this->header(); if ( $this->header_type != GESHI_HEADER_PRE ) { $result .= $this->indent(htmlentities($this->source, ENT_COMPAT, $this->encoding)); } else { $result .= htmlentities($this->source, ENT_COMPAT, $this->encoding); } // Stop Timing $this->set_time($start_time, microtime()); return $result . $this->footer(); } // Add spaces for regular expression matching and line numbers $code = ' ' . $this->source . ' '; // Replace all newlines to a common form. $code = str_replace("\r\n", "\n", $code); $code = str_replace("\r", "\n", $code); // Initialise various stuff $length = strlen($code); $STRING_OPEN = ''; $CLOSE_STRING = false; $ESCAPE_CHAR_OPEN = false; $COMMENT_MATCHED = false; // Turn highlighting on if strict mode doesn't apply to this language $HIGHLIGHTING_ON = ( !$this->strict_mode ) ? true : ''; // Whether to highlight inside a block of code $HIGHLIGHT_INSIDE_STRICT = false; $stuff_to_parse = ''; $result = ''; // "Important" selections are handled like multiline comments if ( $this->enable_important_blocks ) { $this->language_data['COMMENT_MULTI'][GESHI_START_IMPORTANT] = GESHI_END_IMPORTANT; } if ( $this->strict_mode ) { // Break the source into bits. Each bit will be a portion of the code // within script delimiters - for example, HTML between < and > $parts = array(0 => array(0 => '')); $k = 0; for ( $i = 0; $i < $length; $i++ ) { $char = substr($code, $i, 1); if ( !$HIGHLIGHTING_ON ) { foreach ( $this->language_data['SCRIPT_DELIMITERS'] as $key => $delimiters ) { foreach ( $delimiters as $open => $close ) { // Get the next little bit for this opening string $check = substr($code, $i, strlen($open)); // If it matches... if ( $check == $open ) { // We start a new block with the highlightable // code in it $HIGHLIGHTING_ON = $open; $i += strlen($open) - 1; ++$k; $char = $open; $parts[$k][0] = $char; // No point going around again... break(2); } } } } else { foreach ( $this->language_data['SCRIPT_DELIMITERS'] as $key => $delimiters ) { foreach ( $delimiters as $open => $close ) { if ( $open == $HIGHLIGHTING_ON ) { // Found the closing tag break(2); } } } // We check code from our current position BACKWARDS. This is so // the ending string for highlighting can be included in the block $check = substr($code, $i - strlen($close) + 1, strlen($close)); if ( $check == $close ) { $HIGHLIGHTING_ON = ''; // Add the string to the rest of the string for this part $parts[$k][1] = ( isset($parts[$k][1]) ) ? $parts[$k][1] . $char : $char; ++$k; $parts[$k][0] = ''; $char = ''; } } $parts[$k][1] = ( isset($parts[$k][1]) ) ? $parts[$k][1] . $char : $char; } $HIGHLIGHTING_ON = ''; } else { // Not strict mode - simply dump the source into // the array at index 1 (the first highlightable block) $parts = array( 1 => array( 0 => '', 1 => $code ) ); } // Now we go through each part. We know that even-indexed parts are // code that shouldn't be highlighted, and odd-indexed parts should // be highlighted foreach ( $parts as $key => $data ) { $part = $data[1]; // If this block should be highlighted... if ( $key % 2 ) { if ( $this->strict_mode ) { // Find the class key for this block of code foreach ( $this->language_data['SCRIPT_DELIMITERS'] as $script_key => $script_data ) { foreach ( $script_data as $open => $close ) { if ( $data[0] == $open ) { break(2); } } } if ( $this->language_data['STYLES']['SCRIPT'][$script_key] != '' && $this->lexic_permissions['SCRIPT'] ) { // Add a span element around the source to // highlight the overall source block if ( !$this->use_classes && $this->language_data['STYLES']['SCRIPT'][$script_key] != '' ) { $attributes = ' style="' . $this->language_data['STYLES']['SCRIPT'][$script_key] . '"'; } else { $attributes = ' class="sc' . $script_key . '"'; } $result .= "<span$attributes>"; } } if ( !$this->strict_mode || $this->language_data['HIGHLIGHT_STRICT_BLOCK'][$script_key] ) { // Now, highlight the code in this block. This code // is really the engine of GeSHi (along with the method // parse_non_string_part). $length = strlen($part); for ( $i = 0; $i < $length; $i++ ) { // Get the next char $char = substr($part, $i, 1); // Is this char the newline and line numbers being used? if ( ($this->line_numbers != GESHI_NO_LINE_NUMBERS || count($this->highlight_extra_lines) > 0) && $char == "\n" ) { // If so, is there a string open? If there is, we should end it before // the newline and begin it again (so when <li>s are put in the source // remains XHTML compliant) // NOTE TO SELF: This opens up possibility of config files specifying // that languages can/cannot have multiline strings??? if ( $STRING_OPEN ) { if ( !$this->use_classes ) { $attributes = ' style="' . $this->language_data['STYLES']['STRINGS'][0] . '"'; } else { $attributes = ' class="st0"'; } $char = '</span>' . $char . "<span$attributes>"; } } // Is this a match of a string delimiter? elseif ( $char == $STRING_OPEN ) { if ( ($this->lexic_permissions['ESCAPE_CHAR'] && $ESCAPE_CHAR_OPEN) || ($this->lexic_permissions['STRINGS'] && !$ESCAPE_CHAR_OPEN) ) { $char .= '</span>'; } if ( !$ESCAPE_CHAR_OPEN ) { $STRING_OPEN = ''; $CLOSE_STRING = true; } $ESCAPE_CHAR_OPEN = false; } // Is this the start of a new string? elseif ( in_array( $char, $this->language_data['QUOTEMARKS'] ) && ($STRING_OPEN == '') && $this->lexic_permissions['STRINGS'] ) { $STRING_OPEN = $char; if ( !$this->use_classes ) { $attributes = ' style="' . $this->language_data['STYLES']['STRINGS'][0] . '"'; } else { $attributes = ' class="st0"'; } $char = "<span$attributes>" . $char; $result .= $this->parse_non_string_part( $stuff_to_parse ); $stuff_to_parse = ''; } // Is this an escape char? elseif ( ($char == $this->language_data['ESCAPE_CHAR']) && ($STRING_OPEN != '') ) { if ( !$ESCAPE_CHAR_OPEN ) { $ESCAPE_CHAR_OPEN = true; if ( $this->lexic_permissions['ESCAPE_CHAR'] ) { if ( !$this->use_classes ) { $attributes = ' style="' . $this->language_data['STYLES']['ESCAPE_CHAR'][0] . '"'; } else { $attributes = ' class="es0"'; } $char = "<span$attributes>" . $char; } } else { $ESCAPE_CHAR_OPEN = false; if ( $this->lexic_permissions['ESCAPE_CHAR'] ) { $char .= '</span>'; } } } elseif ( $ESCAPE_CHAR_OPEN ) { if ( $this->lexic_permissions['ESCAPE_CHAR'] ) { $char .= '</span>'; } $ESCAPE_CHAR_OPEN = false; $test_str = $char; } elseif ( $STRING_OPEN == '' ) { // Is this a multiline comment? foreach ( $this->language_data['COMMENT_MULTI'] as $open => $close ) { $com_len = strlen($open); $test_str = substr( $part, $i, $com_len ); $test_str_match = $test_str; if ( $open == $test_str ) { $COMMENT_MATCHED = true; if ( $this->lexic_permissions['COMMENTS']['MULTI'] || $test_str == GESHI_START_IMPORTANT ) { if ( $test_str != GESHI_START_IMPORTANT ) { if ( !$this->use_classes ) { $attributes = ' style="' . $this->language_data['STYLES']['COMMENTS']['MULTI'] . '"'; } else { $attributes = ' class="coMULTI"'; } $test_str = "<span$attributes>" . htmlentities($test_str, ENT_COMPAT, $this->encoding); } else { if ( !$this->use_classes ) { $attributes = ' style="' . $this->important_styles . '"'; } else { $attributes = ' class="imp"'; } // We don't include the start of the comment if it's an // "important" part $test_str = "<span$attributes>"; } } else { $test_str = htmlentities($test_str, ENT_COMPAT, $this->encoding); } $close_pos = strpos( $part, $close, $i + strlen($close) ); if ( $close_pos === false ) { $close_pos = strlen($part); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -