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