Passed
Push — images ( 5aeba8...dcd34d )
by Arnaud
02:48
created

PagesConvert::convertPage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 17
rs 9.9666
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Step;
12
13
use Cecil\Collection\Page\Page;
14
use Cecil\Converter\Converter;
15
use Cecil\Exception\Exception;
16
17
/**
18
 * Converts content of all pages.
19
 */
20
class PagesConvert extends AbstractStep
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function getName(): string
26
    {
27
        return 'Converting pages';
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function init($options)
34
    {
35
        /** @var \Cecil\Builder $builder */
36
        if (is_dir($this->builder->getConfig()->getContentPath())) {
37
            $this->canProcess = true;
38
        }
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function process()
45
    {
46
        if (count($this->builder->getPages()) <= 0) {
47
            return;
48
        }
49
50
        if ($this->builder->getBuildOptions()['drafts']) {
51
            $message = 'drafts included';
52
            $this->builder->getLogger()->info($message);
53
        }
54
55
        $max = count($this->builder->getPages());
56
        $count = 0;
57
        /** @var Page $page */
58
        foreach ($this->builder->getPages() as $page) {
59
            if (!$page->isVirtual()) {
60
                $count++;
61
62
                try {
63
                    $convertedPage = $this->convertPage($page, (string) $this->config->get('frontmatter.format'));
64
                } catch (Exception $e) {
65
                    $this->builder->getPages()->remove($page->getId());
66
67
                    $message = sprintf('Unable to convert front matter of page "%s"', $page->getId());
68
                    $this->builder->getLogger()->error($message);
69
                    $message = sprintf('Page "%s": %s', $page->getId(), $e->getMessage());
70
                    $this->builder->getLogger()->debug($message);
71
72
                    continue;
73
                }
74
                $message = $page->getId();
75
                // forces drafts convert?
76
                if ($this->builder->getBuildOptions()['drafts']) {
77
                    $page->setVariable('published', true);
78
                }
79
                if ($page->getVariable('published')) {
80
                    $this->builder->getPages()->replace($page->getId(), $convertedPage);
81
                } else {
82
                    $this->builder->getPages()->remove($page->getId());
83
                    $message .= ' (not published)';
84
                }
85
                $this->builder->getLogger()->info($message, ['progress' => [$count, $max]]);
86
            }
87
        }
88
    }
89
90
    /**
91
     * Converts page content:
92
     * - Yaml frontmatter to PHP array
93
     * - Markdown body to HTML.
94
     *
95
     * @param Page   $page
96
     * @param string $format
97
     *
98
     * @throws Exception
99
     *
100
     * @return Page
101
     */
102
    public function convertPage(Page $page, $format = 'yaml'): Page
103
    {
104
        // converts frontmatter
105
        if ($page->getFrontmatter()) {
106
            try {
107
                $variables = (new Converter($this->builder))->convertFrontmatter($page->getFrontmatter(), $format);
108
            } catch (\Exception $e) {
109
                throw new Exception($e->getMessage());
110
            }
111
            $page->setVariables($variables);
112
        }
113
114
        // converts body
115
        $html = (new Converter($this->builder))->convertBody($page->getBody());
116
        $page->setBodyHtml($html);
117
118
        return $page;
119
    }
120
}
121