summaryrefslogtreecommitdiffstats
path: root/src/Plugin/Filter/GeshiFilterFilter.php
blob: 09c7175c24086849c2101091e1466bc5ba0659a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
<?php

namespace Drupal\geshifilter\Plugin\Filter;

// Base class for filters.
use Drupal\filter\Plugin\FilterBase;

// Necessary for SafeMarkup::checkPlain().
use Drupal\Component\Utility\SafeMarkup;

// Necessary for passing HTML into t().
use Drupal\Core\Render\Markup;

// Necessary for Html::decodeEntities().
use Drupal\Component\Utility\Html;

// Necessary for forms.
use Drupal\Core\Form\FormStateInterface;

// Necessary for result of process().
use Drupal\filter\FilterProcessResult;

// Necessary for URL.
use Drupal\Core\Url;

use Drupal\geshifilter\GeshiFilter;
use Drupal\geshifilter\GeshiFilterProcess;

/**
 * Provides a base filter for Geshi Filter.
 *
 * @Filter(
 *   id = "filter_geshifilter",
 *   module = "geshifilter",
 *   title = @Translation("GeSHi filter"),
 *   description = @Translation("Enables syntax highlighting of inline/block
 *     source code using the GeSHi engine"),
 *   type = \Drupal\filter\Plugin\FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
 *   cache = FALSE,
 *   settings = {
 *     "general_tags" = {},
 *     "per_language_settings" = {}
 *   },
 *   weight = 0
 * )
 */
class GeshiFilterFilter extends FilterBase {

  /**
   * Object with configuration for geshifilter.
   *
   * @var object
   */
  protected $config;

  /**
   * Object with configuration for geshifilter, where we need editable..
   *
   * @var object
   */
  protected $configEditable;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->config = \Drupal::config('geshifilter.settings');
    $this->configEditable = \Drupal::configFactory()->getEditable('geshifilter.settings');
  }

  /**
   * {@inheritdoc}
   */
  public function process($text, $langcode) {

    $result = new FilterProcessResult($text);

    try {
      // Load GeSHi library (if not already).
      $geshi_library = GeshiFilter::loadGeshi();
      if (!$geshi_library['loaded']) {
        throw new \Exception($geshi_library['error message']);
      }

      // Get the available tags.
      list($generic_code_tags, $language_tags, $tag_to_lang) = $this->getTags();
      if (in_array(GeshiFilter::BRACKETS_PHPBLOCK, array_filter($this->tagStyles()))) {
        $language_tags[] = 'questionmarkphp';
        $tag_to_lang['questionmarkphp'] = 'php';
      }
      $tags = array_merge($generic_code_tags, $language_tags);
      // Escape special (regular expression) characters in tags (for tags like
      // 'c++' and 'c#').
      $tags = preg_replace('#(\\+|\\#)#', '\\\\$1', $tags);

      $tags_string = implode('|', $tags);
      // Pattern for matching the prepared "<code>...</code>" stuff.
      $pattern = '#\\[geshifilter-(' . $tags_string . ')([^\\]]*)\\](.*?)(\\[/geshifilter-\1\\])#s';
      $text = preg_replace_callback($pattern, [
        $this,
        'replaceCallback',
      ], $text);

      // Create the object with result.
      $result = new FilterProcessResult($text);

      // Add the css file when necessary.
      if (in_array($this->config->get('css_mode'), [GeshiFilter::CSS_CLASSES_AUTOMATIC, GeshiFilter::CSS_INLINE])) {
        $result->setAttachments([
          'library' => [
            'geshifilter/geshifilter',
          ],
        ]);
      }

      // Add cache tags, so we can re-create the node when some geshifilter
      // settings change.
      $cache_tags = ['geshifilter'];
      $result->addCacheTags($cache_tags);
    }
    catch (\Exception $e) {
      watchdog_exception('geshifilter', $e);
      drupal_set_message($geshi_library['error message'], 'error');
    }

    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function prepare($text, $langcode) {
    // Get the available tags.
    list($generic_code_tags, $language_tags, $tag_to_lang) = $this->getTags();
    $tags = array_merge($generic_code_tags, $language_tags);

    // Escape special (regular expression) characters in tags (for tags like
    // 'c++' and 'c#').
    $tags = preg_replace('#(\\+|\\#)#', '\\\\$1', $tags);
    $tags_string = implode('|', $tags);
    // Pattern for matching "<code>...</code>" like stuff
    // Also matches "<code>...$"  where "$" refers to end of string, not end of
    // line (because PCRE_MULTILINE (modifier 'm') is not enabled), so matching
    // still works when teaser view trims inside the source code.
    // Replace the code container tag brackets
    // and prepare the container content (newline and angle bracket protection).
    // @todo: make sure that these replacements can be done in series.
    $tag_styles = array_filter($this->tagStyles());
    if (in_array(GeshiFilter::BRACKETS_ANGLE, $tag_styles)) {
      // Prepare <foo>..</foo> blocks.
      $pattern = '#(<)(' . $tags_string . ')((\s+[^>]*)*)(>)(.*?)(</\2\s*>|$)#s';
      $text = preg_replace_callback($pattern, [$this, 'prepareCallback'], $text);
    }
    if (in_array(GeshiFilter::BRACKETS_SQUARE, $tag_styles)) {
      // Prepare [foo]..[/foo] blocks.
      $pattern = '#((?<!\[)\[)(' . $tags_string . ')((\s+[^\]]*)*)(\])(.*?)((?<!\[)\[/\2\s*\]|$)#s';
      $text = preg_replace_callback($pattern, [$this, 'prepareCallback'], $text);
    }
    if (in_array(GeshiFilter::BRACKETS_DOUBLESQUARE, $tag_styles)) {
      // Prepare [[foo]]..[[/foo]] blocks.
      $pattern = '#(\[\[)(' . $tags_string . ')((\s+[^\]]*)*)(\]\])(.*?)(\[\[/\2\s*\]\]|$)#s';
      $text = preg_replace_callback($pattern, [$this, 'prepareCallback'], $text);
    }
    if (in_array(GeshiFilter::BRACKETS_PHPBLOCK, $tag_styles)) {
      // Prepare < ?php ... ? > blocks.
      $pattern = '#[\[<](\?php|\?PHP|%)(.+?)((\?|%)[\]>]|$)#s';
      $text = preg_replace_callback($pattern, [$this, 'preparePhpCallback'], $text);
    }
    if (in_array(GeshiFilter::BRACKETS_MARKDOWNBLOCK, $tag_styles)) {
      // Prepare ```php ``` blocks(markdown).
      $pattern = '#(```([a-z]*)\n([\s\S]*?)\n```)#s';
      $text = preg_replace_callback($pattern, [$this, 'prepareMarkdownCallback'], $text);
    }
    return $text;
  }

  /**
   * Get the tips for the filter.
   *
   * @param bool $long
   *   If get the long or short tip.
   *
   * @return string
   *   The tip to show for the user.
   */
  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
            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;
  }

  /**
   * Create the settings form for the filter.
   *
   * @param array $form
   *   A minimally prepopulated form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The state of the (entire) configuration form.
   *
   * @return array
   *   The $form array with additional form elements for the settings of
   *   this filter. The submitted form values should match $this->settings.
   *
   * @todo Add validation of submited form values, it already exists for
   *       drupal 7, must update it only.
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    if ($this->configEditable->get('use_format_specific_options')) {
      // Tags and attributes.
      $form['general_tags'] = $this->generalHighlightTagsSettings();
      // Per language tags.
      $form['per_language_settings'] = [
        '#type' => 'fieldset',
        '#title' => $this->t('Per language tags'),
        '#collapsible' => TRUE,
        'table' => $this->perLanguageSettings('enabled', FALSE, TRUE),
      ];
      // Validate the tags
      // $form['#validate'][] = '::validateForm';.
    }
    else {
      $form['info'] = [
        '#markup' => '<p>' . $this->t('GeSHi filter is configured to use global tag
          settings. For separate settings per text format, enable this option in
          the <a href=":geshi_admin_url">general GeSHi filter settings</a>.', [
            ':geshi_admin_url' => Url::fromRoute('geshifilter.settings')->toString(),
          ]
        ) . '</p>',
      ];
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  /*public function validateForm(array &$form, FormStateInterface $form_state) {
  // Language tags should differ from each other.
  $languages = GeshiFilter::getAvailableLanguages();

  $values = $form_state->getValue('language');
  foreach ($languages as $language1 => $language_data1) {

  if ($values[$language1]['enabled'] == FALSE) {
  continue;
  }

  $tags1 = GeshiFilter::tagSplit($values[$language1]['tags']);

  // Check that other languages do not use these tags.
  foreach ($languages as $language2 => $language_data2) {
  // Check these tags against the tags of other enabled languages.
  if ($language1 == $language2) {
  continue;
  }
  // Get tags for $language2.
  $tags2 = GeshiFilter::tagSplit($values[$language2]['tags']);

  // Get generic tags.
  $generics = GeshiFilter::tagSplit($this->config->get('tags'));
  $tags2 = array_merge($tags2, $generics);

  // And now we can check tags1 against tags2.
  foreach ($tags1 as $tag1) {
  foreach ($tags2 as $tag2) {
  if ($tag1 == $tag2) {
  $name = "language[{$language2}][tags]";
  $form_state->setErrorByName($name, t('The language tags should differ between
  languages and from the generic tags.'));
  }
  }
  }
  }
  }
  }*/

  /**
   * Get the tags for this filter.
   *
   * @return string
   *   A string with the tags for this filter.
   */
  protected function tags() {
    if (!$this->config->get('use_format_specific_options')) {
      // We do not want per filter tags, so get the global tags.
      return $this->config->get('tags');
    }
    else {
      if (isset($this->settings['general_tags']['tags'])) {
        // Tags are set for this format.
        return $this->settings['general_tags']['tags'];
      }
      else {
        // Tags are not set for this format, so use the global ones.
        return $this->config->get('tags');
      }
    }
  }

  /**
   * Helper function for gettings the tags.
   *
   * Old: _geshifilter_get_tags.
   *
   * @todo: recreate a cache for this function.
   */
  protected function getTags() {
    $generic_code_tags = GeshiFilter::tagSplit($this->tags());
    $language_tags = [];
    $tag_to_lang = [];
    $enabled_languages = GeshiFilter::getEnabledLanguages();
    foreach ($enabled_languages as $language => $fullname) {
      $lang_tags = GeshiFilter::tagSplit($this->languageTags($language));
      foreach ($lang_tags as $lang_tag) {
        $language_tags[] = $lang_tag;
        $tag_to_lang[$lang_tag] = $language;
      }
    }

    return [
      $generic_code_tags,
      $language_tags,
      $tag_to_lang,
    ];
  }

  /**
   * Helper function for some settings form fields.
   */
  protected function generalHighlightTagsSettings() {
    $form = [];

    // Generic tags.
    $form["tags"] = [
      '#type' => 'textfield',
      '#title' => $this->t('Generic syntax highlighting tags'),
      '#default_value' => $this->tags(),
      '#description' => $this->t('Tags that should activate the GeSHi syntax highlighting. Specify a space-separated list of tagnames.'),
    ];
    // Container tag styles.
    $form["tag_styles"] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Container tag style'),
      '#options' => [
        GeshiFilter::BRACKETS_ANGLE => '<code>' . htmlentities('<foo> ... </foo>') . '</code>',
        GeshiFilter::BRACKETS_SQUARE => '<code>' . htmlentities('[foo] ... [/foo]') . '</code>',
        GeshiFilter::BRACKETS_DOUBLESQUARE => '<code>' . htmlentities('[[foo]] ... [[/foo]]') . '</code>',
        GeshiFilter::BRACKETS_PHPBLOCK => $this->t('PHP style source code blocks: <code>@php</code> and <code>@percent</code>', [
          '@php' => '<?php ... ?>',
          '@percent' => '<% ... %>',
        ]),
        GeshiFilter::BRACKETS_MARKDOWNBLOCK => '<code>' . htmlentities('```foo ... ```') . '</code>',
      ],
      '#default_value' => $this->tagStyles(),
      '#description' => $this->t('Select the container tag styles that should trigger GeSHi syntax highlighting.'),
    ];
    // Decode entities.
    $form["decode_entities"] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Decode entities'),
      '#default_value' => $this->settings['general_tags']['decode_entities'],
      '#description' => $this->t('Decode entities, for example, if the code has been typed in a WYSIWYG editor.'),
    ];
    return $form;
  }

  /**
   * Function for generating a form table for per language settings.
   *
   * @param string $view
   *   Which languages to show:
   *   - enabled Show only enabled languages.
   *   - disabled Show only disabled languages.
   *   - all Show all languages.
   * @param bool $add_checkbox
   *   When add(TRUE) or not a  checkbox to enable languages.
   * @param bool $add_tag_option
   *   When add(TRUE) or not a textbox to set the tags for a language.
   *
   * @return array
   *   An array with form elements for languages.
   */
  protected function perLanguageSettings($view, $add_checkbox, $add_tag_option) {
    $form = [];
    $header = [
      $this->t('Language'),
      $this->t('GeSHi language code'),
    ];
    if ($add_tag_option) {
      $header[] = $this->t('Tag/language attribute value');
    }
    $form['language'] = [
      '#type' => 'table',
      '#header' => $header,
      '#empty' => $this->t('Nome language is available.'),
    ];

    // Table body.
    $languages = GeshiFilter::getAvailableLanguages();
    foreach ($languages as $language => $language_data) {
      $enabled = $this->config->get("language.{$language}.enabled", FALSE);
      // Skip items to hide.
      if (($view == 'enabled' && !$enabled) || ($view == 'disabled' && $enabled)) {
        continue;
      }
      // Build language row.
      $form['language'][$language] = [];
      // Add enable/disable checkbox.
      if ($add_checkbox) {
        $form['language'][$language]['enabled'] = [
          '#type' => 'checkbox',
          '#default_value' => $enabled,
          '#title' => $language_data['fullname'],
        ];
      }
      else {
        $form['language'][$language]['fullname'] = [
          '#type' => 'markup',
          '#markup' => $language_data['fullname'],
        ];
      }
      // Language code.
      $form['language'][$language]['name'] = [
        '#type' => 'markup',
        '#markup' => $language,
      ];
      // Add a textfield for tags.
      if ($add_tag_option) {
        $form['language'][$language]['tags'] = [
          '#type' => 'textfield',
          '#default_value' => $this->settings['per_language_settings']['table']['language'][$language]['tags'],
          '#size' => 20,
        ];
      }
    }
    return $form;
  }

  /**
   * Get the tags for a language.
   *
   * @param string $language
   *   The language to get the tags(ex: php, html, ...).
   *
   * @return string
   *   The tags for the language(ex: [php],[php5],...).
   */
  private function languageTags($language) {
    if (!$this->config->get('use_format_specific_options')) {
      return $this->config->get("language.{$language}.tags");
    }
    else {
      $settings = $this->settings["per_language_settings"]['table']['language'];
      if (isset($settings[$language]["tags"])) {
        // Tags are set for this language.
        return $settings[$language]["tags"];
      }
      else {
        // Tags are not set for this language, so use the global ones.
        return $this->config->get("language.{$language}.tags");
      }
    }
  }

  /**
   * Get the tag style.
   *
   * @return array
   *   Where to use [], <>, or both for tags.
   */
  protected function tagStyles() {
    if ($this->config->get('use_format_specific_options') == FALSE) {
      // Get global tag styles.
      $styles = $this->config->get('tag_styles');
    }
    else {
      if (isset($this->settings['general_tags']["tag_styles"])) {
        // Tags are set for this language.
        $styles = $this->settings['general_tags']["tag_styles"];
      }
      else {
        // Tags are not set for this language, so use the global ones.
        $styles = $this->config->get('tag_styles');
      }
    }
    return $styles;
  }

  /**
   * Callback for preg_replace_callback.
   *
   * Old: _geshifilter_replace_callback($match, $format).
   *
   * @param array $match
   *   Elements from array:
   *   - 0: complete matched string.
   *   - 1: tag name.
   *   - 2: tag attributes.
   *   - 3: tag content.
   *
   * @return string
   *   Return the string processed by geshi library.
   */
  protected function replaceCallback(array $match) {
    $complete_match = $match[0];
    $tag_name = $match[1];
    $tag_attributes = $match[2];
    $source_code = $match[3];

    // Undo linebreak and escaping from preparation phase.
    $source_code = Html::decodeEntities($source_code);

    // Initialize to default settings.
    $lang = $this->config->get('default_highlighting');
    $line_numbering = $this->config->get('default_line_numbering');
    $linenumbers_start = 1;
    $title = NULL;

    // Determine language based on tag name if possible.
    list($generic_code_tags, $language_tags, $tag_to_lang) = $this->getTags();
    if (in_array(GeshiFilter::BRACKETS_PHPBLOCK, array_filter($this->tagStyles()))) {
      $language_tags[] = 'questionmarkphp';
      $tag_to_lang['questionmarkphp'] = 'php';
    }
    if (isset($tag_to_lang[$tag_name])) {
      $lang = $tag_to_lang[$tag_name];
    }

    // Get additional settings from the tag attributes.
    $settings = $this->parseAttributes($tag_attributes);
    if (isset($settings['language'])) {
      $lang = $settings['language'];
    }
    if (isset($settings['line_numbering'])) {
      $line_numbering = $settings['line_numbering'];
    }
    if (isset($settings['linenumbers_start'])) {
      $linenumbers_start = $settings['linenumbers_start'];
    }
    if (isset($settings['title'])) {
      $title = $settings['title'];
    }
    if (isset($settings['special_lines'])) {
      $special_lines = $settings['special_lines'];
    }

    if ($lang == GeshiFilter::DEFAULT_DONOTHING) {
      // Do nothing, and return the original.
      return $complete_match;
    }
    if ($lang == GeshiFilter::DEFAULT_PLAINTEXT) {
      // Use plain text 'highlighting'.
      $lang = 'text';
    }
    $inline_mode = (strpos($source_code, "\n") === FALSE);
    // Process and return.
    return GeshiFilterProcess::processSourceCode($source_code, $lang, $line_numbering, $linenumbers_start, $inline_mode, $title, $special_lines);
  }

  /**
   * Helper function for parsing the attributes of GeSHi code tags.
   *
   * Get the settings for language, line numbers, etc.
   *
   * @param string $attributes
   *   String with the attributes.
   *
   * @return array
   *   An array of settings with fields 'language', 'line_numbering',
   *   'linenumbers_start' and 'title'.
   */
  public function parseAttributes($attributes) {
    // Initial values.
    $lang = NULL;
    $line_numbering = NULL;
    $linenumbers_start = NULL;
    $title = NULL;
    $special_lines = [];
    $class_to_lang = NULL;

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

    $language_attributes = GeshiFilter::whitespaceExplode(GeshiFilter::ATTRIBUTES_LANGUAGE);
    $attributes_preg_string = implode('|', array_merge(
      $language_attributes, [
        GeshiFilter::ATTRIBUTE_LINE_NUMBERING,
        GeshiFilter::ATTRIBUTE_LINE_NUMBERING_START,
        GeshiFilter::ATTRIBUTE_FANCY_N,
        GeshiFilter::ATTRIBUTE_TITLE,
        GeshiFilter::ATTRIBUTE_SPECIAL_LINES,
      ]
    ));
    $enabled_languages = GeshiFilter::getEnabledLanguages();

    // Parse $attributes to an array $attribute_matches with:
    // $attribute_matches[0][xx] fully matched string, e.g. 'language="python"'
    // $attribute_matches[1][xx] param name, e.g. 'language'
    // $attribute_matches[2][xx] param value, e.g. 'python'.
    preg_match_all('#(' . $attributes_preg_string . ')="?([^"]*)"?#', $attributes, $attribute_matches);

    foreach ($attribute_matches[1] as $a_key => $att_name) {
      // Get attribute value.
      $att_value = $attribute_matches[2][$a_key];

      // Check for the language attributes.
      $class_to_lang = str_replace('language-', '', $att_value);

      if (in_array($att_name, $language_attributes)) {
        // Try first to map the attribute value to geshi language code.
        if (in_array($att_value, $language_tags)) {
          $att_value = $tag_to_lang[$att_value];
        }
        // Set language if extracted language is an enabled language.
        if (array_key_exists($att_value, $enabled_languages)) {
          $lang = $att_value;
        }
        // class_to_lang hotfix: language never filled cause ckeditor plugin uses classes instead of tags.
        elseif($att_name == 'class' && array_key_exists($class_to_lang, $enabled_languages)) {
          $lang = $class_to_lang;
        }
      }

      // Check for line numbering related attributes.
      // $line_numbering defines the line numbering mode:
      // 0: no line numbering.
      // 1: normal line numbering.
      // n>= 2: fancy line numbering every nth line.
      elseif ($att_name == GeshiFilter::ATTRIBUTE_LINE_NUMBERING) {
        switch (strtolower($att_value)) {
          case "off":
            $line_numbering = 0;
            break;

          case "normal":
            $line_numbering = 1;
            break;

          case "fancy":
            $line_numbering = 5;
            break;
        }
      }
      elseif ($att_name == GeshiFilter::ATTRIBUTE_FANCY_N) {
        $att_value = (int) ($att_value);
        if ($att_value >= 2) {
          $line_numbering = $att_value;
        }
      }
      elseif ($att_name == GeshiFilter::ATTRIBUTE_LINE_NUMBERING_START) {
        if ($line_numbering < 1) {
          $line_numbering = 1;
        }
        $linenumbers_start = (int) ($att_value);
      }
      elseif ($att_name == GeshiFilter::ATTRIBUTE_TITLE) {
        $title = $att_value;
      }
      elseif ($att_name == GeshiFilter::ATTRIBUTE_SPECIAL_LINES) {
        $special_lines = explode(',', $att_value);
      }
    }

    // Return parsed results.
    return [
      'language' => $lang,
      'line_numbering' => $line_numbering,
      'linenumbers_start' => $linenumbers_start,
      'title' => $title,
      'special_lines' => $special_lines,
    ];
  }

  /**
   * Callback_geshifilter_prepare for preparing input text.
   *
   * Replaces the code tags brackets with geshifilter specific ones to prevent
   * possible messing up by other filters, e.g.
   *   '[python]foo[/python]' to '[geshifilter-python]foo[/geshifilter-python]'.
   * Replaces newlines with "&#10;" to prevent issues with the line break filter
   * Escapes the tricky characters like angle brackets with
   * SafeMarkup::checkPlain() to prevent messing up by other filters like the
   * HTML filter.
   *
   * @param array $match
   *   An array with the pieces from matched string.
   *   - 0: complete matched string.
   *   - 1: opening bracket ('<' or '[').
   *   - 2: tag.
   *   - 3: and.
   *   - 4: attributes.
   *   - 5: closing bracket.
   *   - 6: source code.
   *   - 7: closing tag.
   *
   * @return string
   *   Return escaped code block.
   */
  public function prepareCallback(array $match) {
    $tag_name = $match[2];
    $tag_attributes = $match[3];
    $content = $match[6];

    // Get the default highlighting mode.
    $lang = $this->config->get('default_highlighting');
    if ($lang == GeshiFilter::DEFAULT_DONOTHING) {
      // If the default highlighting mode is GeshiFilter::DEFAULT_DONOTHING
      // and there is no language set (with language tag or language attribute),
      // we should not do any escaping in this prepare phase,
      // so that other filters can do their thing.
      $enabled_languages = GeshiFilter::getEnabledLanguages();
      // Usage of language tag?
      list($generic_code_tags, $language_tags, $tag_to_lang) = $this->getTags();
      if (isset($tag_to_lang[$tag_name]) && isset($enabled_languages[$tag_to_lang[$tag_name]])) {
        $lang = $tag_to_lang[$tag_name];
      }
      // Usage of language attribute?
      else {
        // Get additional settings from the tag attributes.
        $settings = $this->parseAttributes($tag_attributes);
        if ($settings['language'] && isset($enabled_languages[$settings['language']])) {
          $lang = $settings['language'];
        }
      }
      // If no language was set: prevent escaping and return original string.
      if ($lang == GeshiFilter::DEFAULT_DONOTHING) {
        return $match[0];
      }
    }
    if ($this->decodeEntities()) {
      $content = $this->unencode($content);
    }
    // Return escaped code block.
    return '[geshifilter-' . $tag_name . $tag_attributes . ']'
      . str_replace(["\r", "\n"], ['', '&#10;'], SafeMarkup::checkPlain($content))
      . '[/geshifilter-' . $tag_name . ']';
  }

  /**
   * Callback for _geshifilter_prepare for < ?php ... ? > blocks.
   *
   * @param array $match
   *   An array with the pieces from matched string.
   */
  public function preparePhpCallback(array $match) {
    if ($this->decodeEntities()) {
      $match[2] = $this->unencode($match[2]);
    }
    return '[geshifilter-questionmarkphp]'
    . str_replace(["\r", "\n"], ['', '&#10;'], SafeMarkup::checkPlain($match[2]))
    . '[/geshifilter-questionmarkphp]';
  }

  /**
   * Callback for preparing input text from markdown(```) tags.
   *
   * Replaces the code tags brackets with geshifilter specific ones to prevent
   * possible messing up by other filters, e.g.
   *   '[python]foo[/python]' to '[geshifilter-python]foo[/geshifilter-python]'.
   * Replaces newlines with "&#10;" to prevent issues with the line break filter
   * Escapes the tricky characters like angle brackets with
   * SafeMarkup::checkPlain() to prevent messing up by other filters like the
   * HTML filter.
   *
   * @param array $match
   *   An array with the pieces from matched string.
   *
   * @return string
   *   Return escaped code block.
   */
  public function prepareMarkdownCallback(array $match) {
    $tag_name = $match[2];
    $tag_attributes = '';
    $content = $match[3];

    // Get the default highlighting mode.
    $lang = $this->config->get('default_highlighting');
    if ($lang == GeshiFilter::DEFAULT_DONOTHING) {
      // If the default highlighting mode is GeshiFilter::DEFAULT_DONOTHING
      // and there is no language set (with language tag or language attribute),
      // we should not do any escaping in this prepare phase,
      // so that other filters can do their thing.
      $enabled_languages = GeshiFilter::getEnabledLanguages();
      // Usage of language tag?
      list($generic_code_tags, $language_tags, $tag_to_lang) = $this->getTags();
      if (isset($tag_to_lang[$tag_name]) && isset($enabled_languages[$tag_to_lang[$tag_name]])) {
        $lang = $tag_to_lang[$tag_name];
      }

      // If no language was set: prevent escaping and return original string.
      if ($lang == GeshiFilter::DEFAULT_DONOTHING) {
        return $match[0];
      }
    }
    if ($this->decodeEntities()) {
      $content = $this->unencode($content);
    }
    // Return escaped code block.
    return '[geshifilter-' . $tag_name . $tag_attributes . ']'
      . str_replace(["\r", "\n"], ['', '&#10;'], SafeMarkup::checkPlain($content))
      . '[/geshifilter-' . $tag_name . ']';
  }

  /**
   * Return the string with some html entities unencoded.
   *
   * Text editors like ckeditor encodes some strings, like " to &amp&quote;,
   * we must undo this on code passed to geshifilter.
   *
   * @param string $text
   *   The original text.
   *
   * @return string
   *   The text unencoded.
   */
  public function unencode($text) {
    $text = html_entity_decode($text, ENT_QUOTES);
    return $text;
  }

  /**
   * Return when we need to decode html entities for this filter.
   *
   * @return bool
   *   Return TRUE if we need to decode the entities.
   */
  protected function decodeEntities() {
    if (!$this->config->get('use_format_specific_options')) {
      // Return global value.
      return $this->config->get('decode_entities');
    }
    // Return value for this filter.
    return $this->settings['general_tags']['decode_entities'];
  }

}