📄 geshi.php
字号:
// Perhaps some checking might be added here later to check that // $language data is a valid thing but maybe not $this->language_data = $language_data; // Set strict mode if should be set if ( $this->language_data['STRICT_MODE_APPLIES'] == GESHI_ALWAYS ) { $this->strict_mode = true; } // Set permissions for all lexics to true // so they'll be highlighted by default $this->enable_highlighting(); // Set default class for CSS $this->overall_class = $this->language; } /** * method: get_tab_replacement * --------------------------- * Gets the replacement string for tabs in the source code. Useful for * HTML highlighting, where tabs don't mean anything to a browser. */ function get_tab_replacement () { $i = 0; $result = ''; while ( $i < $this->tab_width ) { $i++; if ( $i % 2 == 0 ) { $result .= ' '; } else { $result .= ' '; } } return $result; } /** * method: finalise * ---------------- * Takes the parsed code and various options, and creates the HTML * surrounding it to make it look nice. */ function finalise ( $parsed_code ) { // Remove end parts of important declarations // This is BUGGY!! My fault for bad code: fix coming in 1.2 if ( $this->enable_important_blocks && (strstr($parsed_code, htmlentities(GESHI_START_IMPORTANT, ENT_COMPAT, $this->encoding)) === false) ) { $parsed_code = str_replace(htmlentities(GESHI_END_IMPORTANT, ENT_COMPAT, $this->encoding), '', $parsed_code); } // Add HTML whitespace stuff if we're using the <div> header if ( $this->header_type == GESHI_HEADER_DIV ) { $parsed_code = $this->indent($parsed_code); } // If we're using line numbers, we insert <li>s and appropriate // markup to style them (otherwise we don't need to do anything) if ( $this->line_numbers != GESHI_NO_LINE_NUMBERS ) { // If we're using the <pre> header, we shouldn't add newlines because // the <pre> will line-break them (and the <li>s already do this for us) $ls = ( $this->header_type != GESHI_HEADER_PRE ) ? "\n" : ''; // Get code into lines $code = explode("\n", $parsed_code); // Set vars to defaults for following loop $parsed_code = ''; $i = 0; // Foreach line... foreach ( $code as $line ) { $line = ( $line ) ? $line : ' '; // If this is a "special line"... if ( $this->line_numbers == GESHI_FANCY_LINE_NUMBERS && $i % $this->line_nth_row == ($this->line_nth_row - 1) ) { // Set the attributes to style the line if ( $this->use_classes ) { $attr = ' class="li2"'; $def_attr = ' class="de2"'; } else { $attr = ' style="' . $this->line_style2 . '"'; // This style "covers up" the special styles set for special lines // so that styles applied to special lines don't apply to the actual // code on that line $def_attr = ' style="' . $this->code_style . '"'; } // Span or div? $start = "<div$def_attr>"; $end = '</div>'; } else { if ( $this->use_classes ) { $def_attr = ' class="de1"'; } else { $def_attr = ' style="' . $this->code_style . '"'; } // Reset everything $attr = ''; // Span or div? $start = "<div$def_attr>"; $end = '</div>'; } ++$i; // Are we supposed to use ids? If so, add them if ( $this->add_ids ) { $attr .= " id=\"{$this->overall_id}-{$i}\""; } if ( $this->use_classes && in_array($i, $this->highlight_extra_lines) ) { $attr .= " class=\"ln-xtra\""; } if ( !$this->use_classes && in_array($i, $this->highlight_extra_lines) ) { $attr .= " style=\"{$this->highlight_extra_lines_style}\""; } // Add in the line surrounded by appropriate list HTML $parsed_code .= "<li$attr>$start$line$end</li>$ls"; } } else { // No line numbers, but still need to handle highlighting lines extra. // Have to use divs so the full width of the code is highlighted $code = explode("\n", $parsed_code); $parsed_code = ''; $i = 0; foreach ( $code as $line ) { // Make lines have at least one space in them if they're empty $line = ( $line ) ? $line : ' '; if ( in_array(++$i, $this->highlight_extra_lines) ) { if ( $this->use_classes ) { //$id = ( $this->overall_id != '' ) ? $this->overall_id . "-$i" : $this->overall_class . "-$i"; $parsed_code .= '<div class="ln-xtra">'; } else { $parsed_code .= "<div style=\"{$this->highlight_extra_lines_style}\">"; } $parsed_code .= $line . "</div>\n"; } else { $parsed_code .= $line . "\n"; } } } return $this->header() . chop($parsed_code) . $this->footer(); } /** * method: header * -------------- * Creates the header for the code block (with correct attributes) */ function header () { // Get attributes needed $attributes = $this->get_attributes(); if ( $this->use_classes ) { $ol_attributes = ''; } else { $ol_attributes = ' style="margin: 0;"'; } if ( $this->line_numbers_start != 1 ) { $ol_attributes .= ' start="' . $this->line_numbers_start . '"'; } // Get the header HTML $header = $this->format_header_content(); // Work out what to return and do it if ( $this->line_numbers != GESHI_NO_LINE_NUMBERS ) { if ( $this->header_type == GESHI_HEADER_PRE ) { return "<pre$attributes>$header<ol$ol_attributes>"; } elseif ( $this->header_type == GESHI_HEADER_DIV ) { return "<div$attributes>$header<ol$ol_attributes>"; } } else { if ( $this->header_type == GESHI_HEADER_PRE ) { return "<pre$attributes>$header"; } elseif ( $this->header_type == GESHI_HEADER_DIV ) { return "<div$attributes>$header"; } } } /** * method: format_header_content * ----------------------------- * Returns the header content, formatted for output */ function format_header_content () { $header = $this->header_content; if ( $header ) { if ( $this->header_type == GESHI_HEADER_PRE ) { $header = str_replace("\n", '', $header); } $header = $this->replace_keywords($header); if ( $this->use_classes ) { $attr = ' class="head"'; } else { $attr = " style=\"{$this->header_content_style}\""; } return "<div$attr>$header</div>"; } } /** * method: footer * -------------- * Returns the footer for the code block. Ending newline removed in 1.0.2 */ function footer () { $footer_content = $this->format_footer_content(); if ( $this->header_type == GESHI_HEADER_DIV ) { if ( $this->line_numbers != GESHI_NO_LINE_NUMBERS ) { return "</ol>$footer_content</div>"; } return "$footer_content</div>"; } else { if ( $this->line_numbers != GESHI_NO_LINE_NUMBERS ) { return "</ol>$footer_content</pre>"; } return "$footer_content</pre>"; } } /** * method: format_footer_content * ----------------------------- * Returns the footer content, formatted for output */ function format_footer_content () { $footer = $this->footer_content; if ( $footer ) { if ( $this->header_type == GESHI_HEADER_PRE ) { $footer = str_replace("\n", '', $footer);; } $footer = $this->replace_keywords($footer); if ( $this->use_classes ) { $attr = ' class="foot"'; } else { $attr = " style=\"{$this->footer_content_style}\">"; } return "<div$attr>$footer</div>"; } } /** * method: replace_keywords * ---------------------- * Replaces certain keywords in the header and footer with * certain configuration values */ function replace_keywords ( $instr ) { $keywords = $replacements = array(); $keywords[] = '<TIME>'; $replacements[] = number_format($this->get_time(), 3); $keywords[] = '<LANGUAGE>'; $replacements[] = $this->language; $keywords[] = '<VERSION>'; $replacements[] = '1.0.4'; return str_replace($keywords, $replacements, $instr); } /** * method: get_attributes * ---------------------- * Gets the CSS attributes for this code */ function get_attributes () { $attributes = ''; if ( $this->overall_class != '' && $this->use_classes ) { $attributes .= " class=\"{$this->overall_class}\""; } if ( $this->overall_id != '' ) { $attributes .= " id=\"{$this->overall_id}\""; } if ( $this->overall_style != '' && !$this->use_classes ) { $attributes .= ' style="' . $this->overall_style . '"'; } return $attributes; } /** * method: get_stylesheet * ---------------------- * Returns a stylesheet for the highlighted code. If $economy mode * is true, we only return the stylesheet declarations that matter for * this code block instead of the whole thing */ function get_stylesheet ( $economy_mode = true ) { // If there's an error, chances are that the language file // won't have populated the language data file, so we can't // risk getting a stylesheet... if ( $this->error ) { return ''; } // First, work out what the selector should be. If there's an ID, // that should be used, the same for a class. Otherwise, a selector // of '' means that these styles will be applied anywhere $selector = ( $this->overall_id != '' ) ? "#{$this->overall_id} " : ''; $selector = ( $selector == '' && $this->overall_class != '' ) ? ".{$this->overall_class} " : $selector; // Header of the stylesheet if ( !$economy_mode ) { $stylesheet = "/**\n * GeSHi Dynamically Generated Stylesheet\n * --------------------------------------\n * Dynamically generated stylesheet for {$this->language}\n * CSS class: {$this->overall_class}, CSS id: {$this->overall_id}\n * GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter)\n */\n"; } else { $stylesheet = '/* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */' . "\n"; } // Set the <ol> to have no effect at all if there are line numbers // (<ol>s have margins that should be destroyed so all layout is // controlled by the set_overall_style method, which works on the // <pre> or <div> container). Additionally, set default styles for lines if ( !$economy_mode || $this->line_numbers != GESHI_NO_LINE_NUMBERS ) { $stylesheet .= "$selector, {$selector}ol, {$selector}ol li {margin: 0;}\n"; $stylesheet .= "$selector.de1, $selector.de2 {{$this->code_style}}\n"; } // Add overall styles if ( !$economy_mode || $this->overall_style != '' ) { $stylesheet .= "$selector {{$this->overall_style}}\n"; } // Add styles for links foreach ( $this->link_styles as $key => $style ) { if ( !$economy_mode || $key == GESHI_LINK && $style != '' ) { $stylesheet .= "{$selector}a:link {{$style}}\n"; } if ( !$economy_mode || $key == GESHI_HOVER && $style != '' ) { $stylesheet .= "{$selector}a:hover {{$style}}\n"; } if ( !$economy_mode || $key == GESHI_ACTIVE && $style != '' ) { $stylesheet .= "{$selector}a:active {{$style}}\n"; } if ( !$economy_mode || $key == GESHI_VISITED && $style != '' ) { $stylesheet .= "{$selector}a:visited {{$style}}\n"; } } // Header and footer if ( !$economy_mode || $this->header_content_style != '' ) { $stylesheet .= "$selector.head {{$this->header_content_style}}\n"; } if ( !$economy_mode || $this->footer_content_style != '' ) { $stylesheet .= "$selector.foot {{$this->footer_content_style}}\n"; } // Styles for important stuff if ( !$economy_mode || $this->important_styles != '' ) { $stylesheet .= "$selector.imp {{$this->important_styles}}\n"; } // Styles for lines being highlighted extra if ( !$economy_mode || count($this->highlight_extra_lines) ) { /*foreach ( $this->highlight_extra_lines as $line ) { $id = ( $this->overall_id != '' ) ? $this->overall_id . "-$line" : $this->overall_class . "-$line"; $stylesheet .= "$selector#$id,"; }*/ $stylesheet .= "$selector.ln-xtra {{$this->highlight_extra_lines_style}}\n"; } // Simple line number styles if ( !$economy_mode || ($this->line_numbers != GESHI_NO_LINE_NUMBERS && $this->line_style1 != '') ) { $stylesheet .= "{$selector}li {{$this->line_style1}}\n"; } // If there is a style set for fancy line numbers, echo it out if ( !$economy_mode || ($this->line_numbers == GESHI_FANCY_LINE_NUMBERS && $this->line_style2 != '') ) { $stylesheet .= "{$sel
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -