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