Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
89.47% |
17 / 19 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
Markdown | |
89.47% |
17 / 19 |
|
66.67% |
2 / 3 |
3.01 | |
0.00% |
0 / 1 |
__construct | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
1.06 | |||
setSafeMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
build | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Mvc; |
6 | |
7 | use PeServer\Core\Html\CodeHighlighter; |
8 | use PeServer\Core\Regex; |
9 | use PeServer\Core\Text; |
10 | |
11 | class Markdown |
12 | { |
13 | #region variable |
14 | |
15 | /** |
16 | * Markdown |
17 | * |
18 | * @var \Michelf\Markdown|\Michelf\MarkdownExtra |
19 | */ |
20 | private \Michelf\Markdown $parser; |
21 | |
22 | private bool $isSafeMode = true; // @phpstan-ignore-line |
23 | |
24 | #endregion |
25 | |
26 | /** @SuppressWarnings(PHPMD.MissingImport) */ |
27 | public function __construct() |
28 | { |
29 | $this->parser = new \Michelf\MarkdownExtra(); |
30 | $this->parser->code_block_content_func = function ($code, $language) { |
31 | $codeHighlighter = new CodeHighlighter(); |
32 | return '<!-- {CODE -->' . $codeHighlighter->toHtml($language, $code) . '<!-- CODE} -->'; |
33 | }; |
34 | } |
35 | |
36 | #region function |
37 | |
38 | public function setSafeMode(bool $isSafeMode): void |
39 | { |
40 | $this->isSafeMode = $isSafeMode; |
41 | } |
42 | |
43 | public function build(string $markdown): string |
44 | { |
45 | $html = $this->parser->transform($markdown); |
46 | |
47 | $regex = new Regex(); |
48 | $trimmedHead = $regex->replace( |
49 | $html, |
50 | '/<pre><code(\s+class=".+")?><!-- {CODE -->/', |
51 | Text::EMPTY |
52 | ); |
53 | $trimmedTail = Text::replace( |
54 | $trimmedHead, |
55 | '</code></pre><!-- CODE} -->', |
56 | Text::EMPTY |
57 | ); |
58 | |
59 | return $trimmedTail; |
60 | } |
61 | |
62 | #endregion |
63 | } |