1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Cecil. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Cecil\Renderer\Extension; |
15
|
|
|
|
16
|
|
|
use Cecil\Assets\Asset; |
17
|
|
|
use Cecil\Assets\Cache; |
18
|
|
|
use Cecil\Assets\Image; |
19
|
|
|
use Cecil\Assets\Url; |
20
|
|
|
use Cecil\Builder; |
21
|
|
|
use Cecil\Collection\CollectionInterface; |
22
|
|
|
use Cecil\Collection\Page\Collection as PagesCollection; |
23
|
|
|
use Cecil\Collection\Page\Page; |
24
|
|
|
use Cecil\Collection\Page\Type; |
25
|
|
|
use Cecil\Config; |
26
|
|
|
use Cecil\Converter\Parsedown; |
27
|
|
|
use Cecil\Exception\RuntimeException; |
28
|
|
|
use Cocur\Slugify\Bridge\Twig\SlugifyExtension; |
29
|
|
|
use Cocur\Slugify\Slugify; |
30
|
|
|
use MatthiasMullie\Minify; |
31
|
|
|
use ScssPhp\ScssPhp\Compiler; |
32
|
|
|
use SVG\SVG; |
33
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
34
|
|
|
use Symfony\Component\Yaml\Yaml; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Class Renderer\Extension\Core. |
38
|
|
|
*/ |
39
|
|
|
class Core extends SlugifyExtension |
40
|
|
|
{ |
41
|
|
|
/** @var Builder */ |
42
|
|
|
protected $builder; |
43
|
|
|
|
44
|
|
|
/** @var Config */ |
45
|
|
|
protected $config; |
46
|
|
|
|
47
|
|
|
/** @var Slugify */ |
48
|
|
|
private static $slugifier; |
49
|
|
|
|
50
|
1 |
|
public function __construct(Builder $builder) |
51
|
|
|
{ |
52
|
1 |
|
if (!self::$slugifier instanceof Slugify) { |
53
|
1 |
|
self::$slugifier = Slugify::create(['regexp' => Page::SLUGIFY_PATTERN]); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
parent::__construct(self::$slugifier); |
57
|
|
|
|
58
|
1 |
|
$this->builder = $builder; |
59
|
1 |
|
$this->config = $this->builder->getConfig(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getName() |
66
|
|
|
{ |
67
|
|
|
return 'CoreExtension'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
1 |
|
public function getFunctions() |
74
|
|
|
{ |
75
|
1 |
|
return [ |
76
|
1 |
|
new \Twig\TwigFunction('url', [$this, 'url']), |
77
|
|
|
// assets |
78
|
1 |
|
new \Twig\TwigFunction('asset', [$this, 'asset']), |
79
|
1 |
|
new \Twig\TwigFunction('integrity', [$this, 'integrity']), |
80
|
|
|
// content |
81
|
1 |
|
new \Twig\TwigFunction('readtime', [$this, 'readtime']), |
82
|
|
|
// others |
83
|
1 |
|
new \Twig\TwigFunction('getenv', [$this, 'getEnv']), |
84
|
|
|
// deprecated |
85
|
1 |
|
new \Twig\TwigFunction( |
86
|
1 |
|
'hash', |
87
|
1 |
|
[$this, 'integrity'], |
88
|
1 |
|
['deprecated' => true, 'alternative' => 'integrity'] |
89
|
1 |
|
), |
90
|
1 |
|
new \Twig\TwigFunction( |
91
|
1 |
|
'minify', |
92
|
1 |
|
[$this, 'minify'], |
93
|
1 |
|
['deprecated' => true, 'alternative' => 'minify filter'] |
94
|
1 |
|
), |
95
|
1 |
|
new \Twig\TwigFunction( |
96
|
1 |
|
'toCSS', |
97
|
1 |
|
[$this, 'toCss'], |
98
|
1 |
|
['deprecated' => true, 'alternative' => 'to_css filter'] |
99
|
1 |
|
), |
100
|
1 |
|
]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
1 |
|
public function getFilters() |
107
|
|
|
{ |
108
|
1 |
|
return [ |
109
|
1 |
|
new \Twig\TwigFilter('url', [$this, 'url']), |
110
|
|
|
// collections |
111
|
1 |
|
new \Twig\TwigFilter('sort_by_title', [$this, 'sortByTitle']), |
112
|
1 |
|
new \Twig\TwigFilter('sort_by_weight', [$this, 'sortByWeight']), |
113
|
1 |
|
new \Twig\TwigFilter('sort_by_date', [$this, 'sortByDate']), |
114
|
1 |
|
new \Twig\TwigFilter('filter_by', [$this, 'filterBy']), |
115
|
|
|
// assets |
116
|
1 |
|
new \Twig\TwigFilter('html', [$this, 'html']), |
117
|
1 |
|
new \Twig\TwigFilter('inline', [$this, 'inline']), |
118
|
1 |
|
new \Twig\TwigFilter('fingerprint', [$this, 'fingerprint']), |
119
|
1 |
|
new \Twig\TwigFilter('to_css', [$this, 'toCss']), |
120
|
1 |
|
new \Twig\TwigFilter('minify', [$this, 'minify']), |
121
|
1 |
|
new \Twig\TwigFilter('minify_css', [$this, 'minifyCss']), |
122
|
1 |
|
new \Twig\TwigFilter('minify_js', [$this, 'minifyJs']), |
123
|
1 |
|
new \Twig\TwigFilter('scss_to_css', [$this, 'scssToCss']), |
124
|
1 |
|
new \Twig\TwigFilter('sass_to_css', [$this, 'scssToCss']), |
125
|
1 |
|
new \Twig\TwigFilter('resize', [$this, 'resize']), |
126
|
1 |
|
new \Twig\TwigFilter('dataurl', [$this, 'dataurl']), |
127
|
1 |
|
new \Twig\TwigFilter('dominant_color', [$this, 'dominantColor']), |
128
|
|
|
// content |
129
|
1 |
|
new \Twig\TwigFilter('slugify', [$this, 'slugifyFilter']), |
130
|
1 |
|
new \Twig\TwigFilter('excerpt', [$this, 'excerpt']), |
131
|
1 |
|
new \Twig\TwigFilter('excerpt_html', [$this, 'excerptHtml']), |
132
|
1 |
|
new \Twig\TwigFilter('markdown_to_html', [$this, 'markdownToHtml']), |
133
|
1 |
|
new \Twig\TwigFilter('toc', [$this, 'markdownToToc']), |
134
|
1 |
|
new \Twig\TwigFilter('json_decode', [$this, 'jsonDecode']), |
135
|
1 |
|
new \Twig\TwigFilter('yaml_parse', [$this, 'yamlParse']), |
136
|
1 |
|
new \Twig\TwigFilter('preg_split', [$this, 'pregSplit']), |
137
|
1 |
|
new \Twig\TwigFilter('preg_match_all', [$this, 'pregMatchAll']), |
138
|
1 |
|
new \Twig\TwigFilter('hex_to_rgb', [$this, 'hexToRgb']), |
139
|
1 |
|
new \Twig\TwigFilter('splitline', [$this, 'splitLine']), |
140
|
1 |
|
new \Twig\TwigFilter('svg_to_png', [$this, 'svgToPng']), |
141
|
|
|
// deprecated |
142
|
1 |
|
new \Twig\TwigFilter( |
143
|
1 |
|
'filterBySection', |
144
|
1 |
|
[$this, 'filterBySection'], |
145
|
1 |
|
['deprecated' => true, 'alternative' => 'filter_by'] |
146
|
1 |
|
), |
147
|
1 |
|
new \Twig\TwigFilter( |
148
|
1 |
|
'filterBy', |
149
|
1 |
|
[$this, 'filterBy'], |
150
|
1 |
|
['deprecated' => true, 'alternative' => 'filter_by'] |
151
|
1 |
|
), |
152
|
1 |
|
new \Twig\TwigFilter( |
153
|
1 |
|
'sortByTitle', |
154
|
1 |
|
[$this, 'sortByTitle'], |
155
|
1 |
|
['deprecated' => true, 'alternative' => 'sort_by_title'] |
156
|
1 |
|
), |
157
|
1 |
|
new \Twig\TwigFilter( |
158
|
1 |
|
'sortByWeight', |
159
|
1 |
|
[$this, 'sortByWeight'], |
160
|
1 |
|
['deprecated' => true, 'alternative' => 'sort_by_weight'] |
161
|
1 |
|
), |
162
|
1 |
|
new \Twig\TwigFilter( |
163
|
1 |
|
'sortByDate', |
164
|
1 |
|
[$this, 'sortByDate'], |
165
|
1 |
|
['deprecated' => true, 'alternative' => 'sort_by_date'] |
166
|
1 |
|
), |
167
|
1 |
|
new \Twig\TwigFilter( |
168
|
1 |
|
'minifyCSS', |
169
|
1 |
|
[$this, 'minifyCss'], |
170
|
1 |
|
['deprecated' => true, 'alternative' => 'minifyCss'] |
171
|
1 |
|
), |
172
|
1 |
|
new \Twig\TwigFilter( |
173
|
1 |
|
'minifyJS', |
174
|
1 |
|
[$this, 'minifyJs'], |
175
|
1 |
|
['deprecated' => true, 'alternative' => 'minifyJs'] |
176
|
1 |
|
), |
177
|
1 |
|
new \Twig\TwigFilter( |
178
|
1 |
|
'SCSStoCSS', |
179
|
1 |
|
[$this, 'scssToCss'], |
180
|
1 |
|
['deprecated' => true, 'alternative' => 'scss_to_css'] |
181
|
1 |
|
), |
182
|
1 |
|
new \Twig\TwigFilter( |
183
|
1 |
|
'excerptHtml', |
184
|
1 |
|
[$this, 'excerptHtml'], |
185
|
1 |
|
['deprecated' => true, 'alternative' => 'excerpt_html'] |
186
|
1 |
|
), |
187
|
1 |
|
new \Twig\TwigFilter( |
188
|
1 |
|
'urlize', |
189
|
1 |
|
[$this, 'slugifyFilter'], |
190
|
1 |
|
['deprecated' => true, 'alternative' => 'slugify'] |
191
|
1 |
|
), |
192
|
1 |
|
]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritdoc} |
197
|
|
|
*/ |
198
|
1 |
|
public function getTests() |
199
|
|
|
{ |
200
|
1 |
|
return [ |
201
|
1 |
|
new \Twig\TwigTest('asset', [$this, 'isAsset']), |
202
|
1 |
|
]; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Filters by Section. |
207
|
|
|
*/ |
208
|
|
|
public function filterBySection(PagesCollection $pages, string $section): CollectionInterface |
209
|
|
|
{ |
210
|
|
|
return $this->filterBy($pages, 'section', $section); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Filters a pages collection by variable's name/value. |
215
|
|
|
*/ |
216
|
1 |
|
public function filterBy(PagesCollection $pages, string $variable, string $value): CollectionInterface |
217
|
|
|
{ |
218
|
1 |
|
$filteredPages = $pages->filter(function (Page $page) use ($variable, $value) { |
219
|
|
|
// is a dedicated getter exists? |
220
|
1 |
|
$method = 'get' . ucfirst($variable); |
221
|
1 |
|
if (method_exists($page, $method) && $page->$method() == $value) { |
222
|
|
|
return $page->getType() == Type::PAGE() && !$page->isVirtual() && true; |
223
|
|
|
} |
224
|
|
|
// or a classic variable |
225
|
1 |
|
if ($page->getVariable($variable) == $value) { |
226
|
1 |
|
return $page->getType() == Type::PAGE() && !$page->isVirtual() && true; |
227
|
|
|
} |
228
|
1 |
|
}); |
229
|
|
|
|
230
|
1 |
|
return $filteredPages; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Sorts a collection by title. |
235
|
|
|
*/ |
236
|
1 |
|
public function sortByTitle(\Traversable $collection): array |
237
|
|
|
{ |
238
|
1 |
|
$sort = \SORT_ASC; |
239
|
|
|
|
240
|
1 |
|
$collection = iterator_to_array($collection); |
241
|
1 |
|
array_multisort(array_keys(/** @scrutinizer ignore-type */ $collection), $sort, \SORT_NATURAL | \SORT_FLAG_CASE, $collection); |
|
|
|
|
242
|
|
|
|
243
|
1 |
|
return $collection; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Sorts a collection by weight. |
248
|
|
|
*/ |
249
|
1 |
|
public function sortByWeight(\Traversable $collection): array |
250
|
|
|
{ |
251
|
1 |
|
$callback = function ($a, $b) { |
252
|
1 |
|
if (!isset($a['weight'])) { |
253
|
1 |
|
$a['weight'] = 0; |
254
|
|
|
} |
255
|
1 |
|
if (!isset($b['weight'])) { |
256
|
|
|
$a['weight'] = 0; |
257
|
|
|
} |
258
|
1 |
|
if ($a['weight'] == $b['weight']) { |
259
|
1 |
|
return 0; |
260
|
|
|
} |
261
|
|
|
|
262
|
1 |
|
return $a['weight'] < $b['weight'] ? -1 : 1; |
263
|
1 |
|
}; |
264
|
|
|
|
265
|
1 |
|
$collection = iterator_to_array($collection); |
266
|
1 |
|
usort(/** @scrutinizer ignore-type */ $collection, $callback); |
267
|
|
|
|
268
|
1 |
|
return $collection; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Sorts by creation date (or 'updated' date): the most recent first. |
273
|
|
|
*/ |
274
|
1 |
|
public function sortByDate(\Traversable $collection, string $variable = 'date', bool $descTitle = false): array |
275
|
|
|
{ |
276
|
1 |
|
$callback = function ($a, $b) use ($variable, $descTitle) { |
277
|
1 |
|
if ($a[$variable] == $b[$variable]) { |
278
|
|
|
// if dates are equal and "descTitle" is true |
279
|
1 |
|
if ($descTitle && (isset($a['title']) && isset($b['title']))) { |
280
|
|
|
return strnatcmp($b['title'], $a['title']); |
281
|
|
|
} |
282
|
|
|
|
283
|
1 |
|
return 0; |
284
|
|
|
} |
285
|
|
|
|
286
|
1 |
|
return $a[$variable] > $b[$variable] ? -1 : 1; |
287
|
1 |
|
}; |
288
|
|
|
|
289
|
1 |
|
$collection = iterator_to_array($collection); |
290
|
1 |
|
usort(/** @scrutinizer ignore-type */ $collection, $callback); |
291
|
|
|
|
292
|
1 |
|
return $collection; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Creates an URL. |
297
|
|
|
* |
298
|
|
|
* $options[ |
299
|
|
|
* 'canonical' => false, |
300
|
|
|
* 'format' => 'html', |
301
|
|
|
* 'language' => null, |
302
|
|
|
* ]; |
303
|
|
|
* |
304
|
|
|
* @param Page|Asset|string|null $value |
305
|
|
|
* @param array|null $options |
306
|
|
|
*/ |
307
|
1 |
|
public function url($value = null, array $options = null): string |
308
|
|
|
{ |
309
|
1 |
|
return (new Url($this->builder, $value, $options))->getUrl(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Creates an Asset (CSS, JS, images, etc.) from a path or an array of paths. |
314
|
|
|
* |
315
|
|
|
* @param string|array $path File path or array of files path (relative from `assets/` or `static/` dir). |
316
|
|
|
* @param array|null $options |
317
|
|
|
* |
318
|
|
|
* @return Asset |
319
|
|
|
*/ |
320
|
1 |
|
public function asset($path, array $options = null): Asset |
321
|
|
|
{ |
322
|
1 |
|
return new Asset($this->builder, $path, $options); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Compiles a SCSS asset. |
327
|
|
|
* |
328
|
|
|
* @param string|Asset $asset |
329
|
|
|
* |
330
|
|
|
* @return Asset |
331
|
|
|
*/ |
332
|
1 |
|
public function toCss($asset): Asset |
333
|
|
|
{ |
334
|
1 |
|
if (!$asset instanceof Asset) { |
335
|
|
|
$asset = new Asset($this->builder, $asset); |
336
|
|
|
} |
337
|
|
|
|
338
|
1 |
|
return $asset->compile(); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Minifying an asset (CSS or JS). |
343
|
|
|
* |
344
|
|
|
* @param string|Asset $asset |
345
|
|
|
* |
346
|
|
|
* @return Asset |
347
|
|
|
*/ |
348
|
1 |
|
public function minify($asset): Asset |
349
|
|
|
{ |
350
|
1 |
|
if (!$asset instanceof Asset) { |
351
|
|
|
$asset = new Asset($this->builder, $asset); |
352
|
|
|
} |
353
|
|
|
|
354
|
1 |
|
return $asset->minify(); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Fingerprinting an asset. |
359
|
|
|
* |
360
|
|
|
* @param string|Asset $asset |
361
|
|
|
* |
362
|
|
|
* @return Asset |
363
|
|
|
*/ |
364
|
1 |
|
public function fingerprint($asset): Asset |
365
|
|
|
{ |
366
|
1 |
|
if (!$asset instanceof Asset) { |
367
|
|
|
$asset = new Asset($this->builder, $asset); |
368
|
|
|
} |
369
|
|
|
|
370
|
1 |
|
return $asset->fingerprint(); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Resizes an image. |
375
|
|
|
* |
376
|
|
|
* @param string|Asset $asset |
377
|
|
|
* |
378
|
|
|
* @return Asset |
379
|
|
|
*/ |
380
|
|
|
public function resize($asset, int $size): Asset |
381
|
|
|
{ |
382
|
|
|
if (!$asset instanceof Asset) { |
383
|
|
|
$asset = new Asset($this->builder, $asset); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
return $asset->resize($size); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Returns the data URL of an image. |
391
|
|
|
* |
392
|
|
|
* @param string|Asset $asset |
393
|
|
|
* |
394
|
|
|
* @return string |
395
|
|
|
*/ |
396
|
1 |
|
public function dataurl($asset): string |
397
|
|
|
{ |
398
|
1 |
|
if (!$asset instanceof Asset) { |
399
|
|
|
$asset = new Asset($this->builder, $asset); |
400
|
|
|
} |
401
|
|
|
|
402
|
1 |
|
return $asset->dataurl(); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Hashing an asset with algo (sha384 by default). |
407
|
|
|
* |
408
|
|
|
* @param string|Asset $asset |
409
|
|
|
* @param string $algo |
410
|
|
|
* |
411
|
|
|
* @return string |
412
|
|
|
*/ |
413
|
1 |
|
public function integrity($asset, string $algo = 'sha384'): string |
414
|
|
|
{ |
415
|
1 |
|
if (!$asset instanceof Asset) { |
416
|
1 |
|
$asset = new Asset($this->builder, $asset); |
417
|
|
|
} |
418
|
|
|
|
419
|
1 |
|
return $asset->getIntegrity($algo); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Minifying a CSS string. |
424
|
|
|
*/ |
425
|
1 |
|
public function minifyCss(?string $value): string |
426
|
|
|
{ |
427
|
1 |
|
$value = $value ?? ''; |
428
|
|
|
|
429
|
1 |
|
if ($this->builder->isDebug()) { |
430
|
1 |
|
return $value; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
$cache = new Cache($this->builder); |
434
|
|
|
$cacheKey = $cache->createKeyFromString($value); |
435
|
|
|
if (!$cache->has($cacheKey)) { |
436
|
|
|
$minifier = new Minify\CSS($value); |
437
|
|
|
$value = $minifier->minify(); |
438
|
|
|
$cache->set($cacheKey, $value); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
return $cache->get($cacheKey, $value); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Minifying a JavaScript string. |
446
|
|
|
*/ |
447
|
1 |
|
public function minifyJs(?string $value): string |
448
|
|
|
{ |
449
|
1 |
|
$value = $value ?? ''; |
450
|
|
|
|
451
|
1 |
|
if ($this->builder->isDebug()) { |
452
|
1 |
|
return $value; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
$cache = new Cache($this->builder); |
456
|
|
|
$cacheKey = $cache->createKeyFromString($value); |
457
|
|
|
if (!$cache->has($cacheKey)) { |
458
|
|
|
$minifier = new Minify\JS($value); |
459
|
|
|
$value = $minifier->minify(); |
460
|
|
|
$cache->set($cacheKey, $value); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
return $cache->get($cacheKey, $value); |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Compiles a SCSS string. |
468
|
|
|
* |
469
|
|
|
* @throws RuntimeException |
470
|
|
|
*/ |
471
|
|
|
public function scssToCss(?string $value): string |
472
|
|
|
{ |
473
|
|
|
$value = $value ?? ''; |
474
|
|
|
|
475
|
|
|
$cache = new Cache($this->builder); |
476
|
|
|
$cacheKey = $cache->createKeyFromString($value); |
477
|
|
|
if (!$cache->has($cacheKey)) { |
478
|
|
|
$scssPhp = new Compiler(); |
479
|
|
|
$outputStyles = ['expanded', 'compressed']; |
480
|
|
|
$outputStyle = strtolower((string) $this->config->get('assets.compile.style')); |
481
|
|
|
if (!in_array($outputStyle, $outputStyles)) { |
482
|
|
|
throw new RuntimeException(\sprintf('Scss output style "%s" doesn\'t exists.', $outputStyle)); |
483
|
|
|
} |
484
|
|
|
$scssPhp->setOutputStyle($outputStyle); |
485
|
|
|
$variables = $this->config->get('assets.compile.variables') ?? []; |
486
|
|
|
if (!empty($variables)) { |
487
|
|
|
$variables = array_map('ScssPhp\ScssPhp\ValueConverter::parseValue', $variables); |
488
|
|
|
$scssPhp->replaceVariables($variables); |
489
|
|
|
} |
490
|
|
|
$value = $scssPhp->compileString($value)->getCss(); |
491
|
|
|
$cache->set($cacheKey, $value); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
return $cache->get($cacheKey, $value); |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
/** |
498
|
|
|
* Creates the HTML element of an asset. |
499
|
|
|
* |
500
|
|
|
* $options[ |
501
|
|
|
* 'preload' => false, |
502
|
|
|
* 'responsive' => false, |
503
|
|
|
* 'webp' => false, |
504
|
|
|
* ]; |
505
|
|
|
* |
506
|
|
|
* @throws RuntimeException |
507
|
|
|
*/ |
508
|
1 |
|
public function html(Asset $asset, array $attributes = [], array $options = []): string |
509
|
|
|
{ |
510
|
1 |
|
$htmlAttributes = ''; |
511
|
1 |
|
$preload = false; |
512
|
1 |
|
$responsive = $this->config->get('assets.images.responsive.enabled') ?? false; |
513
|
1 |
|
$webp = (bool) $this->config->get('assets.images.webp.enabled') ?? false; |
514
|
1 |
|
extract($options, EXTR_IF_EXISTS); |
515
|
|
|
|
516
|
|
|
// builds HTML attributes |
517
|
1 |
|
foreach ($attributes as $name => $value) { |
518
|
1 |
|
$attribute = \sprintf(' %s="%s"', $name, $value); |
519
|
1 |
|
if (empty($value)) { |
520
|
1 |
|
$attribute = \sprintf(' %s', $name); |
521
|
|
|
} |
522
|
1 |
|
$htmlAttributes .= $attribute; |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
// be sure Asset file is saved |
526
|
1 |
|
$asset->save(); |
527
|
|
|
|
528
|
|
|
// CSS or JavaScript |
529
|
1 |
|
switch ($asset['ext']) { |
530
|
1 |
|
case 'css': |
531
|
1 |
|
if ($preload) { |
532
|
1 |
|
return \sprintf( |
533
|
1 |
|
'<link href="%s" rel="preload" as="style" onload="this.onload=null;this.rel=\'stylesheet\'"%s><noscript><link rel="stylesheet" href="%1$s"%2$s></noscript>', |
534
|
1 |
|
$this->url($asset, $options), |
535
|
1 |
|
$htmlAttributes |
536
|
1 |
|
); |
537
|
|
|
} |
538
|
|
|
|
539
|
1 |
|
return \sprintf('<link rel="stylesheet" href="%s"%s>', $this->url($asset, $options), $htmlAttributes); |
540
|
1 |
|
case 'js': |
541
|
|
|
return \sprintf('<script src="%s"%s></script>', $this->url($asset, $options), $htmlAttributes); |
542
|
|
|
} |
543
|
|
|
// image |
544
|
1 |
|
if ($asset['type'] == 'image') { |
545
|
|
|
// responsive |
546
|
1 |
|
$sizes = ''; |
547
|
|
|
if ( |
548
|
1 |
|
$responsive && $srcset = Image::buildSrcset( |
549
|
1 |
|
$asset, |
550
|
1 |
|
$this->config->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
551
|
1 |
|
) |
552
|
|
|
) { |
553
|
|
|
$htmlAttributes .= \sprintf(' srcset="%s"', $srcset); |
554
|
|
|
$sizes = $this->config->get('assets.images.responsive.sizes.default') ?? '100vw'; |
555
|
|
|
if (isset($attributes['class'])) { |
556
|
|
|
$sizes = Image::getSizes($attributes['class'], (array) $this->builder->getConfig()->get('assets.images.responsive.sizes')); |
557
|
|
|
} |
558
|
|
|
$htmlAttributes .= \sprintf(' sizes="%s"', $sizes); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
// <img> element |
562
|
1 |
|
$img = \sprintf( |
563
|
1 |
|
'<img src="%s" width="' . ($asset->getWidth() ?: '') . '" height="' . ($asset->getHeight() ?: '') . '"%s>', |
564
|
1 |
|
$this->url($asset, $options), |
565
|
1 |
|
$htmlAttributes |
566
|
1 |
|
); |
567
|
|
|
|
568
|
|
|
// WebP conversion? |
569
|
1 |
|
if ($webp && $asset['subtype'] != 'image/webp' && !Image::isAnimatedGif($asset)) { |
570
|
|
|
try { |
571
|
1 |
|
$assetWebp = Image::convertTopWebp($asset, $this->config->get('assets.images.quality') ?? 75); |
572
|
|
|
// <source> element |
573
|
|
|
$source = \sprintf('<source type="image/webp" srcset="%s">', $assetWebp); |
574
|
|
|
// responsive |
575
|
|
|
if ($responsive) { |
576
|
|
|
$srcset = Image::buildSrcset( |
577
|
|
|
$assetWebp, |
578
|
|
|
$this->config->get('assets.images.responsive.widths') ?? [480, 640, 768, 1024, 1366, 1600, 1920] |
579
|
|
|
) ?: (string) $assetWebp; |
580
|
|
|
// <source> element |
581
|
|
|
$source = \sprintf( |
582
|
|
|
'<source type="image/webp" srcset="%s" sizes="%s">', |
583
|
|
|
$srcset, |
584
|
|
|
$sizes |
585
|
|
|
); |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
return \sprintf("<picture>\n %s\n %s\n</picture>", $source, $img); |
589
|
1 |
|
} catch (\Exception $e) { |
590
|
1 |
|
$this->builder->getLogger()->debug($e->getMessage()); |
591
|
|
|
} |
592
|
|
|
} |
593
|
|
|
|
594
|
1 |
|
return $img; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
throw new RuntimeException(\sprintf('%s is available for CSS, JavaScript and images files only.', '"html" filter')); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
/** |
601
|
|
|
* Returns the content of an asset. |
602
|
|
|
*/ |
603
|
1 |
|
public function inline(Asset $asset): string |
604
|
|
|
{ |
605
|
1 |
|
return $asset['content']; |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* Reads $length first characters of a string and adds a suffix. |
610
|
|
|
*/ |
611
|
|
|
public function excerpt(?string $string, int $length = 450, string $suffix = ' …'): string |
612
|
|
|
{ |
613
|
|
|
$string = $string ?? ''; |
614
|
|
|
|
615
|
|
|
$string = str_replace('</p>', '<br /><br />', $string); |
616
|
|
|
$string = trim(strip_tags($string, '<br>'), '<br />'); |
617
|
|
|
if (mb_strlen($string) > $length) { |
618
|
|
|
$string = mb_substr($string, 0, $length); |
619
|
|
|
$string .= $suffix; |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
return $string; |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
/** |
626
|
|
|
* Reads characters before or after '<!-- separator -->'. |
627
|
|
|
* Options: |
628
|
|
|
* - separator: string to use as separator (`excerpt|break` by default) |
629
|
|
|
* - capture: part to capture, `before` or `after` the separator (`before` by default). |
630
|
|
|
*/ |
631
|
1 |
|
public function excerptHtml(?string $string, array $options = []): string |
632
|
|
|
{ |
633
|
1 |
|
$string = $string ?? ''; |
634
|
|
|
|
635
|
1 |
|
$separator = (string) $this->builder->getConfig()->get('body.excerpt.separator'); |
636
|
1 |
|
$capture = (string) $this->builder->getConfig()->get('body.excerpt.capture'); |
637
|
1 |
|
extract($options, EXTR_IF_EXISTS); |
638
|
|
|
|
639
|
|
|
// https://regex101.com/r/n9TWHF/1 |
640
|
1 |
|
$pattern = '(.*)<!--[[:blank:]]?(' . $separator . ')[[:blank:]]?-->(.*)'; |
641
|
1 |
|
preg_match('/' . $pattern . '/is', $string, $matches); |
642
|
|
|
|
643
|
1 |
|
if (empty($matches)) { |
644
|
1 |
|
return $string; |
645
|
|
|
} |
646
|
1 |
|
$result = trim($matches[1]); |
647
|
1 |
|
if ($capture == 'after') { |
648
|
1 |
|
$result = trim($matches[3]); |
649
|
|
|
} |
650
|
|
|
// removes footnotes and returns result |
651
|
1 |
|
return preg_replace('/<sup[^>]*>[^u]*<\/sup>/', '', $result); |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
/** |
655
|
|
|
* Converts a Markdown string to HTML. |
656
|
|
|
* |
657
|
|
|
* @throws RuntimeException |
658
|
|
|
*/ |
659
|
1 |
|
public function markdownToHtml(?string $markdown): ?string |
660
|
|
|
{ |
661
|
1 |
|
$markdown = $markdown ?? ''; |
662
|
|
|
|
663
|
|
|
try { |
664
|
1 |
|
$parsedown = new Parsedown($this->builder); |
665
|
1 |
|
$html = $parsedown->text($markdown); |
666
|
|
|
} catch (\Exception $e) { |
667
|
|
|
throw new RuntimeException('"markdown_to_html" filter can not convert supplied Markdown.'); |
668
|
|
|
} |
669
|
|
|
|
670
|
1 |
|
return $html; |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
/** |
674
|
|
|
* Extract table of content of a Markdown string, |
675
|
|
|
* in the given format ("html" or "json", "html" by default). |
676
|
|
|
* |
677
|
|
|
* @throws RuntimeException |
678
|
|
|
*/ |
679
|
|
|
public function markdownToToc(?string $markdown, $format = 'html', $url = ''): ?string |
680
|
|
|
{ |
681
|
|
|
$markdown = $markdown ?? ''; |
682
|
|
|
|
683
|
|
|
try { |
684
|
|
|
$parsedown = new Parsedown($this->builder, ['selectors' => ['h2'], 'url' => $url]); |
685
|
|
|
$parsedown->body($markdown); |
686
|
|
|
$return = $parsedown->contentsList($format); |
687
|
|
|
} catch (\Exception $e) { |
688
|
|
|
throw new RuntimeException('"toc" filter can not convert supplied Markdown.'); |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
return $return; |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* Converts a JSON string to an array. |
696
|
|
|
* |
697
|
|
|
* @throws RuntimeException |
698
|
|
|
*/ |
699
|
1 |
|
public function jsonDecode(?string $json): ?array |
700
|
|
|
{ |
701
|
1 |
|
$json = $json ?? ''; |
702
|
|
|
|
703
|
|
|
try { |
704
|
1 |
|
$array = json_decode($json, true); |
705
|
1 |
|
if ($array === null && json_last_error() !== JSON_ERROR_NONE) { |
706
|
1 |
|
throw new \Exception('JSON error.'); |
707
|
|
|
} |
708
|
|
|
} catch (\Exception $e) { |
709
|
|
|
throw new RuntimeException('"json_decode" filter can not parse supplied JSON.'); |
710
|
|
|
} |
711
|
|
|
|
712
|
1 |
|
return $array; |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
/** |
716
|
|
|
* Converts a YAML string to an array. |
717
|
|
|
* |
718
|
|
|
* @throws RuntimeException |
719
|
|
|
*/ |
720
|
1 |
|
public function yamlParse(?string $yaml): ?array |
721
|
|
|
{ |
722
|
1 |
|
$yaml = $yaml ?? ''; |
723
|
|
|
|
724
|
|
|
try { |
725
|
1 |
|
$array = Yaml::parse($yaml); |
726
|
1 |
|
if (!is_array($array)) { |
727
|
1 |
|
throw new ParseException('YAML error.'); |
728
|
|
|
} |
729
|
|
|
} catch (ParseException $e) { |
730
|
|
|
throw new RuntimeException(\sprintf('"yaml_parse" filter can not parse supplied YAML: %s', $e->getMessage())); |
731
|
|
|
} |
732
|
|
|
|
733
|
1 |
|
return $array; |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
/** |
737
|
|
|
* Split a string into an array using a regular expression. |
738
|
|
|
* |
739
|
|
|
* @throws RuntimeException |
740
|
|
|
*/ |
741
|
|
|
public function pregSplit(?string $value, string $pattern, int $limit = 0): ?array |
742
|
|
|
{ |
743
|
|
|
$value = $value ?? ''; |
744
|
|
|
|
745
|
|
|
try { |
746
|
|
|
$array = preg_split($pattern, $value, $limit); |
747
|
|
|
if ($array === false) { |
748
|
|
|
throw new RuntimeException('PREG split error.'); |
749
|
|
|
} |
750
|
|
|
} catch (\Exception $e) { |
751
|
|
|
throw new RuntimeException('"preg_split" filter can not split supplied string.'); |
752
|
|
|
} |
753
|
|
|
|
754
|
|
|
return $array; |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
/** |
758
|
|
|
* Perform a regular expression match and return the group for all matches. |
759
|
|
|
* |
760
|
|
|
* @throws RuntimeException |
761
|
|
|
*/ |
762
|
|
|
public function pregMatchAll(?string $value, string $pattern, int $group = 0): ?array |
763
|
|
|
{ |
764
|
|
|
$value = $value ?? ''; |
765
|
|
|
|
766
|
|
|
try { |
767
|
|
|
$array = preg_match_all($pattern, $value, $matches, PREG_PATTERN_ORDER); |
768
|
|
|
if ($array === false) { |
769
|
|
|
throw new RuntimeException('PREG match all error.'); |
770
|
|
|
} |
771
|
|
|
} catch (\Exception $e) { |
772
|
|
|
throw new RuntimeException('"preg_match_all" filter can not match in supplied string.'); |
773
|
|
|
} |
774
|
|
|
|
775
|
|
|
return $matches[$group]; |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
/** |
779
|
|
|
* Calculates estimated time to read a text. |
780
|
|
|
*/ |
781
|
|
|
public function readtime(?string $text): string |
782
|
|
|
{ |
783
|
|
|
$text = $text ?? ''; |
784
|
|
|
|
785
|
|
|
$words = str_word_count(strip_tags($text)); |
786
|
|
|
$min = floor($words / 200); |
787
|
|
|
if ($min === 0) { |
788
|
|
|
return '1'; |
789
|
|
|
} |
790
|
|
|
|
791
|
|
|
return (string) $min; |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
/** |
795
|
|
|
* Gets the value of an environment variable. |
796
|
|
|
*/ |
797
|
1 |
|
public function getEnv(?string $var): ?string |
798
|
|
|
{ |
799
|
1 |
|
$var = $var ?? ''; |
800
|
|
|
|
801
|
1 |
|
return getenv($var) ?: null; |
802
|
|
|
} |
803
|
|
|
|
804
|
|
|
/** |
805
|
|
|
* Tests if a variable is an Asset. |
806
|
|
|
*/ |
807
|
|
|
public function isAsset($variable): bool |
808
|
|
|
{ |
809
|
|
|
return $variable instanceof Asset; |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
/** |
813
|
|
|
* Returns the dominant hex color of an image asset. |
814
|
|
|
* |
815
|
|
|
* @param string|Asset $asset |
816
|
|
|
* |
817
|
|
|
* @return string |
818
|
|
|
*/ |
819
|
1 |
|
public function dominantColor($asset): string |
820
|
|
|
{ |
821
|
1 |
|
if (!$asset instanceof Asset) { |
822
|
|
|
$asset = new Asset($this->builder, $asset); |
823
|
|
|
} |
824
|
|
|
|
825
|
1 |
|
return Image::getDominantColor($asset); |
826
|
|
|
} |
827
|
|
|
|
828
|
|
|
/** |
829
|
|
|
* Converts an hexadecimal color to RGB. |
830
|
|
|
* |
831
|
|
|
* @throws RuntimeException |
832
|
|
|
*/ |
833
|
|
|
public function hexToRgb(?string $variable): array |
834
|
|
|
{ |
835
|
|
|
$variable = $variable ?? ''; |
836
|
|
|
|
837
|
|
|
if (!self::isHex($variable)) { |
838
|
|
|
throw new RuntimeException(\sprintf('"%s" is not a valid hexadecimal value.', $variable)); |
839
|
|
|
} |
840
|
|
|
$hex = ltrim($variable, '#'); |
841
|
|
|
if (strlen($hex) == 3) { |
842
|
|
|
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
843
|
|
|
} |
844
|
|
|
$c = hexdec($hex); |
845
|
|
|
|
846
|
|
|
return [ |
847
|
|
|
'red' => $c >> 16 & 0xFF, |
848
|
|
|
'green' => $c >> 8 & 0xFF, |
849
|
|
|
'blue' => $c & 0xFF, |
850
|
|
|
]; |
851
|
|
|
} |
852
|
|
|
|
853
|
|
|
/** |
854
|
|
|
* Split a string in multiple lines. |
855
|
|
|
*/ |
856
|
|
|
public function splitLine(?string $variable, int $max = 18): array |
857
|
|
|
{ |
858
|
|
|
$variable = $variable ?? ''; |
859
|
|
|
|
860
|
|
|
return preg_split("/.{0,{$max}}\K(\s+|$)/", $variable, 0, PREG_SPLIT_NO_EMPTY); |
861
|
|
|
} |
862
|
|
|
|
863
|
|
|
/** |
864
|
|
|
* Converts a SVG to a PNG. |
865
|
|
|
*/ |
866
|
|
|
public function svgToPng(?string $variable, $width = 1200, $height = 628): string |
867
|
|
|
{ |
868
|
|
|
$variable = $variable ?? ''; |
869
|
|
|
|
870
|
|
|
$image = SVG::fromString($variable); |
871
|
|
|
ob_start(); |
872
|
|
|
header('Content-Type: image/png'); |
873
|
|
|
imagepng($image->toRasterImage($width, $height)); |
874
|
|
|
$imageData = ob_get_contents(); |
875
|
|
|
ob_end_clean(); |
876
|
|
|
|
877
|
|
|
return 'data:image/png;base64,' . base64_encode($imageData); |
878
|
|
|
} |
879
|
|
|
|
880
|
|
|
/** |
881
|
|
|
* Is a hexadecimal color is valid? |
882
|
|
|
*/ |
883
|
|
|
private static function isHex(string $hex): bool |
884
|
|
|
{ |
885
|
|
|
$valid = is_string($hex); |
886
|
|
|
$hex = ltrim($hex, '#'); |
887
|
|
|
$length = strlen($hex); |
888
|
|
|
$valid = $valid && ($length === 3 || $length === 6); |
889
|
|
|
$valid = $valid && ctype_xdigit($hex); |
890
|
|
|
|
891
|
|
|
return $valid; |
892
|
|
|
} |
893
|
|
|
} |
894
|
|
|
|