You are here

public function GeshiFilterFilter::tips in GeSHi Filter for syntax highlighting 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Filter/GeshiFilterFilter.php \Drupal\geshifilter\Plugin\Filter\GeshiFilterFilter::tips()

Get the tips for the filter.

Parameters

bool $long: If get the long or short tip.

Return value

string The tip to show for the user.

Overrides FilterBase::tips

File

src/Plugin/Filter/GeshiFilterFilter.php, line 188

Class

GeshiFilterFilter
Provides a base filter for Geshi Filter.

Namespace

Drupal\geshifilter\Plugin\Filter

Code

public function tips($long = FALSE) {

  // Get the supported tag styles.
  $tag_styles = array_filter($this
    ->tagStyles());
  $tag_style_examples = [];
  $bracket_open = NULL;
  $bracket_close = NULL;
  if (in_array(GeshiFilter::BRACKETS_ANGLE, $tag_styles)) {
    if (!$bracket_open) {
      $bracket_open = SafeMarkup::checkPlain('<');
      $bracket_close = SafeMarkup::checkPlain('>');
    }
    $tag_style_examples[] = '<code>' . SafeMarkup::checkPlain('<foo>') . '</code>';
  }
  if (in_array(GeshiFilter::BRACKETS_SQUARE, $tag_styles)) {
    if (!$bracket_open) {
      $bracket_open = SafeMarkup::checkPlain('[');
      $bracket_close = SafeMarkup::checkPlain(']');
    }
    $tag_style_examples[] = '<code>' . SafeMarkup::checkPlain('[foo]') . '</code>';
  }
  if (in_array(GeshiFilter::BRACKETS_DOUBLESQUARE, $tag_styles)) {
    if (!$bracket_open) {
      $bracket_open = SafeMarkup::checkPlain('[[');
      $bracket_close = SafeMarkup::checkPlain(']]');
    }
    $tag_style_examples[] = '<code>' . SafeMarkup::checkPlain('[[foo]]') . '</code>';
  }
  if (in_array(GeshiFilter::BRACKETS_MARKDOWNBLOCK, $tag_styles)) {
    if (!$bracket_open) {
      $bracket_open = SafeMarkup::checkPlain('```');
      $bracket_close = SafeMarkup::checkPlain('```');
    }
    $tag_style_examples[] = '<code>' . SafeMarkup::checkPlain('```foo ```') . '</code>';
  }
  if (!$bracket_open) {
    drupal_set_message($this
      ->t('Could not determine a valid tag style for GeSHi filtering.'), 'error');
    $bracket_open = SafeMarkup::checkPlain('<');
    $bracket_close = SafeMarkup::checkPlain('>');
  }
  if ($long) {

    // Get the available tags.
    list($generic_code_tags, $language_tags, $tag_to_lang) = $this
      ->getTags();

    // Get the available languages.
    $languages = GeshiFilter::getEnabledLanguages();
    $lang_attributes = GeshiFilter::whitespaceExplode(GeshiFilter::ATTRIBUTES_LANGUAGE);

    // Syntax highlighting tags.
    $output = '<p>' . $this
      ->t('Syntax highlighting of source code can be enabled with the following tags:') . '</p>';
    $items = [];

    // Seneric tags.
    $tags = [];
    foreach ($generic_code_tags as $tag) {
      $tags[] = $bracket_open . $tag . $bracket_close;
    }
    $items[] = $this
      ->t('Generic syntax highlighting tags: <code>@tags</code>.', [
      '@tags' => Markup::create(implode(', ', $tags)),
    ]);

    // Language tags.
    $tags = [];
    foreach ($language_tags as $tag) {
      $tags[] = $this
        ->t('<code>@tag</code> for @lang source code', [
        '@tag' => Markup::create($bracket_open . $tag . $bracket_close),
        '@lang' => $languages[$tag_to_lang[$tag]],
      ]);
    }
    $items[] = '<li>' . $this
      ->t('Language specific syntax highlighting tags:') . implode(', ', $tags) . '</li>';

    // PHP specific delimiters.
    if (in_array(GeshiFilter::BRACKETS_PHPBLOCK, $tag_styles)) {
      $items[] = $this
        ->t('PHP source code can also be enclosed in &lt;?php ... ?&gt; or &lt;% ... %&gt;, but additional options like line numbering are not possible here.');
    }
    $output .= '<ul>' . implode('', $items) . '</ul>';

    // Options and tips.
    $output .= '<p>' . $this
      ->t('Options and tips:') . '</p>';
    $items = [];

    // Info about language attribute to language mapping.
    $att_to_full = [];
    foreach ($languages as $langcode => $fullname) {
      $att_to_full[$langcode] = $fullname;
    }
    foreach ($tag_to_lang as $tag => $lang) {
      $att_to_full[$tag] = $languages[$lang];
    }
    ksort($att_to_full);
    $att_for_full = [];
    foreach ($att_to_full as $att => $fullname) {
      $att_for_full[] = $this
        ->t('"<code>@langcode</code>" (for @fullname)', [
        '@langcode' => $att,
        '@fullname' => $fullname,
      ]);
    }
    $items[] = $this
      ->t('The language for the generic syntax highlighting tags can be
        specified with one of the attribute(s): %attributes. The possible values
        are: @languages.', [
      '%attributes' => implode(', ', $lang_attributes),
      '@languages' => Markup::create(implode(', ', $att_for_full)),
    ]);

    // Tag style options.
    if (count($tag_style_examples) > 1) {
      $items[] = $this
        ->t('The supported tag styles are: @tag_styles.', [
        '@tag_styles' => Markup::create(implode(', ', $tag_style_examples)),
      ]);
    }

    // Line numbering options.
    $items[] = $this
      ->t('<em>Line numbering</em> can be enabled/disabled with the
        attribute "%linenumbers". Possible values are: "%off" for no line
        numbers, "%normal" for normal line numbers and "%fancy" for fancy line
        numbers (every n<sup>th</sup> line number highlighted). The start line
        number can be specified with the attribute "%start", which implicitly
        enables normal line numbering. For fancy line numbering the interval
        for the highlighted line numbers can be specified with the attribute
        "%interval", which implicitly enables fancy line numbering.', [
      '%linenumbers' => GeshiFilter::ATTRIBUTE_LINE_NUMBERING,
      '%off' => 'off',
      '%normal' => 'normal',
      '%fancy' => 'fancy',
      '%start' => GeshiFilter::ATTRIBUTE_LINE_NUMBERING_START,
      '%interval' => GeshiFilter::ATTRIBUTE_FANCY_N,
    ]);

    // Block versus inline.
    $items[] = $this
      ->t('If the source code between the tags contains a newline (e.g.
        immediatly after the opening tag), the highlighted source code will be
        displayed as a code block. Otherwise it will be displayed inline.');

    // Code block title.
    $items[] = $this
      ->t('A title can be added to a code block with the attribute "%title".', [
      '%title' => GeshiFilter::ATTRIBUTE_TITLE,
    ]);
    $render = [
      '#theme' => 'item_list',
      '#items' => $items,
      '#type' => 'ul',
    ];
    $output .= render($render);

    // Defaults.
    $output .= '<p>' . $this
      ->t('Defaults:') . '</p>';
    $items = [];
    $default_highlighting = $this->config
      ->get('default_highlighting');
    switch ($default_highlighting) {
      case GeshiFilter::DEFAULT_DONOTHING:
        $description = $this
          ->t("when no language attribute is specified the code\n            block won't be processed by the GeSHi filter");
        break;
      case GeshiFilter::DEFAULT_PLAINTEXT:
        $description = $this
          ->t('when no language attribute is specified, no syntax
           highlighting will be done');
        break;
      default:
        $description = $this
          ->t('the default language used for syntax highlighting is
            "%default_lang"', [
          '%default_lang' => $default_highlighting,
        ]);
        break;
    }
    $items[] = $this
      ->t('Default highlighting mode for generic syntax highlighting
        tags: @description.', [
      '@description' => $description,
    ]);
    $default_line_numbering = $this->config
      ->get('default_line_numbering');
    switch ($default_line_numbering) {
      case GeshiFilter::LINE_NUMBERS_DEFAULT_NONE:
        $description = $this
          ->t('no line numbers');
        break;
      case GeshiFilter::LINE_NUMBERS_DEFAULT_NORMAL:
        $description = $this
          ->t('normal line numbers');
        break;
      default:
        $description = $this
          ->t('fancy line numbers (every @n lines)', [
          '@n' => $default_line_numbering,
        ]);
        break;
    }
    $items[] = $this
      ->t('Default line numbering: @description.', [
      '@description' => $description,
    ]);
    $render = [
      '#theme' => 'item_list',
      '#items' => $items,
      '#type' => 'ul',
    ];
    $output .= render($render);
  }
  else {

    // Get the available tags.
    list($generic_code_tags, $language_tags, $tag_to_lang) = $this
      ->getTags();
    $tags = [];
    foreach ($generic_code_tags as $tag) {
      $tags[] = '<code>' . $bracket_open . SafeMarkup::checkPlain($tag) . $bracket_close . '</code>';
    }
    foreach ($language_tags as $tag) {
      $tags[] = '<code>' . $bracket_open . SafeMarkup::checkPlain($tag) . $bracket_close . '</code>';
    }
    $output = $this
      ->t('You can enable syntax highlighting of source code with the following tags: @tags.', [
      '@tags' => Markup::create(implode(', ', $tags)),
    ]);

    // Tag style options.
    if (count($tag_style_examples) > 1) {
      $output .= ' ' . $this
        ->t('The supported tag styles are: @tag_styles.', [
        '@tag_styles' => Markup::create(implode(', ', $tag_style_examples)),
      ]);
    }
    if (in_array(GeshiFilter::BRACKETS_PHPBLOCK, $tag_styles)) {
      $output .= ' ' . $this
        ->t('PHP source code can also be enclosed in &lt;?php ... ?&gt; or &lt;% ... %&gt;.');
    }
  }
  return $output;
}