public function GeshiFilterFilter::prepare in GeSHi Filter for syntax highlighting 8
Same name and namespace in other branches
- 8.2 src/Plugin/Filter/GeshiFilterFilter.php \Drupal\geshifilter\Plugin\Filter\GeshiFilterFilter::prepare()
Prepares the text for processing.
Filters should not use the prepare method for anything other than escaping, because that would short-circuit the control the user has over the order in which filters are applied.
Parameters
string $text: The text string to be filtered.
string $langcode: The language code of the text to be filtered.
Return value
string The prepared, escaped text.
Overrides FilterBase::prepare
File
- src/
Plugin/ Filter/ GeshiFilterFilter.php, line 134
Class
- GeshiFilterFilter
- Provides a base filter for Geshi Filter.
Namespace
Drupal\geshifilter\Plugin\FilterCode
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;
}