HtmlExcerpt   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 16
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 11 2
1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cecil\Renderer\PostProcessor;
15
16
use Cecil\Collection\Page\Page;
17
18
/**
19
 * HtmlExcerpt class.
20
 *
21
 * This class processes HTML output to insert an excerpt marker
22
 * at the first occurrence of an HTML comment indicating an excerpt or break.
23
 * The marker is inserted as a `<span id="more"></span>` element.
24
 */
25
class HtmlExcerpt extends AbstractPostProcessor
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function process(Page $page, string $output, string $format): string
31
    {
32
        if ($format == 'html') {
33
            // https://regex101.com/r/Xl7d5I/3
34
            $pattern = '(.*)(<!--[[:blank:]]?(excerpt|break)[[:blank:]]?-->)(.*)';
35
            $replacement = '$1<span id="more"></span>$4';
36
            $excerpt = preg_replace('/' . $pattern . '/is', $replacement, $output, 1);
37
            $output = $excerpt ?? $output;
38
        }
39
40
        return $output;
41
    }
42
}
43