|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pagemark; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
6
|
|
|
use Pagemark\Contracts\Parseable; |
|
7
|
|
|
|
|
8
|
|
|
class Pagemark |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var Filesystem |
|
12
|
|
|
*/ |
|
13
|
|
|
private $filesystem; |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Parseable |
|
16
|
|
|
*/ |
|
17
|
|
|
private $parser; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* TODO: Not liking the filesystem dependency, so look at other options |
|
21
|
|
|
* |
|
22
|
|
|
* @param Filesystem $filesystem |
|
23
|
|
|
* @param Parseable $parser |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Filesystem $filesystem, Parseable $parser) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->filesystem = $filesystem; |
|
28
|
|
|
$this->parser = $parser; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Return a new pagemark instance. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Parseable $parser |
|
35
|
|
|
* @return static |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function create(Parseable $parser = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$parser = $parser ?: new Parser(new \Parsedown()); |
|
40
|
|
|
|
|
41
|
|
|
return new static(new Filesystem(), $parser); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Parse a post into an array of content elements |
|
46
|
|
|
* without needing to instantiate the class first. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $basePath |
|
49
|
|
|
* @param string $post |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function parse($basePath, $post) |
|
53
|
|
|
{ |
|
54
|
|
|
$pagemark = static::create(); |
|
55
|
|
|
|
|
56
|
|
|
return $pagemark->getContent($basePath, $post); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Parse a post into an array of content elements. |
|
61
|
|
|
* |
|
62
|
|
|
* TODO: Refactor |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $basePath |
|
65
|
|
|
* @param string $post |
|
66
|
|
|
* @return array |
|
67
|
|
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getContent($basePath, $post) |
|
70
|
|
|
{ |
|
71
|
|
|
$breadcrumbs = $this->makeBreadcrumbsFromPost($post); |
|
72
|
|
|
$title = $this->makeTitleFromBreadcrumbs($breadcrumbs); |
|
73
|
|
|
|
|
74
|
|
|
$postPath = $basePath .($post? '/'. $post : ''); |
|
75
|
|
|
$isDir = false; |
|
76
|
|
|
$index = []; |
|
77
|
|
|
|
|
78
|
|
|
if ($this->filesystem->isDirectory($postPath)) { |
|
79
|
|
|
$isDir = true; |
|
80
|
|
|
$index['subcategories'] = $this->filesystem->directories($postPath); |
|
81
|
|
|
$index['files'] = $this->filesystem->files($postPath); |
|
82
|
|
|
$postPath .= '/index'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$file = $postPath.'.md'; |
|
86
|
|
|
if ($this->filesystem->exists($file)) { |
|
87
|
|
|
$post = $this->parser->parse($this->filesystem->get($file)); |
|
88
|
|
|
} else { |
|
89
|
|
|
$post = ''; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ($isDir) { |
|
93
|
|
|
foreach ($index['subcategories'] as $i => $item) { |
|
94
|
|
|
$item = str_replace($basePath, '', $item); |
|
95
|
|
|
$paths = explode('/', $item); |
|
96
|
|
|
$name = array_pop($paths); |
|
97
|
|
|
$index['subcategories'][$i] = [ |
|
98
|
|
|
'href' => $item, |
|
99
|
|
|
'name' => $this->deslugify($name), |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
foreach ($index['files'] as $i => $item) { |
|
103
|
|
|
$item = str_replace($basePath, '', $item); |
|
104
|
|
|
$item = str_replace('.md', '', $item); |
|
105
|
|
|
$paths = explode('/', $item); |
|
106
|
|
|
$name = array_pop($paths); |
|
107
|
|
|
$index['files'][$i] = [ |
|
108
|
|
|
'href' => $item, |
|
109
|
|
|
'name' => $this->deslugify($name), |
|
110
|
|
|
]; |
|
111
|
|
|
if ($index['files'][$i]['name'] === 'index') { |
|
112
|
|
|
unset($index['files'][$i]); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return [ |
|
118
|
|
|
'title' => $title, |
|
119
|
|
|
'breadcrumbs' => $breadcrumbs, |
|
120
|
|
|
'index' => $index, |
|
121
|
|
|
'post' => $post, |
|
122
|
|
|
]; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get the Post or Category title from the breadcrumbs array. |
|
127
|
|
|
* |
|
128
|
|
|
* @param $breadcrumbs |
|
129
|
|
|
* @return mixed |
|
130
|
|
|
*/ |
|
131
|
|
|
private function makeTitleFromBreadcrumbs($breadcrumbs) |
|
132
|
|
|
{ |
|
133
|
|
|
$lastCrumb = current(array_slice($breadcrumbs, -1)); |
|
134
|
|
|
|
|
135
|
|
|
return $lastCrumb['name']; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Make an array of breadcrumb items. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $post |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
|
|
private function makeBreadcrumbsFromPost($post) |
|
145
|
|
|
{ |
|
146
|
|
|
$crumbs = explode('/', $post); |
|
147
|
|
|
$crumbs = empty($crumbs[0]) ? [] : $crumbs; |
|
148
|
|
|
|
|
149
|
|
|
$breadcrumbs = []; |
|
150
|
|
|
|
|
151
|
|
|
foreach ($crumbs as $i => $crumb) { |
|
152
|
|
|
$breadcrumbs[$i] = [ |
|
153
|
|
|
'href' => '/'.$this->slugify(implode('/', array_slice($crumbs, 0, $i+1))), |
|
154
|
|
|
'name' => $this->deslugify($crumbs[$i]), |
|
155
|
|
|
]; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $breadcrumbs; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Take a string with spaces and convert the spaces to the specified separator. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $string |
|
165
|
|
|
* @param string $separator |
|
166
|
|
|
* @return mixed |
|
167
|
|
|
*/ |
|
168
|
|
|
private function slugify($string, $separator = '-') |
|
169
|
|
|
{ |
|
170
|
|
|
return str_replace(' ', $separator, $string); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Take a string with dash or underscore word separators and convert the to spaces. |
|
175
|
|
|
* |
|
176
|
|
|
* @param array|string $slugged |
|
177
|
|
|
* @return array|string |
|
178
|
|
|
*/ |
|
179
|
|
|
private function deslugify($slugged) |
|
180
|
|
|
{ |
|
181
|
|
|
if (is_array($slugged)) { |
|
182
|
|
|
return array_map('static::deslugify', $slugged); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return str_replace(['-', '_'], ' ', $slugged); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|