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
|
|
|
/** |
34
|
|
|
* Configuration object. |
35
|
|
|
* @var \Cecil\Config |
36
|
|
|
*/ |
37
|
|
|
protected $config; |
38
|
|
|
/** |
39
|
|
|
* Page item. |
40
|
|
|
* @var PageItem |
41
|
|
|
*/ |
42
|
|
|
protected $page; |
43
|
|
|
|
44
|
1 |
|
public function __construct(Builder $builder, PageItem $page) |
45
|
|
|
{ |
46
|
1 |
|
$this->builder = $builder; |
47
|
1 |
|
$this->config = $this->builder->getConfig(); |
48
|
1 |
|
$this->page = $page; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns the path of the rendered page, based on the output format properties. |
53
|
|
|
* Use cases: |
54
|
|
|
* - default: path + filename + extension (e.g.: 'blog/post-1/index.html') |
55
|
|
|
* - with subpath: path + subpath + filename + extension (e.g.: 'blog/post-1/amp/index.html') |
56
|
|
|
* - ugly URL: path + extension (e.g.: '404.html', 'sitemap.xml', 'robots.txt') |
57
|
|
|
* - path only (e.g.: '_redirects') |
58
|
|
|
* - i18n: language code + default (e.g.: 'fr/blog/page/index.html') |
59
|
|
|
* |
60
|
|
|
* @param string $format Output format (ie: 'html', 'amp', 'json', etc.) |
61
|
|
|
*/ |
62
|
1 |
|
public function getOutputFilePath(string $format): string |
63
|
|
|
{ |
64
|
1 |
|
$path = $this->page->getPath(); |
65
|
1 |
|
$subpath = (string) $this->config->getOutputFormatProperty($format, 'subpath'); |
66
|
1 |
|
$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
|
1 |
|
$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
|
1 |
|
$path = 'index'; |
81
|
|
|
} |
82
|
|
|
// do not prefix path with language code for the default language |
83
|
1 |
|
if ($language === null || ($language == $this->config->getLanguageDefault() && !$this->config->isEnabled('language.prefix'))) { |
84
|
1 |
|
$language = ''; |
85
|
|
|
} |
86
|
|
|
// do not prefix "not multilingual" virtual pages |
87
|
1 |
|
if ($this->page->getVariable('multilingual') === false) { |
88
|
1 |
|
$language = ''; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return \Cecil\Util::joinPath($language, $path, $subpath, $filename) . $extension; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the public path of the page. |
96
|
|
|
* |
97
|
|
|
* @param string $format Output format (ie: 'html', 'amp', 'json', etc.), 'html' by default |
98
|
|
|
*/ |
99
|
1 |
|
public function getPath(string $format = 'html'): string |
100
|
|
|
{ |
101
|
1 |
|
$output = $this->getOutputFilePath($format); |
102
|
|
|
|
103
|
|
|
// if `uglyurl` is true do not remove "index.html" from the path |
104
|
1 |
|
if ($this->page->getVariable('uglyurl')) { |
105
|
1 |
|
return $output; |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
return str_replace('index.html', '', $output); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|