Passed
Push — master ( 8a89b1...4dbe19 )
by Arnaud
04:13 queued 12s
created

Convert   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 96.61%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 61
dl 0
loc 138
ccs 57
cts 59
cp 0.9661
rs 10
c 3
b 2
f 0
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 80 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": %s', $page->getId(), $e->getMessage())
74
                    );
75 1
                    continue;
76
                }
77
78
                /**
79
                 * Apply a custom path to pages of a specified section.
80
                 *
81
                 * ie:
82
                 * paths:
83
                 * - section: Blog
84
                 *   path: :section/:year/:month/:day/:slug
85
                 */
86 1
                if (is_array($this->config->get('paths'))) {
87 1
                    foreach ($this->config->get('paths') as $entry) {
88 1
                        if (isset($entry['section'])) {
89
                            /** @var Page $page */
90 1
                            if ($page->getSection() == Page::slugify($entry['section'])) {
91 1
                                if (isset($entry['path'])) {
92 1
                                    $path = str_replace(
93
                                        [
94 1
                                            ':year',
95
                                            ':month',
96
                                            ':day',
97
                                            ':section',
98
                                            ':slug',
99
                                        ],
100
                                        [
101 1
                                            $page->getVariable('date')->format('Y'),
102 1
                                            $page->getVariable('date')->format('m'),
103 1
                                            $page->getVariable('date')->format('d'),
104 1
                                            $page->getSection(),
105 1
                                            $page->getSlug(),
106
                                        ],
107 1
                                        $entry['path']
108
                                    );
109 1
                                    $page->setPath($path);
110
                                }
111
                            }
112
                        }
113
                    }
114
                }
115
116 1
                $message = $page->getId();
117
                // forces drafts convert?
118 1
                if ($this->builder->getBuildOptions()['drafts']) {
119 1
                    $page->setVariable('published', true);
120
                }
121 1
                if ($page->getVariable('published')) {
122 1
                    $this->builder->getPages()->replace($page->getId(), $convertedPage);
123
                } else {
124
                    $message .= ' (not published)';
125
                }
126 1
                $this->builder->getLogger()->info($message, ['progress' => [$count, $max]]);
127
            }
128
        }
129 1
    }
130
131
    /**
132
     * Converts page content:
133
     * - Yaml frontmatter to PHP array
134
     * - Markdown body to HTML.
135
     *
136
     * @throws Exception
137
     */
138 1
    public function convertPage(Page $page, $format = 'yaml'): Page
139
    {
140 1
        $converter = new Converter($this->builder);
141
        // converts frontmatter
142 1
        if ($page->getFrontmatter()) {
143
            try {
144 1
                $variables = $converter->convertFrontmatter($page->getFrontmatter(), $format);
145 1
            } catch (\Exception $e) {
146 1
                throw new Exception($e->getMessage());
147
            }
148 1
            $page->setFmVariables($variables);
149 1
            $page->setVariables($variables);
150
        }
151
152
        // converts body only if page is published
153 1
        if ($page->getVariable('published') || $this->options['drafts']) {
154 1
            $html = $converter->convertBody($page->getBody());
155 1
            $page->setBodyHtml($html);
156
        }
157
158 1
        return $page;
159
    }
160
}
161