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