Passed
Push — php-di ( 53e65d...9c61c8 )
by Arnaud
03:45
created

Convert::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Step\Pages;
15
16
use Cecil\Builder;
17
use Cecil\Collection\Page\Page;
18
use Cecil\Config;
19
use Cecil\Converter\Converter;
20
use Cecil\Converter\ConverterInterface;
21
use Cecil\Exception\RuntimeException;
22
use Cecil\Step\AbstractStep;
23
use Cecil\Util;
24
use DI\Attribute\Inject;
25
use Psr\Log\LoggerInterface;
26
27
/**
28
 * Convert step.
29
 *
30
 * This step is responsible for converting pages from their source format
31
 * (i.e. Markdown) to HTML, applying front matter processing,
32
 * and ensuring that the pages are ready for rendering. It handles both
33
 * published and draft pages, depending on the build options.
34
 */
35
class Convert extends AbstractStep
36
{
37
    #[Inject]
38 1
    private Converter $converter;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getName(): string
44 1
    {
45 1
        if ($this->builder->getBuildOptions()['drafts']) {
46
            return 'Converting pages (drafts included)';
47
        }
48
49
        return 'Converting pages';
50 1
    }
51
52 1
    /**
53 1
     * {@inheritdoc}
54
     */
55
    public function init(array $options): void
56
    {
57
        parent::init($options);
58
59
        if (\is_null($this->builder->getPages())) {
60
            $this->canProcess = false;
61
        }
62 1
    }
63
64 1
    /**
65
     * {@inheritdoc}
66 1
     */
67
    public function process(): void
68
    {
69
        if (!is_iterable($this->builder->getPages()) || \count($this->builder->getPages()) == 0) {
70
            return;
71
        }
72
73
        $total = \count($this->builder->getPages());
74 1
        $count = 0;
75
        /** @var Page $page */
76 1
        foreach ($this->builder->getPages() as $page) {
77
            if (!$page->isVirtual()) {
78
                $count++;
79
80 1
                try {
81 1
                    $convertedPage = $this->convertPage($this->builder, $page);
82
                    // set default language (ex: "en") if necessary
83 1
                    if ($convertedPage->getVariable('language') === null) {
84 1
                        $convertedPage->setVariable('language', $this->config->getLanguageDefault());
85 1
                    }
86
                } catch (RuntimeException $e) {
87
                    $this->logger->error(\sprintf('Unable to convert "%s:%s": %s', $e->getFile(), $e->getLine(), $e->getMessage()));
88 1
                    $this->builder->getPages()->remove($page->getId());
89
                    continue;
90 1
                } catch (\Exception $e) {
91 1
                    $this->logger->error(\sprintf('Unable to convert "%s": %s', Util::joinPath(Util\File::getFS()->makePathRelative($page->getFilePath(), $this->config->getPagesPath())), $e->getMessage()));
92
                    $this->builder->getPages()->remove($page->getId());
93 1
                    continue;
94 1
                }
95 1
                $message = \sprintf('Page "%s" converted', $page->getId());
96 1
                $statusMessage = ' (not published)';
97
                // forces drafts convert?
98
                if ($this->builder->getBuildOptions()['drafts']) {
99
                    $page->setVariable('published', true);
100
                }
101
                // replaces page in collection
102 1
                if ($page->getVariable('published')) {
103 1
                    $this->builder->getPages()->replace($page->getId(), $convertedPage);
104
                    $statusMessage = '';
105 1
                }
106 1
                $this->logger->info($message . $statusMessage, ['progress' => [$count, $total]]);
107
            }
108
        }
109 1
    }
110 1
111 1
    /**
112
     * Converts page content:
113 1
     *  - front matter to PHP array
114
     *  - body to HTML.
115
     *
116
     * @throws RuntimeException
117
     */
118
    public function convertPage(Builder $builder, Page $page, ?string $format = null, ?ConverterInterface $converter = null): Page
119
    {
120
        $format = $format ?? (string) $builder->getConfig()->get('pages.frontmatter');
121
        $converter = $converter ?? $this->converter;
122
123
        // converts front matter
124
        if ($page->getFrontmatter()) {
125 1
            try {
126
                $variables = $converter->convertFrontmatter($page->getFrontmatter(), $format);
127 1
            } catch (RuntimeException $e) {
128 1
                throw new RuntimeException($e->getMessage(), file: $page->getFilePath(), line: $e->getLine());
129
            }
130
            $page->setFmVariables($variables);
131 1
            $page->setVariables($variables);
132
        }
133 1
134 1
        // converts body (only if page is published or drafts option is enabled)
135 1
        if ($page->getVariable('published') || $this->options['drafts']) {
136
            try {
137 1
                $html = $converter->convertBody($page->getBody());
138 1
            } catch (RuntimeException $e) {
139
                throw new \Exception($e->getMessage());
140
            }
141
            $page->setBodyHtml($html);
142 1
        }
143
144 1
        return $page;
145
    }
146
}
147