Passed
Branch master (e0fe6c)
by Arnaud
05:31
created

Convert   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 96.72%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 63
c 2
b 1
f 0
dl 0
loc 141
ccs 59
cts 61
cp 0.9672
rs 10
wmc 22

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A init() 0 7 2
A convertPage() 0 21 5
C process() 0 83 14
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\Pages;
12
13
use Cecil\Collection\Page\Page;
14
use Cecil\Converter\Converter;
15
use Cecil\Exception\Exception;
16
use Cecil\Step\AbstractStep;
17
18
/**
19
 * Converts content of all pages.
20
 */
21
class Convert extends AbstractStep
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 1
    public function getName(): string
27
    {
28 1
        return 'Converting pages';
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 1
    public function init($options)
35
    {
36 1
        parent::init($options);
37
38
        /** @var \Cecil\Builder $builder */
39 1
        if (is_dir($this->builder->getConfig()->getContentPath())) {
40 1
            $this->canProcess = true;
41
        }
42 1
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function process()
48
    {
49 1
        if (count($this->builder->getPages()) <= 0) {
50
            return;
51
        }
52
53 1
        if ($this->builder->getBuildOptions()['drafts']) {
54 1
            $this->builder->getLogger()->info('Drafts included');
55
        }
56
57 1
        $max = count($this->builder->getPages());
58 1
        $count = 0;
59
        /** @var Page $page */
60 1
        foreach ($this->builder->getPages() as $page) {
61 1
            if (!$page->isVirtual()) {
62 1
                $count++;
63
64
                try {
65 1
                    $convertedPage = $this->convertPage($page, (string) $this->config->get('frontmatter.format'));
66
                    // set default language
67 1
                    if (!$convertedPage->hasVariable('language')) {
68 1
                        $convertedPage->setVariable('language', $this->config->getLanguageDefault());
69
                    }
70 1
                } catch (Exception $e) {
71 1
                    $this->builder->getPages()->remove($page->getId());
72 1
                    $this->builder->getLogger()->error(
73 1
                        sprintf('Unable to convert page "%s"', $page->getId())
74
                    );
75 1
                    $this->builder->getLogger()->debug(
76 1
                        sprintf('%s: %s', $page->getId(), $e->getMessage())
77
                    );
78 1
                    continue;
79
                }
80
81
                /**
82
                 * Apply a custom path to pages of a specified section.
83
                 *
84
                 * ie:
85
                 * paths:
86
                 * - section: Blog
87
                 *   path: :section/:year/:month/:day/:slug
88
                 */
89 1
                if (is_array($this->config->get('paths'))) {
90 1
                    foreach ($this->config->get('paths') as $entry) {
91 1
                        if (isset($entry['section'])) {
92
                            /** @var Page $page */
93 1
                            if ($page->getSection() == Page::slugify($entry['section'])) {
94 1
                                if (isset($entry['path'])) {
95 1
                                    $path = str_replace(
96
                                        [
97 1
                                            ':year',
98
                                            ':month',
99
                                            ':day',
100
                                            ':section',
101
                                            ':slug',
102
                                        ],
103
                                        [
104 1
                                            $page->getVariable('date')->format('Y'),
105 1
                                            $page->getVariable('date')->format('m'),
106 1
                                            $page->getVariable('date')->format('d'),
107 1
                                            $page->getSection(),
108 1
                                            $page->getSlug(),
109
                                        ],
110 1
                                        $entry['path']
111
                                    );
112 1
                                    $page->setPath($path);
113
                                }
114
                            }
115
                        }
116
                    }
117
                }
118
119 1
                $message = $page->getId();
120
                // forces drafts convert?
121 1
                if ($this->builder->getBuildOptions()['drafts']) {
122 1
                    $page->setVariable('published', true);
123
                }
124 1
                if ($page->getVariable('published')) {
125 1
                    $this->builder->getPages()->replace($page->getId(), $convertedPage);
126
                } else {
127
                    $message .= ' (not published)';
128
                }
129 1
                $this->builder->getLogger()->info($message, ['progress' => [$count, $max]]);
130
            }
131
        }
132 1
    }
133
134
    /**
135
     * Converts page content:
136
     * - Yaml frontmatter to PHP array
137
     * - Markdown body to HTML.
138
     *
139
     * @throws Exception
140
     */
141 1
    public function convertPage(Page $page, $format = 'yaml'): Page
142
    {
143 1
        $converter = new Converter($this->builder);
144
        // converts frontmatter
145 1
        if ($page->getFrontmatter()) {
146
            try {
147 1
                $variables = $converter->convertFrontmatter($page->getFrontmatter(), $format);
148 1
            } catch (\Exception $e) {
149 1
                throw new Exception($e->getMessage());
150
            }
151 1
            $page->setFmVariables($variables);
152 1
            $page->setVariables($variables);
153
        }
154
155
        // converts body only if page is published
156 1
        if ($page->getVariable('published') || $this->options['drafts']) {
157 1
            $html = $converter->convertBody($page->getBody());
158 1
            $page->setBodyHtml($html);
159
        }
160
161 1
        return $page;
162
    }
163
}
164