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