forked from OleVik/grav-plugin-fullpage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.php
343 lines (327 loc) · 11.3 KB
/
Utilities.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
namespace Fullpage;
use Grav\Common\Grav;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Page\Collection;
/**
* Fullpage-plugin Utilities
*/
class Utilities
{
/**
* Plugin configuration
*
* @var array
*/
protected $config;
/**
* Instantiate Fullpage Utilities
*/
/**
* Instantiate Fullpage Utilities
*
* @param array $config Plugin configuration
*/
public function __construct($config)
{
$this->config = $config;
}
/**
* Creates page-structure recursively
*
* @param string $route Route to page
* @param string $mode Reserved collection-mode for handling child-pages
* @param integer $depth Reserved placeholder for recursion depth
*
* @return array Page-structure with children
*/
public function buildTree($route, $mode = false, $depth = 0)
{
$page = Grav::instance()['page'];
$depth++;
$mode = '@page.self';
$config = $this->config;
if (isset($page->header()->fullpage)) {
$config = array_merge($config, $page->header()->fullpage);
}
if ($depth > 1) {
$mode = '@page.children';
}
$pages = $page->evaluate([$mode => $route]);
$pages = $pages->published()->order(
$config['order']['by'],
$config['order']['dir']
);
$paths = array();
foreach ($pages as $page) {
$route = $page->rawRoute();
$paths[$route]['depth'] = $depth;
$paths[$route]['title'] = $page->title();
$paths[$route]['menu'] = array(
'anchor' => $page->slug(),
'title' => $page->title()
);
$paths[$route]['route'] = $route;
$paths[$route]['slug'] = $page->slug();
$paths[$route]['header'] = $page->header();
if (isset($config['inject_footer'])) {
$paths[$route]['inject_footer'] = $config['inject_footer'];
}
if (isset($page->header()->inject_footer)) {
$paths[$route]['inject_footer'] = $page->header()->inject_footer;
}
if (!empty($paths[$route]['inject_footer'])) {
$paths[$route]['inject_footer'] = Grav::instance()['twig']->processTemplate($paths[$route]['inject_footer'], ['page' => $page]);
}
if (isset($page->header()->horizontal)) {
$paths[$route]['horizontal'] = $page->header()->horizontal;
}
if (isset($page->header()->styles)) {
$paths[$route]['styles'] = $page->header()->styles;
} elseif (isset($config['styles'])) {
$paths[$route]['styles'] = $config['styles'];
}
$paths[$route]['content'] = $page->content();
if (!empty($paths[$route])) {
$children = $this->buildTree($route, $mode, $depth);
if (!empty($children)) {
$paths[$route]['children'] = $children;
}
}
}
if (!empty($paths)) {
return $paths;
} else {
return null;
}
}
/**
* Create HTML to use with fullPage.js
*
* @param array $pages Page-structure with children
*
* @return string HTML-structure
*/
public function buildContent($pages)
{
$parsedown = new \Parsedown();
$return = '';
foreach ($pages as $route => $page) {
ob_start();
$title = $page['title'];
$slug = $page['slug'];
$content = $page['content'];
$content = $parsedown->text($content);
$index = 0;
$styleIndex = 0;
$styles = array();
$config = $this->config;
if (isset($page['header']->fullpage)) {
$config = array_merge($config, $page['header']->fullpage);
}
$breaks = explode("<hr />", $content);
if (isset($page['horizontal'])) {
$type = 'slide';
echo "<div
data-name='$title'
data-anchor='$slug'
class='section'
>";
} else {
$type = 'section';
}
foreach ($breaks as $break) {
if ($index == 0) {
$id = $page['slug'];
} else {
$id = $index;
}
if ($config['shortcodes']) {
$shortcodes = $this->interpretShortcodes($break);
$break = $shortcodes['content'];
$shortcodeStyles = $shortcodes['styles'];
}
if (!empty($shortcodeStyles)) {
$shortcodeStyles = $this->applyStyles($page['styles'][$styleIndex]);
echo "<div
data-name='$title'
data-anchor='$id'
class='$type'
style='$shortcodeStyles'
>";
} else {
if (isset($page['styles']) && isset($page['styles'][$styleIndex])) {
$styles = $this->applyStyles($page['styles'][$styleIndex]);
echo "<div
data-name='$title'
data-anchor='$id'
class='$type'
style='$styles'
>";
} else {
echo "<div
data-name='$title'
data-anchor='$id'
class='$type'
>";
}
}
echo $break;
if (isset($page['inject_footer'])) {
echo $page['inject_footer'];
}
echo "</div>";
if (isset($page['styles']) && count($page['styles']) == $styleIndex+1) {
$styleIndex = 0;
} else {
$styleIndex++;
}
$index++;
}
if (isset($page['horizontal'])) {
echo "</div>";
}
$return .= ob_get_contents();
ob_end_clean();
if (isset($page['children'])) {
$return .= $this->buildContent($page['children']);
}
}
return $return;
}
public function interpretShortcodes($content)
{
$styles = array();
$re = '~((?:\[\s*(?<name>[a-zA-Z0-9-_]+)\s*(?:\=\s*(?<bbCode>\"(?:[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"|((?:(?!=\s*|\]|\/\])[^\s])+)))?\s*(?<parameters>(?:\s*(?:\w+(?:\s*\=\s*\"(?:[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*)\"|\s*\=\s*((?:(?!=\s*|\]|\/\])[^\s])+)|(?=\s|\]|\/\s*\]|$))))*)\s*(?:\](?<content>.*?)\[\s*(?<markerContent>\/)\s*(\k<name>)\s*\]|\]|(?<marker>\/)\s*\])))~u';
preg_match_all($re, $content, $matches, PREG_SET_ORDER, 0);
if (!empty($matches)) {
foreach ($matches as $match) {
$styles[$match['name']] = $match['bbCode'];
$content = str_replace($match[0], '', $content);
}
}
return ['content' => $content, 'styles' => $styles];
}
/**
* Generate menu with anchors and titles from pages
*
* @param array $tree Page-structure with children
*
* @return array Slide-anchors with titles
*/
public function buildMenu($tree)
{
$items = array();
foreach ($tree as $key => $value) {
if (is_array($value['menu'])) {
$items[$value['menu']['anchor']] = $value['menu']['title'];
}
if (isset($value['children'])) {
$items[] = $this->buildMenu($value['children']);
}
}
return $items;
}
/**
* Format styles for inlining
*
* @param array $styles Array of quote-enclosed properties and values
*
* @return string CSS-styles
*/
public function applyStyles($styles)
{
if (empty($styles)) {
return null;
}
$return = '';
foreach ($styles as $key => $value) {
/* If background is defined, and color is not, try to find a suitable contrast */
if (array_key_exists('background', $styles) && !array_key_exists('color', $styles)) {
if (isset($config['color_function'])) {
if ($config['color_function'] == '50') {
$color = $this->getContrast50($styles['background']);
} elseif ($config['color_function'] == 'YIQ') {
$color = $this->getContrastYIQ($styles['background']);
}
} else {
$color = $this->getContrast50($styles['background']);
}
$return .= 'color: ' . $color . ';';
}
$return .= $key . ': ' . $value . ';';
}
return $return;
}
/**
* Find contrasting color from 50%-equation
*
* @param string $hexcolor Hexadecimal color-value
*
* @return string black|white
*
* @see https://24ways.org/2010/calculating-color-contrast
*/
public function getContrast50($hexcolor)
{
return (hexdec($hexcolor) > 0xffffff/2) ? 'black':'white';
}
/**
* Find contrasting color from YIQ-equation
*
* @param string $hexcolor Hexadecimal color-value
*
* @return string black|white
*
* @see https://24ways.org/2010/calculating-color-contrast
*/
public function getContrastYIQ($hexcolor)
{
$r = hexdec(substr($hexcolor, 0, 2));
$g = hexdec(substr($hexcolor, 2, 4));
$b = hexdec(substr($hexcolor, 4, 6));
$yiq = (($r*299)+($g*587)+($b*114))/1000;
return ($yiq >= 128) ? 'black' : 'white';
}
/**
* Flatten a multidimensional array to one dimension, optionally preserving keys
*
* @param array $array Array to flatten
* @param integer $preserveKeys 0 (default) to not preserve keys, 1 to preserve string keys only, 2 to preserve all keys
* @param array $out Internal parameter for recursion
*
* @return array Flattened array
*
* @see https://stackoverflow.com/a/7256477/603387
*/
public function flattenArray($array, $preserveKeys = 0, &$out = array())
{
foreach ($array as $key => $child) {
if (is_array($child)) {
$out = $this->flattenArray($child, $preserveKeys, $out);
} elseif ($preserveKeys + is_string($key) > 1) {
$out[$key] = $child;
} else {
$out[] = $child;
}
}
return $out;
}
/**
* Insert string within string
*
* @param string $str Original string
* @param string $insert String to insert
* @param int $index Position to insert to
*
* @return string Original string with new string inserted
*
* @see https://stackoverflow.com/a/30820401/603387
*/
public function stringInsert($str, $insert, $index)
{
$str = substr($str, 0, $index) . $insert . substr($str, $index);
return $str;
}
}