1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Document; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
11
|
|
|
use allejo\stakx\RuntimeStatus; |
12
|
|
|
use allejo\stakx\Service; |
13
|
|
|
|
14
|
|
|
trait PermalinkDocumentTrait |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
protected $permalink = null; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
protected $redirects = null; |
21
|
|
|
|
22
|
|
|
/** Extensions that need to be stripped from permalinks. */ |
23
|
|
|
private static $extensionsToStrip = ['twig']; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
11 |
|
public function handleSpecialRedirects() |
29
|
|
|
{ |
30
|
11 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
18 |
|
final public function getTargetFile($permalink = null) |
36
|
|
|
{ |
37
|
18 |
|
if ($permalink === null) |
38
|
|
|
{ |
39
|
16 |
|
$permalink = $this->getPermalink(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
18 |
|
$missingFile = (substr($permalink, -1) == '/'); |
43
|
18 |
|
$permalink = str_replace('/', DIRECTORY_SEPARATOR, $permalink); |
|
|
|
|
44
|
|
|
|
45
|
18 |
|
if ($missingFile) |
46
|
|
|
{ |
47
|
10 |
|
$permalink = rtrim($permalink, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'index.html'; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
18 |
|
return ltrim($permalink, DIRECTORY_SEPARATOR); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
35 |
|
final public function getPermalink() |
57
|
|
|
{ |
58
|
35 |
|
$this->buildPermalink(); |
|
|
|
|
59
|
|
|
|
60
|
35 |
|
$this->permalink = $this->sanitizePermalink($this->permalink); |
|
|
|
|
61
|
35 |
|
$this->permalink = str_replace(DIRECTORY_SEPARATOR, '/', $this->permalink); |
62
|
35 |
|
$this->permalink = '/' . ltrim($this->permalink, '/'); // Permalinks should always use '/' and not be OS specific |
63
|
|
|
|
64
|
35 |
|
return $this->permalink; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
12 |
|
final public function getRedirects() |
|
|
|
|
71
|
|
|
{ |
72
|
12 |
|
if ($this->redirects === null) |
73
|
|
|
{ |
74
|
1 |
|
$this->getPermalink(); |
75
|
|
|
} |
76
|
|
|
|
77
|
12 |
|
$this->handleSpecialRedirects(); |
78
|
|
|
|
79
|
12 |
|
return $this->redirects; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the permalink based off the location of where the file is relative to the website. This permalink is to be |
84
|
|
|
* used as a fallback in the case that a permalink is not explicitly specified in the Front Matter. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
2 |
|
final protected function getPathPermalink() |
89
|
|
|
{ |
90
|
|
|
// Remove the protocol of the path, if there is one and prepend a '/' to the beginning |
91
|
2 |
|
$cleanPath = preg_replace('/[\w|\d]+:\/\//', '', $this->getRelativeFilePath()); |
|
|
|
|
92
|
2 |
|
$cleanPath = ltrim($cleanPath, DIRECTORY_SEPARATOR); |
93
|
|
|
|
94
|
|
|
// Handle vfs:// paths by replacing their forward slashes with the OS appropriate directory separator |
95
|
2 |
|
if (DIRECTORY_SEPARATOR !== '/') |
96
|
|
|
{ |
97
|
|
|
$cleanPath = str_replace('/', DIRECTORY_SEPARATOR, $cleanPath); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Check the first folder and see if it's a data folder (starts with an underscore) intended for stakx |
101
|
2 |
|
$folders = explode(DIRECTORY_SEPARATOR, $cleanPath); |
102
|
|
|
|
103
|
2 |
|
if (substr($folders[0], 0, 1) === '_') |
104
|
|
|
{ |
105
|
1 |
|
array_shift($folders); |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
$cleanPath = implode(DIRECTORY_SEPARATOR, $folders); |
109
|
|
|
|
110
|
2 |
|
return $cleanPath; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Sanitize a permalink to remove unsupported characters or multiple '/' and replace spaces with hyphens. |
115
|
|
|
* |
116
|
|
|
* @param string $permalink A permalink |
117
|
|
|
* |
118
|
|
|
* @return string $permalink The sanitized permalink |
|
|
|
|
119
|
|
|
*/ |
120
|
35 |
|
final private function sanitizePermalink($permalink) |
121
|
|
|
{ |
122
|
|
|
// Remove multiple '/' together |
123
|
35 |
|
$permalink = preg_replace('/\/+/', '/', $permalink); |
|
|
|
|
124
|
|
|
|
125
|
|
|
// Replace all spaces with hyphens |
126
|
35 |
|
$permalink = str_replace(' ', '-', $permalink); |
|
|
|
|
127
|
|
|
|
128
|
|
|
// Remove all disallowed characters |
129
|
35 |
|
$permalink = preg_replace('/[^0-9a-zA-Z-_\/\\\.]/', '', $permalink); |
|
|
|
|
130
|
|
|
|
131
|
35 |
|
if (in_array(fs::getExtension($permalink), self::$extensionsToStrip)) |
|
|
|
|
132
|
|
|
{ |
133
|
3 |
|
$permalink = fs::removeExtension($permalink); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// Remove any special characters before a sane value |
137
|
35 |
|
$permalink = preg_replace('/^[^0-9a-zA-Z-_]*/', '', $permalink); |
|
|
|
|
138
|
|
|
|
139
|
|
|
// Convert permalinks to lower case |
140
|
35 |
|
if (!Service::hasRunTimeFlag(RuntimeStatus::COMPILER_PRESERVE_CASE)) |
141
|
|
|
{ |
142
|
35 |
|
$permalink = mb_strtolower($permalink, 'UTF-8'); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
35 |
|
return $permalink; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|