|
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\Collection\Page\Collection as PagesCollection; |
|
17
|
|
|
use Cecil\Collection\Page\Page; |
|
18
|
|
|
use Cecil\Step\AbstractStep; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Creates Pages collection from content iterator. |
|
22
|
|
|
*/ |
|
23
|
|
|
class Create extends AbstractStep |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function getName(): string |
|
29
|
|
|
{ |
|
30
|
1 |
|
return 'Creating pages'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
1 |
|
public function init(array $options): void |
|
37
|
|
|
{ |
|
38
|
1 |
|
$this->builder->setPages(new PagesCollection('all-pages')); |
|
39
|
|
|
|
|
40
|
1 |
|
if (is_dir($this->config->getPagesPath())) { |
|
41
|
1 |
|
$this->canProcess = true; |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function process(): void |
|
49
|
|
|
{ |
|
50
|
1 |
|
if (!is_iterable($this->builder->getPagesFiles()) || \count($this->builder->getPagesFiles()) == 0) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
$total = \count($this->builder->getPagesFiles()); |
|
55
|
1 |
|
$count = 0; |
|
56
|
|
|
|
|
57
|
1 |
|
foreach ($this->builder->getPagesFiles() as $file) { |
|
58
|
1 |
|
$count++; |
|
59
|
|
|
// create a page from its (Markdown) file |
|
60
|
1 |
|
$page = new Page(Page::createIdFromFile($file)); |
|
61
|
1 |
|
$page->setFile($file); |
|
62
|
|
|
// parse frontmatter and body |
|
63
|
1 |
|
$page->parse(); |
|
64
|
|
|
|
|
65
|
|
|
/* |
|
66
|
|
|
* Apply an - optional - custom path to pages of a section. |
|
67
|
|
|
* |
|
68
|
|
|
* ```yaml |
|
69
|
|
|
* paths: |
|
70
|
|
|
* - section: Blog |
|
71
|
|
|
* language: fr # optional |
|
72
|
|
|
* path: :section/:year/:month/:day/:slug |
|
73
|
|
|
* ``` |
|
74
|
|
|
*/ |
|
75
|
1 |
|
if (\is_array($this->config->get('paths'))) { |
|
76
|
|
|
foreach ($this->config->get('paths') as $entry) { |
|
77
|
|
|
if (isset($entry['section'])) { |
|
78
|
|
|
/** @var Page $page */ |
|
79
|
|
|
if ($page->getSection() == Page::slugify($entry['section'])) { |
|
80
|
|
|
if (isset($entry['language']) && $entry['language'] != $page->getVariable('language')) { |
|
81
|
|
|
break; |
|
82
|
|
|
} |
|
83
|
|
|
if (isset($entry['path'])) { |
|
84
|
|
|
$path = str_replace( |
|
85
|
|
|
[ |
|
86
|
|
|
':year', |
|
87
|
|
|
':month', |
|
88
|
|
|
':day', |
|
89
|
|
|
':section', |
|
90
|
|
|
':slug', |
|
91
|
|
|
], |
|
92
|
|
|
[ |
|
93
|
|
|
$page->getVariable('date')->format('Y'), |
|
94
|
|
|
$page->getVariable('date')->format('m'), |
|
95
|
|
|
$page->getVariable('date')->format('d'), |
|
96
|
|
|
$page->getSection(), |
|
97
|
|
|
$page->getSlug(), |
|
98
|
|
|
], |
|
99
|
|
|
$entry['path'] |
|
100
|
|
|
); |
|
101
|
|
|
$page->setPath(trim($path, '/')); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// add the page to pages collection only if its language is defined in configuration |
|
109
|
1 |
|
if (\in_array($page->getVariable('language', $this->config->getLanguageDefault()), array_column($this->config->getLanguages(), 'code'))) { |
|
110
|
1 |
|
$this->builder->getPages()->add($page); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
1 |
|
$message = \sprintf('Page "%s" created', $page->getId()); |
|
114
|
1 |
|
$this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|