FilterSyntaxHighlighter.php in Syntax Highlighter 8
File
src/Plugin/Filter/FilterSyntaxHighlighter.php
View source
<?php
namespace Drupal\syntaxhighlighter\Plugin\Filter;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
class FilterSyntaxHighlighter extends FilterBase {
public function prepare($text, $langcode) {
$config = \Drupal::config('syntaxhighlighter.settings');
$tag_name = $config
->get('tagname');
$pattern = "#<{$tag_name}\\s*([^>]*)>|</\\s*{$tag_name}>#";
preg_match_all($pattern, $text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
$output = '';
$at = 0;
$n = count($matches);
if ($n % 2 !== 0) {
return $text;
}
for ($i = 0; $i < $n; $i += 2) {
$open_tag = $matches[$i];
$close_tag = $matches[$i + 1];
if (strpos($open_tag[1][0], 'brush:')) {
$output .= substr($text, $at, $open_tag[0][1] - $at);
$begin = $open_tag[0][1] + strlen($open_tag[0][0]);
$length = $close_tag[0][1] - $begin;
$output .= '{' . SYNTAXHIGHLIGHTER_TAG_STRING . ' ' . $open_tag[1][0] . '}' . substr($text, $begin, $length) . '{/' . SYNTAXHIGHLIGHTER_TAG_STRING . '}';
$at = $close_tag[0][1] + strlen($close_tag[0][0]);
}
}
$output .= substr($text, $at);
return $output;
}
public function process($text, $langcode) {
$config = \Drupal::config('syntaxhighlighter.settings');
$patterns = [
'#{' . SYNTAXHIGHLIGHTER_TAG_STRING . ' ([^}]+)}#',
'#{/' . SYNTAXHIGHLIGHTER_TAG_STRING . '}#',
];
$tag_name = $config
->get('tagname');
$replacements = [
"<{$tag_name} \$1>",
"</{$tag_name}>",
];
$output = preg_replace($patterns, $replacements, $text);
return new FilterProcessResult($output);
}
public function tips($long = FALSE) {
return _syntaxhighlighter_filter_tips(0, 0, TRUE);
}
}