1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cecil. |
5
|
|
|
* |
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
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
|
|
|
* Create pages step. |
22
|
|
|
* |
23
|
|
|
* This step is responsible for creating pages from Markdown files |
24
|
|
|
* located in the configured pages directory. It initializes a collection |
25
|
|
|
* of pages, processes each Markdown file to create a `Page` object, |
26
|
|
|
* and applies any custom path configurations defined in the site configuration. |
27
|
|
|
* The created pages are then added to the pages collection, which can be |
28
|
|
|
* used later in the build process. |
29
|
|
|
*/ |
30
|
|
|
class Create extends AbstractStep |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
1 |
|
public function getName(): string |
36
|
|
|
{ |
37
|
1 |
|
return 'Creating pages'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
1 |
|
public function init(array $options): void |
44
|
|
|
{ |
45
|
1 |
|
$this->builder->setPages(new PagesCollection('all-pages')); |
46
|
|
|
|
47
|
1 |
|
if (is_dir($this->config->getPagesPath())) { |
48
|
1 |
|
$this->canProcess = true; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function process(): void |
56
|
|
|
{ |
57
|
1 |
|
if (!is_iterable($this->builder->getPagesFiles()) || \count($this->builder->getPagesFiles()) == 0) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$total = \count($this->builder->getPagesFiles()); |
62
|
1 |
|
$count = 0; |
63
|
|
|
|
64
|
1 |
|
foreach ($this->builder->getPagesFiles() as $file) { |
65
|
1 |
|
$count++; |
66
|
|
|
// create page from the file |
67
|
1 |
|
$page = new Page($file); |
68
|
|
|
// parse frontmatter and body |
69
|
1 |
|
$page->parse(); |
70
|
|
|
|
71
|
|
|
/* |
72
|
|
|
* Apply a custom path to pages of a section. |
73
|
|
|
* |
74
|
|
|
* ```yaml |
75
|
|
|
* paths: |
76
|
|
|
* - section: Blog |
77
|
|
|
* path: :section/:year/:month/:day/:slug |
78
|
|
|
* ``` |
79
|
|
|
*/ |
80
|
1 |
|
if (\is_array($this->config->get('pages.paths', $page->getVariable('language')))) { |
81
|
1 |
|
foreach ($this->config->get('pages.paths', $page->getVariable('language')) as $entry) { |
82
|
1 |
|
if (isset($entry['section'])) { |
83
|
|
|
/** @var Page $page */ |
84
|
1 |
|
if ($page->getSection() == Page::slugify($entry['section'])) { |
85
|
1 |
|
if (isset($entry['path'])) { |
86
|
1 |
|
$path = str_replace( |
87
|
1 |
|
[ |
88
|
1 |
|
':year', |
89
|
1 |
|
':month', |
90
|
1 |
|
':day', |
91
|
1 |
|
':section', |
92
|
1 |
|
':slug', |
93
|
1 |
|
], |
94
|
1 |
|
[ |
95
|
1 |
|
$page->getVariable('date')->format('Y'), |
96
|
1 |
|
$page->getVariable('date')->format('m'), |
97
|
1 |
|
$page->getVariable('date')->format('d'), |
98
|
1 |
|
$page->getSection(), |
99
|
1 |
|
$page->getSlug(), |
100
|
1 |
|
], |
101
|
1 |
|
$entry['path'] |
102
|
1 |
|
); |
103
|
1 |
|
$page->setPath(trim($path, '/')); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// add the page to pages collection only if its language is defined in configuration |
111
|
1 |
|
if (\in_array($page->getVariable('language', $this->config->getLanguageDefault()), array_column($this->config->getLanguages(), 'code'))) { |
112
|
1 |
|
$this->builder->getPages()->add($page); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
$message = \sprintf('Page "%s" created', $page->getId()); |
116
|
1 |
|
$this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|