1 | <?php |
||
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) |
||
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) |
||
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) |
||
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) |
||
137 | |||
138 | /** |
||
139 | * Make an array of breadcrumb items. |
||
140 | * |
||
141 | * @param string $post |
||
142 | * @return array |
||
143 | */ |
||
144 | private function makeBreadcrumbsFromPost($post) |
||
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 = '-') |
||
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) |
||
187 | } |
||
188 |