HtmlExcerpt::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 11
rs 10
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