Passed
Push — nested-sections ( 2d40b2...7dbf34 )
by Arnaud
12:03 queued 05:37
created

HtmlExcerpt::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Renderer\PostProcessor;
15
16
use Cecil\Collection\Page\Page;
17
18
/**
19
 * HtmlExcerpt class.
20
 *
21
 * Replaces excerpt or break tag by HTML anchor.
22
 */
23
class HtmlExcerpt extends AbstractPostProcessor
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function process(Page $page, string $output, string $format): string
29
    {
30
        if ($format == 'html') {
31
            // https://regex101.com/r/Xl7d5I/3
32
            $pattern = '(.*)(<!--[[:blank:]]?(excerpt|break)[[:blank:]]?-->)(.*)';
33
            $replacement = '$1<span id="more"></span>$4';
34
            $excerpt = preg_replace('/' . $pattern . '/is', $replacement, $output, 1);
35
            $output = $excerpt ?? $output;
36
        }
37
38
        return $output;
39
    }
40
}
41