You are here

public static function GeshiFilterProcess::geshiProcess in GeSHi Filter for syntax highlighting 8

Same name and namespace in other branches
  1. 8.2 src/GeshiFilterProcess.php \Drupal\geshifilter\GeshiFilterProcess::geshiProcess()

Geshifilter wrapper for GeSHi processing.

Parameters

string $source_code: Source code to process.

string $lang: Language from sourcecode.

int $line_numbering: The line numbering mode, one of LINE_NUMBERS_* from GeshiFilter class.

int $linenumbers_start: The line number to start from.

bool $inline_mode: When to write all styles inline or from a css.

string $title: The title to use in code.

array $special_lines: Special lines to highlight.

Return value

string The sourcecode after process by Geshi.

3 calls to GeshiFilterProcess::geshiProcess()
geshifield_formatter_default in geshifield/geshifield.module
Format the field.
GeshiFilterProcess::processSourceCode in src/GeshiFilterProcess.php
General geshifilter processing function for a chunk of source code.
GeshiFilterTest::assertGeshiFilterHighlighting in tests/src/Functional/GeshiFilterTest.php
Assert function for testing if GeSHi highlighting works.

File

src/GeshiFilterProcess.php, line 64

Class

GeshiFilterProcess
Helpers functions related to processing the source code with geshi.

Namespace

Drupal\geshifilter

Code

public static function geshiProcess($source_code, $lang, $line_numbering = 0, $linenumbers_start = 1, $inline_mode = FALSE, $title = NULL, array $special_lines = []) {
  $config = \Drupal::config('geshifilter.settings');

  // Load GeSHi library (if not already).
  $geshi_library = GeshiFilter::loadGeshi();
  if (!$geshi_library['loaded']) {
    drupal_set_message($geshi_library['error message'], 'error');
    return $source_code;
  }
  $source_code = trim($source_code, "\n\r");

  // Create GeSHi object.
  $geshi = self::geshiFactory($source_code, $lang);

  // CSS mode.
  $ccs_mode = $config
    ->get('css_mode');
  if ($ccs_mode == GeshiFilter::CSS_CLASSES_AUTOMATIC || $ccs_mode == GeshiFilter::CSS_CLASSES_ONLY) {
    $geshi
      ->enable_classes(TRUE);
  }
  self::overrideGeshiDefaults($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="' . SafeMarkup::checkPlain($title) . '"' : '') . '><code class="' . $code_class . '">' . $geshi
      ->parse_code() . '</code></span>';
  }
  else {
    $geshi
      ->highlight_lines_extra($special_lines);

    // How many spaces to use for tabs.
    $geshi
      ->set_tab_width($config
      ->get('tab_width'));

    // Block source code mode.
    $geshi
      ->set_header_type((int) $config
      ->get('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">' . SafeMarkup::checkPlain($title) . '</div>';
    }
    else {
      $source_code = '';
    }
    $source_code .= '<div class="geshifilter">' . $geshi
      ->parse_code() . '</div>';
  }
  return $source_code;
}