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

Cecilapp / Cecil / 5046064611

pending completion
5046064611

push

github

GitHub
perf: native_function_invocation (#1697)

322 of 322 new or added lines in 62 files covered. (100.0%)

2784 of 4121 relevant lines covered (67.56%)

0.68 hits per line

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

80.0
/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_dir($this->config->getPagesPath())) {
1✔
49
            $this->canProcess = true;
1✔
50
        }
51
    }
52

53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function process(): void
57
    {
58
        if (\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->getPageFile(), $e->getPageLine(), $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
                // forces drafts convert?
86
                if ($this->builder->getBuildOptions()['drafts']) {
1✔
87
                    $page->setVariable('published', true);
1✔
88
                }
89
                if ($page->getVariable('published')) {
1✔
90
                    $this->builder->getPages()->replace($page->getId(), $convertedPage);
1✔
91
                } else {
92
                    $message .= ' (not published)';
×
93
                }
94
                $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
1✔
95
            }
96
        }
97
    }
98

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

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

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

132
        return $page;
1✔
133
    }
134
}
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