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