|
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\Renderer; |
|
15
|
|
|
|
|
16
|
|
|
use Cecil\Builder; |
|
17
|
|
|
use Cecil\Collection\Page\Page as PageItem; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Page renderer class. |
|
21
|
|
|
* |
|
22
|
|
|
* This class is responsible for generating the output file path and URL for a page |
|
23
|
|
|
* based on the output format properties defined in the configuration. |
|
24
|
|
|
* It handles various scenarios such as ugly URLs, multilingual support, and subpaths. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Page |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Builder object. |
|
30
|
|
|
* @var Builder |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $builder; |
|
33
|
1 |
|
/** |
|
34
|
|
|
* Configuration object. |
|
35
|
1 |
|
* @var \Cecil\Config |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $config; |
|
38
|
|
|
/** |
|
39
|
|
|
* Page item. |
|
40
|
|
|
* @var PageItem |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $page; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct(Builder $builder, PageItem $page) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->builder = $builder; |
|
47
|
|
|
$this->config = $this->builder->getConfig(); |
|
48
|
|
|
$this->page = $page; |
|
49
|
|
|
} |
|
50
|
1 |
|
|
|
51
|
|
|
/** |
|
52
|
1 |
|
* Returns the path of the rendered page, based on the output format properties. |
|
53
|
1 |
|
* Use cases: |
|
54
|
1 |
|
* - default: path + filename + extension (e.g.: 'blog/post-1/index.html') |
|
55
|
1 |
|
* - with subpath: path + subpath + filename + extension (e.g.: 'blog/post-1/amp/index.html') |
|
56
|
1 |
|
* - ugly URL: path + extension (e.g.: '404.html', 'sitemap.xml', 'robots.txt') |
|
57
|
1 |
|
* - path only (e.g.: '_redirects') |
|
58
|
|
|
* - i18n: language code + default (e.g.: 'fr/blog/page/index.html') |
|
59
|
1 |
|
* |
|
60
|
1 |
|
* @param string $format Output format (ie: 'html', 'amp', 'json', etc.) |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getOutputFilePath(string $format): string |
|
63
|
1 |
|
{ |
|
64
|
1 |
|
$path = $this->page->getPath(); |
|
65
|
|
|
$subpath = (string) $this->config->getOutputFormatProperty($format, 'subpath'); |
|
66
|
|
|
$filename = (string) $this->config->getOutputFormatProperty($format, 'filename'); |
|
67
|
1 |
|
$extension = (string) $this->config->getOutputFormatProperty($format, 'extension'); |
|
68
|
1 |
|
$uglyurl = (bool) $this->page->getVariable('uglyurl'); |
|
69
|
|
|
$language = $this->page->getVariable('language'); |
|
70
|
|
|
// is ugly URL? |
|
71
|
1 |
|
if ($uglyurl) { |
|
72
|
1 |
|
$filename = ''; |
|
73
|
|
|
} |
|
74
|
|
|
// add extension if exists |
|
75
|
1 |
|
if ($extension) { |
|
76
|
1 |
|
$extension = \sprintf('.%s', $extension); |
|
77
|
|
|
} |
|
78
|
|
|
// homepage special case (need "index") |
|
79
|
1 |
|
if (empty($path) && empty($filename)) { |
|
80
|
|
|
$path = 'index'; |
|
81
|
|
|
} |
|
82
|
|
|
// do not prefix path with language code for the default language |
|
83
|
|
|
if ($language === null || ($language == $this->config->getLanguageDefault() && !$this->config->isEnabled('language.prefix'))) { |
|
84
|
|
|
$language = ''; |
|
85
|
|
|
} |
|
86
|
|
|
// do not prefix "not multilingual" virtual pages |
|
87
|
|
|
if ($this->page->getVariable('multilingual') === false) { |
|
88
|
1 |
|
$language = ''; |
|
89
|
|
|
} |
|
90
|
1 |
|
|
|
91
|
|
|
return \Cecil\Util::joinPath($language, $path, $subpath, $filename) . $extension; |
|
92
|
|
|
} |
|
93
|
1 |
|
|
|
94
|
1 |
|
/** |
|
95
|
|
|
* Returns the public path of the page. |
|
96
|
|
|
* |
|
97
|
1 |
|
* @param string $format Output format (ie: 'html', 'amp', 'json', etc.), 'html' by default |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getPath(string $format = 'html'): string |
|
100
|
|
|
{ |
|
101
|
|
|
$output = $this->getOutputFilePath($format); |
|
102
|
|
|
|
|
103
|
|
|
// if `uglyurl` is true do not remove "index.html" from the path |
|
104
|
|
|
if ($this->page->getVariable('uglyurl')) { |
|
105
|
|
|
return $output; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return str_replace('index.html', '', $output); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|