array( + * 'lang_name' => array('extension', 'extension', ...), + * 'lang_name' ... + * );+ * + * @todo Complete rethink of this and above method + * @since 1.0.5 + */ + function load_from_file ($file_name, $lookup = array()) + { + if (is_readable($file_name)) { + $this->set_source(implode('', file($file_name))); + $this->set_language($this->get_language_name_from_extension(substr(strrchr($file_name, '.'), 1), $lookup)); + } else { + $this->error = GESHI_ERROR_FILE_NOT_READABLE; + } + } + + /** + * Adds a keyword to a keyword group for highlighting + * + * @param int The key of the keyword group to add the keyword to + * @param string The word to add to the keyword group + * @since 1.0.0 + */ + function add_keyword ($key, $word) + { + $this->language_data['KEYWORDS'][$key][] = $word; + } + + /** + * Removes a keyword from a keyword group + * + * @param int The key of the keyword group to remove the keyword from + * @param string The word to remove from the keyword group + * @since 1.0.0 + */ + function remove_keyword ($key, $word) + { + $this->language_data['KEYWORDS'][$key] = + array_diff($this->language_data['KEYWORDS'][$key], array($word)); + } + + /** + * Creates a new keyword group + * + * @param int The key of the keyword group to create + * @param string The styles for the keyword group + * @param boolean Whether the keyword group is case sensitive ornot + * @param array The words to use for the keyword group + * @since 1.0.0 + */ + function add_keyword_group ( $key, $styles, $case_sensitive = true, $words = array() ) + { + $words = (array) $words; + $this->language_data['KEYWORDS'][$key] = $words; + $this->lexic_permissions['KEYWORDS'][$key] = true; + $this->language_data['CASE_SENSITIVE'][$key] = $case_sensitive; + $this->language_data['STYLES']['KEYWORDS'][$key] = $styles; + } + + /** + * Removes a keyword group + * + * @param int The key of the keyword group to remove + * @since 1.0.0 + */ + function remove_keyword_group ($key) + { + unset($this->language_data['KEYWORDS'][$key]); + unset($this->lexic_permissions['KEYWORDS'][$key]); + unset($this->language_data['CASE_SENSITIVE'][$key]); + unset($this->language_data['STYLES']['KEYWORDS'][$key]); + } + + /** + * Sets the content of the header block + * + * @param string The content of the header block + * @since 1.0.2 + */ + function set_header_content ($content) + { + $this->header_content = $content; + } + + /** + * Sets the content of the footer block + * + * @param string The content of the footer block + * @since 1.0.2 + */ + function set_footer_content ($content) + { + $this->footer_content = $content; + } + + /** + * Sets the style for the header content + * + * @param string The style for the header content + * @since 1.0.2 + */ + function set_header_content_style ($style) + { + $this->header_content_style = $style; + } + + /** + * Sets the style for the footer content + * + * @param string The style for the footer content + * @since 1.0.2 + */ + function set_footer_content_style ($style) + { + $this->footer_content_style = $style; + } + + /** + * Sets the base URL to be used for keywords + * + * @param int The key of the keyword group to set the URL for + * @param string The URL to set for the group. If {FNAME} is in + * the url somewhere, it is replaced by the keyword + * that the URL is being made for + * @since 1.0.2 + */ + function set_url_for_keyword_group ($group, $url) + { + $this->language_data['URLS'][$group] = $url; + } + + /** + * Sets styles for links in code + * + * @param int A constant that specifies what state the style is being + * set for - e.g. :hover or :visited + * @param string The styles to use for that state + * @since 1.0.2 + */ + function set_link_styles ($type, $styles) + { + $this->link_styles[$type] = $styles; + } + + /** + * Sets the target for links in code + * + * @param string The target for links in the code, e.g. _blank + * @since 1.0.3 + */ + function set_link_target ( $target ) + { + if (!$target) { + $this->link_target = ''; + } else { + $this->link_target = ' target="' . $target . '" '; + } + } + + /** + * Sets styles for important parts of the code + * + * @param string The styles to use on important parts of the code + * @since 1.0.2 + */ + function set_important_styles ($styles) + { + $this->important_styles = $styles; + } + + /** + * Sets whether context-important blocks are highlighted + * + * @todo REMOVE THIS SHIZ FROM GESHI! + * @deprecated + */ + function enable_important_blocks ( $flag ) + { + $this->enable_important_blocks = ( $flag ) ? true : false; + } + + /** + * Whether CSS IDs should be added to each line + * + * @param boolean If true, IDs will be added to each line. + * @since 1.0.2 + */ + function enable_ids ($flag = true) + { + $this->add_ids = ($flag) ? true : false; + } + + /** + * Specifies which lines to highlight extra + * + * @param mixed An array of line numbers to highlight, or just a line + * number on its own. + * @since 1.0.2 + * @todo Some data replication here that could be cut down on + */ + 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); + } + } + + /** + * Sets the style for extra-highlighted lines + * + * @param string The style for extra-highlighted lines + * @since 1.0.2 + */ + function set_highlight_lines_extra_style ($styles) + { + $this->highlight_extra_lines_style = $styles; + } + + /** + * Sets what number line numbers should start at. Should + * be a positive integer, and will be converted to one. + * + * Warning: Using this method will add the "start" + * attribute to the <ol> that is used for line numbering. + * This is not valid XHTML strict, so if that's what you + * care about then don't use this method. Firefox is getting + * support for the CSS method of doing this in 1.1 and Opera + * has support for the CSS method, but (of course) IE doesn't + * so it's not worth doing it the CSS way yet. + * + * @param int The number to start line numbers at + * @since 1.0.2 + */ + function start_line_numbers_at ($number) + { + $this->line_numbers_start = abs(intval($number)); + } + + /** + * Sets the encoding used for htmlspecialchars(), for international + * support. + * + * @param string The encoding to use for the source + * @since 1.0.3 + */ + function set_encoding ($encoding) + { + if ($encoding) { + $this->encoding = $encoding; + } + } + + /** + * 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 + * + * @since 1.0.0 + */ + function parse_code () + { + // Start the timer + $start_time = microtime(); + + // Firstly, if there is an error, we won't highlight + if ($this->error) { + $result = @htmlspecialchars($this->source, ENT_COMPAT, $this->encoding); + // Timing is irrelevant + $this->set_time($start_time, $start_time); + return $this->finalise($result); + } + + // Replace all newlines to a common form. + $code = str_replace("\r\n", "\n", $this->source); + $code = str_replace("\r", "\n", $code); + // Add spaces for regular expression matching and line numbers + $code = "\n" . $code . "\n"; + + // 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; + $HARDQUOTE_OPEN = false; + $stuff_to_parse = ''; + $result = ''; + + // "Important" selections are handled like multiline comments + // @todo GET RID OF THIS SHIZ + 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; + $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; + $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 .= ""; + } + } + + 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); + $hq = isset($this->language_data['HARDQUOTE']) ? $this->language_data['HARDQUOTE'][0] : false; + // 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
header, we shouldn't add newlines because + // thewill line-break them (and the
header + $parsed_code .= $line . "
$header"; + } + return "$footer_content"; + } + } + + /** + * Returns the footer content, formatted for output + * + * @return string The footer content, formatted for output + * @since 1.0.2 + * @access private + */ + 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 ""; + } elseif ($this->header_type == GESHI_HEADER_DIV) { + return "
$footer_content$header"; + } else { + if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) { + return ""; + } + } else { + if ($this->header_type == GESHI_HEADER_PRE) { + return "
$header"; + } elseif ($this->header_type == GESHI_HEADER_DIV) { + return "$header"; + } + } + } + + /** + * Returns the header content, formatted for output + * + * @return string The header content, formatted for output + * @since 1.0.2 + * @access private + */ + 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 ""; + } + return "$footer_content$header"; + } + } + + /** + * Returns the footer for the code block. + * + * @return string The footer for the code block + * @since 1.0.0 + * @access private + */ + function footer () + { + $footer_content = $this->format_footer_content(); + + if (GESHI_HEADER_NONE == $this->header_type) { + return ($this->line_numbers != GESHI_NO_LINE_NUMBERS) ? '' . $footer_content + : $footer_content; + } + + if ($this->header_type == GESHI_HEADER_DIV) { + if ($this->line_numbers != GESHI_NO_LINE_NUMBERS) { + return "$footer_content
';
+
+ $html .= 'function ' . $full . ' ... end';
+
+ $html .= '
';
+ $html .= '
+
+
+
+
+
+HEADER;
+
+
+
+$HTML_MIDDLE = <<< MIDDLE
+
+ |
+ +MIDDLE; + +$HTML_POST = <<< POST + + | +
".$geshi->parse_code()."
";
+}
+
+function strtocode($string)
+{
+ global $geshi;
+ $geshi->set_source($string);
+ return $geshi->parse_code();
+}
+
+function toHtml($string)
+{
+
+ return str_replace(array("[", "]"), array("<", ">"),$string);
+}
+
+function replace_lua($string)
+{
+ // Replace to Lua code.
+ $text = $string;
+
+ preg_match_all("/\[lua\]((.|\s)*?)\[\/lua\]/", $text, $out, PREG_SET_ORDER);
+ $tmp = "";
+ for($i = 0; $i < count($out); $i++)
+ {
+ $pos = strpos($string, $out[$i][0]);
+ $tmp .= (substr($string, 0, $pos));
+ $tmp .= "