|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Cecil. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
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']) { |
|
35
|
|
|
return 'Converting pages (drafts included)'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return 'Converting pages'; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
public function init(array $options): void |
|
45
|
|
|
{ |
|
46
|
|
|
parent::init($options); |
|
47
|
|
|
|
|
48
|
|
|
if (is_dir($this->builder->getConfig()->getPagesPath())) { |
|
49
|
|
|
$this->canProcess = true; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function process(): void |
|
57
|
|
|
{ |
|
58
|
|
|
if (count($this->builder->getPages()) == 0) { |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$max = count($this->builder->getPages()); |
|
63
|
|
|
$count = 0; |
|
64
|
|
|
/** @var Page $page */ |
|
65
|
|
|
foreach ($this->builder->getPages() as $page) { |
|
66
|
|
|
if (!$page->isVirtual()) { |
|
67
|
|
|
$count++; |
|
68
|
|
|
|
|
69
|
|
|
try { |
|
70
|
|
|
$convertedPage = $this->convertPage($this->builder, $page); |
|
71
|
|
|
// set default language (ex: "en") if necessary |
|
72
|
|
|
if ($convertedPage->getVariable('language') === null) { |
|
73
|
|
|
$convertedPage->setVariable('language', $this->config->getLanguageDefault()); |
|
74
|
|
|
} |
|
75
|
|
|
} catch (RuntimeException $e) { |
|
76
|
|
|
$this->builder->getLogger()->error(\sprintf('Unable to convert "%s:%s": %s', $e->getPageFile(), $e->getPageLine(), $e->getMessage())); |
|
77
|
|
|
$this->builder->getPages()->remove($page->getId()); |
|
78
|
|
|
continue; |
|
79
|
|
|
} catch (\Exception $e) { |
|
80
|
|
|
$this->builder->getLogger()->error(\sprintf('Unable to convert "%s": %s', Util::joinPath(Util\File::getFS()->makePathRelative($page->getFilePath(), $this->builder->getConfig()->getPagesPath())), $e->getMessage())); |
|
81
|
|
|
$this->builder->getPages()->remove($page->getId()); |
|
82
|
|
|
continue; |
|
83
|
|
|
} |
|
84
|
|
|
$message = \sprintf('Page "%s" converted', $page->getId()); |
|
85
|
|
|
// forces drafts convert? |
|
86
|
|
|
if ($this->builder->getBuildOptions()['drafts']) { |
|
87
|
|
|
$page->setVariable('published', true); |
|
88
|
|
|
} |
|
89
|
|
|
if ($page->getVariable('published')) { |
|
90
|
|
|
$this->builder->getPages()->replace($page->getId(), $convertedPage); |
|
91
|
|
|
} else { |
|
92
|
|
|
$message .= ' (not published)'; |
|
93
|
|
|
} |
|
94
|
|
|
$this->builder->getLogger()->info($message, ['progress' => [$count, $max]]); |
|
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 ?? $builder->getConfig()->get('frontmatter.format') ?? 'yaml'; |
|
109
|
|
|
$converter = $converter ?? new Converter($builder); |
|
110
|
|
|
|
|
111
|
|
|
// converts front matter |
|
112
|
|
|
if ($page->getFrontmatter()) { |
|
113
|
|
|
try { |
|
114
|
|
|
$variables = $converter->convertFrontmatter($page->getFrontmatter(), $format); |
|
115
|
|
|
} catch (RuntimeException $e) { |
|
116
|
|
|
throw new RuntimeException($e->getMessage(), $page->getFilePath(), $e->getPageLine()); |
|
117
|
|
|
} |
|
118
|
|
|
$page->setFmVariables($variables); |
|
119
|
|
|
$page->setVariables($variables); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// converts body (only if page is published or drafts option is enabled) |
|
123
|
|
|
if ($page->getVariable('published') || $this->options['drafts']) { |
|
124
|
|
|
try { |
|
125
|
|
|
$html = $converter->convertBody($page->getBody()); |
|
126
|
|
|
} catch (RuntimeException $e) { |
|
127
|
|
|
throw new \Exception($e->getMessage()); |
|
128
|
|
|
} |
|
129
|
|
|
$page->setBodyHtml($html); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $page; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|