1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Code highlighter filter.
|
|
|
19 |
*
|
|
|
20 |
* Filter converting text code into a well-styled block of code.
|
|
|
21 |
*
|
|
|
22 |
* @package filter_codehighlighter
|
|
|
23 |
* @copyright 2023 Meirza <meirza.arson@moodle.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
class filter_codehighlighter extends moodle_text_filter {
|
|
|
27 |
/**
|
|
|
28 |
* Apply the filter to the text
|
|
|
29 |
*
|
|
|
30 |
* @param string $text to be processed by the text
|
|
|
31 |
* @param array $options filter options
|
|
|
32 |
* @return string text after processing
|
|
|
33 |
*/
|
|
|
34 |
public function filter($text, array $options = []): string {
|
|
|
35 |
global $PAGE;
|
|
|
36 |
|
|
|
37 |
if (!isset($options['originalformat'])) {
|
|
|
38 |
return $text;
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
// The pattern.
|
|
|
42 |
$re = '/<pre.+?class=".*?language-.*?"><code>/i';
|
|
|
43 |
|
|
|
44 |
// Stops looking after the first match.
|
|
|
45 |
preg_match($re, $text, $matches);
|
|
|
46 |
if ($matches) {
|
|
|
47 |
$PAGE->requires->js_call_amd('filter_codehighlighter/prism-init');
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
return $text;
|
|
|
51 |
}
|
|
|
52 |
}
|