• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 15444845771

04 Jun 2025 02:19PM UTC coverage: 82.811% (+0.04%) from 82.775%
15444845771

push

github

ArnaudLigny
refactor: code quality

5 of 5 new or added lines in 1 file covered. (100.0%)

17 existing lines in 3 files now uncovered.

3117 of 3764 relevant lines covered (82.81%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.43
/src/Step/Pages/Convert.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
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\Step\Pages;
15

16
use Cecil\Builder;
17
use Cecil\Collection\Page\Page;
18
use Cecil\Converter\Converter;
19
use Cecil\Converter\ConverterInterface;
20
use Cecil\Exception\RuntimeException;
21
use Cecil\Step\AbstractStep;
22
use Cecil\Util;
23

24
/**
25
 * Converts content of all pages.
26
 */
27
class Convert extends AbstractStep
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function getName(): string
33
    {
34
        if ($this->builder->getBuildOptions()['drafts']) {
1✔
35
            return 'Converting pages (drafts included)';
1✔
36
        }
37

38
        return 'Converting pages';
×
39
    }
40

41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function init(array $options): void
45
    {
46
        parent::init($options);
1✔
47

48
        if (\is_null($this->builder->getPages())) {
1✔
49
            $this->canProcess = false;
×
50
        }
51
    }
52

53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function process(): void
57
    {
58
        if (!is_iterable($this->builder->getPages()) || \count($this->builder->getPages()) == 0) {
1✔
59
            return;
×
60
        }
61

62
        $total = \count($this->builder->getPages());
1✔
63
        $count = 0;
1✔
64
        /** @var Page $page */
65
        foreach ($this->builder->getPages() as $page) {
1✔
66
            if (!$page->isVirtual()) {
1✔
67
                $count++;
1✔
68

69
                try {
70
                    $convertedPage = $this->convertPage($this->builder, $page);
1✔
71
                    // set default language (ex: "en") if necessary
72
                    if ($convertedPage->getVariable('language') === null) {
1✔
73
                        $convertedPage->setVariable('language', $this->config->getLanguageDefault());
1✔
74
                    }
75
                } catch (RuntimeException $e) {
1✔
76
                    $this->builder->getLogger()->error(\sprintf('Unable to convert "%s:%s": %s', $e->getFile(), $e->getLine(), $e->getMessage()));
1✔
77
                    $this->builder->getPages()->remove($page->getId());
1✔
78
                    continue;
1✔
79
                } catch (\Exception $e) {
×
80
                    $this->builder->getLogger()->error(\sprintf('Unable to convert "%s": %s', Util::joinPath(Util\File::getFS()->makePathRelative($page->getFilePath(), $this->config->getPagesPath())), $e->getMessage()));
×
81
                    $this->builder->getPages()->remove($page->getId());
×
82
                    continue;
×
83
                }
84
                $message = \sprintf('Page "%s" converted', $page->getId());
1✔
85
                $statusMessage = ' (not published)';
1✔
86
                // forces drafts convert?
87
                if ($this->builder->getBuildOptions()['drafts']) {
1✔
88
                    $page->setVariable('published', true);
1✔
89
                }
90
                // replaces page in collection
91
                if ($page->getVariable('published')) {
1✔
92
                    $this->builder->getPages()->replace($page->getId(), $convertedPage);
1✔
93
                    $statusMessage = '';
1✔
94
                }
95
                $this->builder->getLogger()->info($message . $statusMessage, ['progress' => [$count, $total]]);
1✔
96
            }
97
        }
98
    }
99

100
    /**
101
     * Converts page content:
102
     *  - front matter to PHP array
103
     *  - body to HTML.
104
     *
105
     * @throws RuntimeException
106
     */
107
    public function convertPage(Builder $builder, Page $page, ?string $format = null, ?ConverterInterface $converter = null): Page
108
    {
109
        $format = $format ?? (string) $builder->getConfig()->get('pages.frontmatter');
1✔
110
        $converter = $converter ?? new Converter($builder);
1✔
111

112
        // converts front matter
113
        if ($page->getFrontmatter()) {
1✔
114
            try {
115
                $variables = $converter->convertFrontmatter($page->getFrontmatter(), $format);
1✔
116
            } catch (RuntimeException $e) {
1✔
117
                throw new RuntimeException($e->getMessage(), file: $page->getFilePath(), line: $e->getLine());
1✔
118
            }
119
            $page->setFmVariables($variables);
1✔
120
            $page->setVariables($variables);
1✔
121
        }
122

123
        // converts body (only if page is published or drafts option is enabled)
124
        if ($page->getVariable('published') || $this->options['drafts']) {
1✔
125
            try {
126
                $html = $converter->convertBody($page->getBody());
1✔
127
            } catch (RuntimeException $e) {
×
UNCOV
128
                throw new \Exception($e->getMessage());
×
129
            }
130
            $page->setBodyHtml($html);
1✔
131
        }
132

133
        return $page;
1✔
134
    }
135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc