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