You are here

function geshifilter_geshi_process in GeSHi Filter for syntax highlighting 7

Same name and namespace in other branches
  1. 5.2 geshifilter.pages.inc \geshifilter_geshi_process()
  2. 6 geshifilter.pages.inc \geshifilter_geshi_process()

geshifilter wrapper for GeSHi processing.

5 calls to geshifilter_geshi_process()
GeshiFilterTest::assertGeshiFilterHighlighting in ./geshifilter.test
Assert function for testing if GeSHi highlighting works
geshifilter_process_sourcecode in ./geshifilter.pages.inc
General geshifilter processing function for a chunk of source code.
theme_geshifield_formatter_default in geshifield/geshifield.module
theme_geshifield_formatter_nohighlighting in geshifield/geshifield.module
theme_geshifield_formatter_trimmed in geshifield/geshifield.module

File

./geshifilter.pages.inc, line 351

Code

function geshifilter_geshi_process($source_code, $lang, $line_numbering = 0, $linenumbers_start = 1, $inline_mode = FALSE, $title = NULL) {

  // load GeSHi library (if not already)
  $geshi_library = libraries_load('geshi');
  if (!$geshi_library['loaded']) {
    drupal_set_message($geshi_library['error message'], 'error');
    return $source_code;
  }

  // Check for a cached version of this source code and return it if available.
  // @todo: Use a dedicated table instead of using cache_filter? If so,
  // also take care of the flushing in _geshifilter_clear_filter_cache().
  $cache_id = "geshifilter:{$lang}:{$line_numbering}:{$linenumbers_start}:{$inline_mode}" . md5($title . $source_code);
  if ($cached = cache_get($cache_id, 'cache_filter')) {
    return $cached->data;
  }

  // remove leading/trailing newlines
  $source_code = trim($source_code, "\n\r");

  // create GeSHi object
  $geshi = _geshifilter_geshi_factory($source_code, $lang);

  // CSS mode
  $ccs_mode = variable_get('geshifilter_css_mode', GESHIFILTER_CSS_INLINE);
  if ($ccs_mode == GESHIFILTER_CSS_CLASSES_AUTOMATIC || $ccs_mode == GESHIFILTER_CSS_CLASSES_ONLY) {
    $geshi
      ->enable_classes(TRUE);
  }
  _geshifilter_override_geshi_defaults($geshi, $lang);

  // some more GeSHi settings and parsing
  if ($inline_mode) {

    // inline source code mode
    $geshi
      ->set_header_type(GESHI_HEADER_NONE);

    // To make highlighting work we have to manually set a class on the code
    // element we will wrap the code in.
    // To counter a change between GeSHi version 1.0.7.22 and 1.0.8 (svn
    // commit 1610), we use both the language and overall_class for the class,
    // to mimic the 1.0.8 behavior, which is backward compatible.
    // $language and $overall_class are protected with $geshi, with no get functions,
    // recreate them manually.
    $overall_class = 'geshifilter-' . $lang;
    $code_class = "{$lang} {$overall_class}";
    $source_code = '<span class="geshifilter"' . (isset($title) ? ' title="' . check_plain($title) . '"' : '') . '><code class="' . $code_class . '">' . $geshi
      ->parse_code() . '</code></span>';
  }
  else {

    // block source code mode
    $geshi
      ->set_header_type((int) variable_get('geshifilter_code_container', GESHI_HEADER_PRE));
    if ($line_numbering == 1) {
      $geshi
        ->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
      $geshi
        ->start_line_numbers_at($linenumbers_start);
    }
    elseif ($line_numbering >= 2) {
      $geshi
        ->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, $line_numbering);
      $geshi
        ->start_line_numbers_at($linenumbers_start);
    }
    if (isset($title)) {
      $source_code = '<div class="geshifilter-title">' . check_plain($title) . '</div>';
    }
    else {
      $source_code = '';
    }
    $source_code .= '<div class="geshifilter">' . $geshi
      ->parse_code() . '</div>';
  }

  // Store in cache with a minimum expiration time of 1 day.
  cache_set($cache_id, $source_code, 'cache_filter', time() + 60 * 60 * 24);
  return $source_code;
}