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 process() |
24
|
|
|
{ |
25
|
|
|
if (count($this->builder->getPages()) <= 0) { |
26
|
|
|
return; |
27
|
|
|
} |
28
|
|
|
$message = 'Converting pages'; |
29
|
|
|
if ($this->builder->getBuildOptions()['drafts']) { |
30
|
|
|
$message .= ' (drafts included)'; |
31
|
|
|
} |
32
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['CONVERT', $message]); |
33
|
|
|
$max = count($this->builder->getPages()); |
34
|
|
|
$count = 0; |
35
|
|
|
$countError = 0; |
36
|
|
|
/* @var $page Page */ |
37
|
|
|
foreach ($this->builder->getPages() as $page) { |
38
|
|
|
if (!$page->isVirtual()) { |
39
|
|
|
$count++; |
40
|
|
|
$convertedPage = $this->convertPage($page, $this->builder->getConfig()->get('frontmatter.format')); |
41
|
|
|
if (false !== $convertedPage) { |
42
|
|
|
$message = $page->getPathname(); |
43
|
|
|
// force convert drafts? |
44
|
|
|
if ($this->builder->getBuildOptions()['drafts']) { |
45
|
|
|
$page->setVariable('published', true); |
46
|
|
|
} |
47
|
|
|
if ($page->getVariable('published')) { |
48
|
|
|
$this->builder->getPages()->replace($page->getId(), $convertedPage); |
49
|
|
|
} else { |
50
|
|
|
$this->builder->getPages()->remove($page->getId()); |
51
|
|
|
$message .= ' (not published)'; |
52
|
|
|
} |
53
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['CONVERT_PROGRESS', $message, $count, $max]); |
54
|
|
|
} else { |
55
|
|
|
$this->builder->getPages()->remove($page->getId()); |
56
|
|
|
$countError++; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
if ($countError > 0) { |
61
|
|
|
$message = sprintf('Number of errors: %s', $countError); |
62
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['CONVERT_ERROR', $message]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Converts page content: |
68
|
|
|
* - Yaml frontmatter to PHP array |
69
|
|
|
* - Markdown body to HTML. |
70
|
|
|
* |
71
|
|
|
* @param Page $page |
72
|
|
|
* @param string $format |
73
|
|
|
* |
74
|
|
|
* @return Page |
75
|
|
|
*/ |
76
|
|
|
public function convertPage(Page $page, $format = 'yaml') |
77
|
|
|
{ |
78
|
|
|
// converts frontmatter |
79
|
|
|
if ($page->getFrontmatter()) { |
|
|
|
|
80
|
|
|
try { |
81
|
|
|
$variables = Converter::convertFrontmatter($page->getFrontmatter(), $format); |
82
|
|
|
} catch (Exception $e) { |
83
|
|
|
$message = sprintf("Unable to convert frontmatter of '%s': %s", $page->getId(), $e->getMessage()); |
84
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['CONVERT_ERROR', $message]); |
85
|
|
|
|
86
|
|
|
return false; |
87
|
|
|
} |
88
|
|
|
$page->setVariables($variables); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// converts body |
92
|
|
|
$html = Converter::convertBody($page->getBody()); |
93
|
|
|
$page->setHtml($html); |
94
|
|
|
|
95
|
|
|
return $page; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: