Total Complexity | 114 |
Total Lines | 777 |
Duplicated Lines | 0 % |
Changes | 10 | ||
Bugs | 5 | Features | 1 |
Complex classes like Parsedown often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Parsedown, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class Parsedown extends \ParsedownToc |
||
37 | { |
||
38 | /** @var Builder */ |
||
39 | protected $builder; |
||
40 | |||
41 | /** @var \Cecil\Config */ |
||
42 | protected $config; |
||
43 | |||
44 | /** |
||
45 | * Regex for attributes. |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $regexAttribute = '(?:[#.][-\w:\\\]+[ ]*|[-\w:\\\]+(?:=(?:["\'][^\n]*?["\']|[^\s]+)?)?[ ]*)'; |
||
49 | |||
50 | /** |
||
51 | * Regex for image block. |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $regexImage = "~^!\[.*?\]\(.*?\)~"; |
||
55 | |||
56 | /** @var Highlighter */ |
||
57 | protected $highlighter; |
||
58 | |||
59 | public function __construct(Builder $builder, ?array $options = null) |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Insert inline. |
||
87 | * e.g.: ++text++ -> <ins>text</ins>. |
||
88 | */ |
||
89 | protected function inlineInsert($Excerpt) |
||
102 | ], |
||
103 | ]; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | protected function inlineLink($Excerpt) |
||
111 | { |
||
112 | $link = parent::inlineLink($Excerpt); // @phpstan-ignore staticMethod.notFound |
||
113 | |||
114 | if (!isset($link)) { |
||
115 | return null; |
||
116 | } |
||
117 | |||
118 | // Link to a page with "page:page_id" as URL |
||
119 | if (Util\Str::startsWith($link['element']['attributes']['href'], 'page:')) { |
||
120 | $link['element']['attributes']['href'] = new Url($this->builder, substr($link['element']['attributes']['href'], 5, \strlen($link['element']['attributes']['href']))); |
||
121 | |||
122 | return $link; |
||
123 | } |
||
124 | |||
125 | // External link |
||
126 | $link = $this->handleExternalLink($link); |
||
127 | |||
128 | /* |
||
129 | * Embed link? |
||
130 | */ |
||
131 | $embed = $this->config->isEnabled('pages.body.links.embed'); |
||
132 | if (isset($link['element']['attributes']['embed'])) { |
||
133 | $embed = true; |
||
134 | if ($link['element']['attributes']['embed'] == 'false') { |
||
135 | $embed = false; |
||
136 | } |
||
137 | unset($link['element']['attributes']['embed']); |
||
138 | } |
||
139 | $extension = pathinfo($link['element']['attributes']['href'], PATHINFO_EXTENSION); |
||
140 | // video? |
||
141 | if (\in_array($extension, $this->config->get('pages.body.links.embed.video') ?? [])) { |
||
142 | if (!$embed) { |
||
143 | $link['element']['attributes']['href'] = new Url($this->builder, $link['element']['attributes']['href']); |
||
144 | |||
145 | return $link; |
||
146 | } |
||
147 | $video = $this->createMediaFromLink($link, 'video'); |
||
148 | if ($this->config->isEnabled('pages.body.images.caption')) { |
||
149 | return $this->createFigure($video); |
||
150 | } |
||
151 | |||
152 | return $video; |
||
153 | } |
||
154 | // audio? |
||
155 | if (\in_array($extension, $this->config->get('pages.body.links.embed.audio') ?? [])) { |
||
156 | if (!$embed) { |
||
157 | $link['element']['attributes']['href'] = new Url($this->builder, $link['element']['attributes']['href']); |
||
158 | |||
159 | return $link; |
||
160 | } |
||
161 | $audio = $this->createMediaFromLink($link, 'audio'); |
||
162 | if ($this->config->isEnabled('pages.body.images.caption')) { |
||
163 | return $this->createFigure($audio); |
||
164 | } |
||
165 | |||
166 | return $audio; |
||
167 | } |
||
168 | if (!$embed) { |
||
169 | return $link; |
||
170 | } |
||
171 | // Youtube link? |
||
172 | // https://regex101.com/r/gznM1j/1 |
||
173 | $pattern = '(?:https?:\/\/)?(?:www\.)?youtu(?:\.be\/|be.com\/\S*(?:watch|embed)(?:(?:(?=\/[-a-zA-Z0-9_]{11,}(?!\S))\/)|(?:\S*v=|v\/)))([-a-zA-Z0-9_]{11,})'; |
||
174 | if (preg_match('/' . $pattern . '/is', (string) $link['element']['attributes']['href'], $matches)) { |
||
175 | return $this->createEmbeddedVideoFromLink($link, 'https://www.youtube.com/embed/', $matches[1]); |
||
176 | } |
||
177 | // Vimeo link? |
||
178 | // https://regex101.com/r/wCEFhd/1 |
||
179 | $pattern = 'https:\/\/vimeo\.com\/([0-9]+)'; |
||
180 | if (preg_match('/' . $pattern . '/is', (string) $link['element']['attributes']['href'], $matches)) { |
||
181 | return $this->createEmbeddedVideoFromLink($link, 'https://player.vimeo.com/video/', $matches[1]); |
||
182 | } |
||
183 | // GitHub Gist link? |
||
184 | // https://regex101.com/r/KWVMYI/1 |
||
185 | $pattern = 'https:\/\/gist\.github\.com\/[-a-zA-Z0-9_]+\/[-a-zA-Z0-9_]+'; |
||
186 | if (preg_match('/' . $pattern . '/is', (string) $link['element']['attributes']['href'], $matches)) { |
||
187 | $gist = [ |
||
188 | 'extent' => $link['extent'], |
||
189 | 'element' => [ |
||
190 | 'name' => 'script', |
||
191 | 'text' => $link['element']['text'], |
||
192 | 'attributes' => [ |
||
193 | 'src' => $matches[0] . '.js', |
||
194 | 'title' => $link['element']['attributes']['title'], |
||
195 | ], |
||
196 | ], |
||
197 | ]; |
||
198 | if ($this->config->isEnabled('pages.body.images.caption')) { |
||
199 | return $this->createFigure($gist); |
||
200 | } |
||
201 | |||
202 | return $gist; |
||
203 | } |
||
204 | |||
205 | return $link; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | protected function inlineUrl($Excerpt) |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | protected function inlineUrlTag($Excerpt) |
||
227 | { |
||
228 | $link = parent::inlineUrlTag($Excerpt); // @phpstan-ignore staticMethod.notFound |
||
229 | |||
230 | if (!isset($link)) { |
||
231 | return; |
||
232 | } |
||
233 | |||
234 | // External link |
||
235 | return $this->handleExternalLink($link); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | protected function inlineImage($Excerpt) |
||
242 | { |
||
243 | $InlineImage = parent::inlineImage($Excerpt); // @phpstan-ignore staticMethod.notFound |
||
244 | if (!isset($InlineImage)) { |
||
245 | return null; |
||
246 | } |
||
247 | |||
248 | // normalize path |
||
249 | $InlineImage['element']['attributes']['src'] = $this->normalizePath($InlineImage['element']['attributes']['src']); |
||
250 | |||
251 | // should be lazy loaded? |
||
252 | if ($this->config->isEnabled('pages.body.images.lazy') && !isset($InlineImage['element']['attributes']['loading'])) { |
||
253 | $InlineImage['element']['attributes']['loading'] = 'lazy'; |
||
254 | } |
||
255 | // should be decoding async? |
||
256 | if ($this->config->isEnabled('pages.body.images.decoding') && !isset($InlineImage['element']['attributes']['decoding'])) { |
||
257 | $InlineImage['element']['attributes']['decoding'] = 'async'; |
||
258 | } |
||
259 | // add default class? |
||
260 | if ((string) $this->config->get('pages.body.images.class')) { |
||
261 | if (!\array_key_exists('class', $InlineImage['element']['attributes'])) { |
||
262 | $InlineImage['element']['attributes']['class'] = ''; |
||
263 | } |
||
264 | $InlineImage['element']['attributes']['class'] .= ' ' . (string) $this->config->get('pages.body.images.class'); |
||
265 | $InlineImage['element']['attributes']['class'] = trim($InlineImage['element']['attributes']['class']); |
||
266 | } |
||
267 | |||
268 | // disable remote image handling? |
||
269 | if (Util\File::isRemote($InlineImage['element']['attributes']['src']) && !$this->config->isEnabled('pages.body.images.remote')) { |
||
270 | return $InlineImage; |
||
271 | } |
||
272 | |||
273 | // create asset |
||
274 | $assetOptions = ['leading_slash' => false]; |
||
275 | if ($this->config->isEnabled('pages.body.images.remote.fallback')) { |
||
276 | $assetOptions = ['leading_slash' => true]; |
||
277 | $assetOptions += ['fallback' => (string) $this->config->get('pages.body.images.remote.fallback')]; |
||
278 | } |
||
279 | $asset = new Asset($this->builder, $InlineImage['element']['attributes']['src'], $assetOptions); |
||
280 | $InlineImage['element']['attributes']['src'] = new Url($this->builder, $asset); |
||
281 | $width = $asset['width']; |
||
282 | |||
283 | /* |
||
284 | * Should be resized? |
||
285 | */ |
||
286 | $shouldResize = false; |
||
287 | $assetResized = null; |
||
288 | // pages.body.images.resize |
||
289 | if ( |
||
290 | \is_int($this->config->get('pages.body.images.resize')) |
||
291 | && $this->config->get('pages.body.images.resize') > 0 |
||
292 | && $width > $this->config->get('pages.body.images.resize') |
||
293 | ) { |
||
294 | $shouldResize = true; |
||
295 | $width = $this->config->get('pages.body.images.resize'); |
||
296 | } |
||
297 | // width attribute |
||
298 | if ( |
||
299 | isset($InlineImage['element']['attributes']['width']) |
||
300 | && $width > (int) $InlineImage['element']['attributes']['width'] |
||
301 | ) { |
||
302 | $shouldResize = true; |
||
303 | $width = (int) $InlineImage['element']['attributes']['width']; |
||
304 | } |
||
305 | // responsive images |
||
306 | if ( |
||
307 | $this->config->isEnabled('pages.body.images.responsive') |
||
308 | && !empty($this->config->getAssetsImagesWidths()) |
||
309 | && $width > max($this->config->getAssetsImagesWidths()) |
||
310 | ) { |
||
311 | $shouldResize = true; |
||
312 | $width = max($this->config->getAssetsImagesWidths()); |
||
313 | } |
||
314 | if ($shouldResize) { |
||
315 | try { |
||
316 | $assetResized = $asset->resize($width); |
||
317 | } catch (\Exception $e) { |
||
318 | $this->builder->getLogger()->debug($e->getMessage()); |
||
319 | |||
320 | return $InlineImage; |
||
321 | } |
||
322 | } |
||
323 | |||
324 | // set width |
||
325 | $InlineImage['element']['attributes']['width'] = $width; |
||
326 | // set height |
||
327 | $InlineImage['element']['attributes']['height'] = $assetResized['height'] ?? $asset['height']; |
||
328 | |||
329 | // placeholder |
||
330 | if ( |
||
331 | (!empty($this->config->get('pages.body.images.placeholder')) || isset($InlineImage['element']['attributes']['placeholder'])) |
||
332 | && \in_array($assetResized['subtype'] ?? $asset['subtype'], ['image/jpeg', 'image/png', 'image/gif']) |
||
333 | ) { |
||
334 | if (!\array_key_exists('placeholder', $InlineImage['element']['attributes'])) { |
||
335 | $InlineImage['element']['attributes']['placeholder'] = (string) $this->config->get('pages.body.images.placeholder'); |
||
336 | } |
||
337 | if (!\array_key_exists('style', $InlineImage['element']['attributes'])) { |
||
338 | $InlineImage['element']['attributes']['style'] = ''; |
||
339 | } |
||
340 | $InlineImage['element']['attributes']['style'] = trim($InlineImage['element']['attributes']['style'], ';'); |
||
341 | switch ($InlineImage['element']['attributes']['placeholder']) { |
||
342 | case 'color': |
||
343 | $InlineImage['element']['attributes']['style'] .= \sprintf(';max-width:100%%;height:auto;background-color:%s;', Image::getDominantColor($assetResized ?? $asset)); |
||
344 | break; |
||
345 | case 'lqip': |
||
346 | // aborts if animated GIF for performance reasons |
||
347 | if (Image::isAnimatedGif($assetResized ?? $asset)) { |
||
348 | break; |
||
349 | } |
||
350 | $InlineImage['element']['attributes']['style'] .= \sprintf(';max-width:100%%;height:auto;background-image:url(%s);background-repeat:no-repeat;background-position:center;background-size:cover;', Image::getLqip($asset)); |
||
351 | break; |
||
352 | } |
||
353 | unset($InlineImage['element']['attributes']['placeholder']); |
||
354 | $InlineImage['element']['attributes']['style'] = trim($InlineImage['element']['attributes']['style']); |
||
355 | } |
||
356 | |||
357 | /* |
||
358 | * Should be responsive? |
||
359 | */ |
||
360 | $sizes = ''; |
||
361 | if ($this->config->isEnabled('pages.body.images.responsive')) { |
||
362 | try { |
||
363 | if ( |
||
364 | $srcset = Image::buildSrcset( |
||
365 | $assetResized ?? $asset, |
||
366 | $this->config->getAssetsImagesWidths() |
||
367 | ) |
||
368 | ) { |
||
369 | $InlineImage['element']['attributes']['srcset'] = $srcset; |
||
370 | $sizes = Image::getSizes($InlineImage['element']['attributes']['class'] ?? '', (array) $this->config->getAssetsImagesSizes()); |
||
371 | $InlineImage['element']['attributes']['sizes'] = $sizes; |
||
372 | } |
||
373 | } catch (\Exception $e) { |
||
374 | $this->builder->getLogger()->debug($e->getMessage()); |
||
375 | } |
||
376 | } |
||
377 | |||
378 | /* |
||
379 | <!-- if title: a <figure> is required to put in it a <figcaption> --> |
||
380 | <figure> |
||
381 | <!-- if formats: a <picture> is required for each <source> --> |
||
382 | <picture> |
||
383 | <source type="image/avif" |
||
384 | srcset="..." |
||
385 | sizes="..." |
||
386 | > |
||
387 | <source type="image/webp" |
||
388 | srcset="..." |
||
389 | sizes="..." |
||
390 | > |
||
391 | <img src="..." |
||
392 | srcset="..." |
||
393 | sizes="..." |
||
394 | > |
||
395 | </picture> |
||
396 | <figcaption><!-- title --></figcaption> |
||
397 | </figure> |
||
398 | */ |
||
399 | |||
400 | $image = $InlineImage; |
||
401 | |||
402 | // converts image to formats and put them in picture > source |
||
403 | if ( |
||
404 | \count($formats = ((array) $this->config->get('pages.body.images.formats'))) > 0 |
||
405 | && \in_array($assetResized['subtype'] ?? $asset['subtype'], ['image/jpeg', 'image/png', 'image/gif']) |
||
406 | ) { |
||
407 | try { |
||
408 | // InlineImage src must be an Asset instance |
||
409 | if (!($assetResized ?? $asset) instanceof Asset) { |
||
|
|||
410 | throw new RuntimeException(\sprintf('Asset "%s" can\'t be converted.', $InlineImage['element']['attributes']['src'])); |
||
411 | } |
||
412 | // abord if InlineImage is an animated GIF |
||
413 | if (Image::isAnimatedGif($assetResized ?? $asset)) { |
||
414 | $filepath = Util::joinFile($this->config->getOutputPath(), $assetResized['path'] ?? $asset['path']); |
||
415 | throw new RuntimeException(\sprintf('Asset "%s" is not converted (animated GIF).', $filepath)); |
||
416 | } |
||
417 | $sources = []; |
||
418 | foreach ($formats as $format) { |
||
419 | $srcset = ''; |
||
420 | try { |
||
421 | $assetConverted = ($assetResized ?? $asset)->convert($format); |
||
422 | } catch (\Exception $e) { |
||
423 | $this->builder->getLogger()->debug($e->getMessage()); |
||
424 | continue; |
||
425 | } |
||
426 | // build responsive images? |
||
427 | if ($this->config->isEnabled('pages.body.images.responsive')) { |
||
428 | try { |
||
429 | $srcset = Image::buildSrcset($assetConverted, $this->config->getAssetsImagesWidths()); |
||
430 | } catch (\Exception $e) { |
||
431 | $this->builder->getLogger()->debug($e->getMessage()); |
||
432 | } |
||
433 | } |
||
434 | // if not, use default image as srcset |
||
435 | if (empty($srcset)) { |
||
436 | $srcset = (string) $assetConverted; |
||
437 | } |
||
438 | // add format to <sources> |
||
439 | $sources[] = [ |
||
440 | 'name' => 'source', |
||
441 | 'attributes' => [ |
||
442 | 'type' => "image/$format", |
||
443 | 'srcset' => $srcset, |
||
444 | 'sizes' => $sizes, |
||
445 | 'width' => $InlineImage['element']['attributes']['width'], |
||
446 | 'height' => $InlineImage['element']['attributes']['height'], |
||
447 | ], |
||
448 | ]; |
||
449 | } |
||
450 | if (\count($sources) > 0) { |
||
451 | $picture = [ |
||
452 | 'extent' => $InlineImage['extent'], |
||
453 | 'element' => [ |
||
454 | 'name' => 'picture', |
||
455 | 'handler' => 'elements', |
||
456 | 'attributes' => [ |
||
457 | 'title' => $image['element']['attributes']['title'], |
||
458 | ], |
||
459 | ], |
||
460 | ]; |
||
461 | $picture['element']['text'] = $sources; |
||
462 | unset($image['element']['attributes']['title']); // @phpstan-ignore unset.offset |
||
463 | $picture['element']['text'][] = $image['element']; |
||
464 | $image = $picture; |
||
465 | } |
||
466 | } catch (\Exception $e) { |
||
467 | $this->builder->getLogger()->debug($e->getMessage()); |
||
468 | } |
||
469 | } |
||
470 | |||
471 | // if title: put the <img> (or <picture>) in a <figure> and create a <figcaption> |
||
472 | if ($this->config->isEnabled('pages.body.images.caption')) { |
||
473 | return $this->createFigure($image); |
||
474 | } |
||
475 | |||
476 | return $image; |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Image block. |
||
481 | */ |
||
482 | protected function blockImage($Excerpt) |
||
483 | { |
||
484 | if (1 !== preg_match($this->regexImage, $Excerpt['text'])) { |
||
485 | return; |
||
486 | } |
||
487 | |||
488 | $InlineImage = $this->inlineImage($Excerpt); |
||
489 | if (!isset($InlineImage)) { |
||
490 | return; |
||
491 | } |
||
492 | |||
493 | return $InlineImage; |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * Note block-level markup. |
||
498 | * |
||
499 | * :::tip |
||
500 | * **Tip:** This is an advice. |
||
501 | * ::: |
||
502 | * |
||
503 | * Code inspired by https://github.com/sixlive/parsedown-alert from TJ Miller (@sixlive). |
||
504 | */ |
||
505 | protected function blockNote($block) |
||
506 | { |
||
507 | if (preg_match('/:::(.*)/', $block['text'], $matches)) { |
||
508 | $block = [ |
||
509 | 'char' => ':', |
||
510 | 'element' => [ |
||
511 | 'name' => 'aside', |
||
512 | 'text' => '', |
||
513 | 'attributes' => [ |
||
514 | 'class' => 'note', |
||
515 | ], |
||
516 | ], |
||
517 | ]; |
||
518 | if (!empty($matches[1])) { |
||
519 | $block['element']['attributes']['class'] .= " note-{$matches[1]}"; |
||
520 | } |
||
521 | |||
522 | return $block; |
||
523 | } |
||
524 | } |
||
525 | |||
526 | protected function blockNoteContinue($line, $block) |
||
527 | { |
||
528 | if (isset($block['complete'])) { |
||
529 | return; |
||
530 | } |
||
531 | if (preg_match('/:::/', $line['text'])) { |
||
532 | $block['complete'] = true; |
||
533 | |||
534 | return $block; |
||
535 | } |
||
536 | $block['element']['text'] .= $line['text'] . "\n"; |
||
537 | |||
538 | return $block; |
||
539 | } |
||
540 | |||
541 | protected function blockNoteComplete($block) |
||
542 | { |
||
543 | $block['element']['rawHtml'] = $this->text($block['element']['text']); |
||
544 | unset($block['element']['text']); |
||
545 | |||
546 | return $block; |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * Apply Highlight to code blocks. |
||
551 | */ |
||
552 | protected function blockFencedCodeComplete($block) |
||
553 | { |
||
554 | if (!$this->config->isEnabled('pages.body.highlight')) { |
||
555 | return $block; |
||
556 | } |
||
557 | if (!isset($block['element']['text']['attributes'])) { |
||
558 | return $block; |
||
559 | } |
||
560 | |||
561 | try { |
||
562 | $code = $block['element']['text']['text']; |
||
563 | $languageClass = $block['element']['text']['attributes']['class']; |
||
564 | $language = explode('-', $languageClass); |
||
565 | $highlighted = $this->highlighter->highlight($language[1], $code); |
||
566 | $block['element']['text']['attributes']['class'] = vsprintf('%s hljs %s', [ |
||
567 | $languageClass, |
||
568 | $highlighted->language, |
||
569 | ]); |
||
570 | $block['element']['text']['rawHtml'] = $highlighted->value; |
||
571 | $block['element']['text']['allowRawHtmlInSafeMode'] = true; |
||
572 | unset($block['element']['text']['text']); |
||
573 | } catch (\Exception $e) { |
||
574 | $this->builder->getLogger()->debug($e->getMessage()); |
||
575 | } finally { |
||
576 | return $block; |
||
577 | } |
||
578 | } |
||
579 | |||
580 | /** |
||
581 | * {@inheritdoc} |
||
582 | */ |
||
583 | protected function parseAttributeData($attributeString) |
||
584 | { |
||
585 | $attributes = preg_split('/[ ]+/', $attributeString, -1, PREG_SPLIT_NO_EMPTY); |
||
586 | $Data = []; |
||
587 | $HtmlAtt = []; |
||
588 | |||
589 | if (is_iterable($attributes)) { |
||
590 | foreach ($attributes as $attribute) { |
||
591 | switch ($attribute[0]) { |
||
592 | case '#': // ID |
||
593 | $Data['id'] = substr($attribute, 1); |
||
594 | break; |
||
595 | case '.': // Classes |
||
596 | $classes[] = substr($attribute, 1); |
||
597 | break; |
||
598 | default: // Attributes |
||
599 | parse_str($attribute, $parsed); |
||
600 | $HtmlAtt = array_merge($HtmlAtt, $parsed); |
||
601 | } |
||
602 | } |
||
603 | |||
604 | if (isset($classes)) { |
||
605 | $Data['class'] = implode(' ', $classes); |
||
606 | } |
||
607 | if (!empty($HtmlAtt)) { |
||
608 | foreach ($HtmlAtt as $a => $v) { |
||
609 | $Data[$a] = trim($v, '"'); |
||
610 | } |
||
611 | } |
||
612 | } |
||
613 | |||
614 | return $Data; |
||
615 | } |
||
616 | |||
617 | /** |
||
618 | * {@inheritdoc} |
||
619 | * |
||
620 | * Converts XHTML '<br />' tag to '<br>'. |
||
621 | * |
||
622 | * @return string |
||
623 | */ |
||
624 | protected function unmarkedText($text) |
||
625 | { |
||
626 | return str_replace('<br />', '<br>', parent::unmarkedText($text)); // @phpstan-ignore staticMethod.notFound |
||
627 | } |
||
628 | |||
629 | /** |
||
630 | * {@inheritdoc} |
||
631 | * |
||
632 | * XHTML closing tag to HTML5 closing tag. |
||
633 | * |
||
634 | * @return string |
||
635 | */ |
||
636 | protected function element(array $Element) |
||
639 | } |
||
640 | |||
641 | /** |
||
642 | * Turns a path relative to static or assets into a website relative path. |
||
643 | * |
||
644 | * "../../assets/images/img.jpeg" |
||
645 | * -> |
||
646 | * "/images/img.jpeg" |
||
647 | */ |
||
648 | private function normalizePath(string $path): string |
||
649 | { |
||
650 | // https://regex101.com/r/Rzguzh/1 |
||
651 | $pattern = \sprintf( |
||
652 | '(\.\.\/)+(\b%s|%s\b)+(\/.*)', |
||
653 | (string) $this->config->get('static.dir'), |
||
654 | (string) $this->config->get('assets.dir') |
||
655 | ); |
||
656 | $path = Util::joinPath($path); |
||
657 | if (!preg_match('/' . $pattern . '/is', $path, $matches)) { |
||
658 | return $path; |
||
659 | } |
||
660 | |||
661 | return $matches[3]; |
||
662 | } |
||
663 | |||
664 | /** |
||
665 | * Create a media (video or audio) element from a link. |
||
666 | */ |
||
667 | private function createMediaFromLink(array $link, string $type = 'video'): array |
||
668 | { |
||
669 | $block = [ |
||
670 | 'extent' => $link['extent'], |
||
671 | 'element' => [ |
||
672 | 'text' => $link['element']['text'], |
||
673 | ], |
||
674 | ]; |
||
675 | $block['element']['attributes'] = $link['element']['attributes']; |
||
676 | unset($block['element']['attributes']['href']); |
||
677 | $block['element']['attributes']['src'] = new Url($this->builder, new Asset($this->builder, $link['element']['attributes']['href'])); |
||
678 | switch ($type) { |
||
679 | case 'video': |
||
680 | $block['element']['name'] = 'video'; |
||
681 | // no controls = autoplay, loop, muted, playsinline |
||
682 | if (!isset($block['element']['attributes']['controls'])) { |
||
683 | $block['element']['attributes']['autoplay'] = ''; |
||
684 | $block['element']['attributes']['loop'] = ''; |
||
685 | $block['element']['attributes']['muted'] = ''; |
||
686 | $block['element']['attributes']['playsinline'] = ''; |
||
687 | } |
||
688 | if (isset($block['element']['attributes']['poster'])) { |
||
689 | $block['element']['attributes']['poster'] = new Url($this->builder, new Asset($this->builder, $block['element']['attributes']['poster'])); |
||
690 | } |
||
691 | if (!\array_key_exists('style', $block['element']['attributes'])) { |
||
692 | $block['element']['attributes']['style'] = ''; |
||
693 | } |
||
694 | $block['element']['attributes']['style'] .= ';max-width:100%;height:auto;background-color:#d8d8d8;'; // background color if offline |
||
695 | |||
696 | return $block; |
||
697 | case 'audio': |
||
698 | $block['element']['name'] = 'audio'; |
||
699 | |||
700 | return $block; |
||
701 | } |
||
702 | |||
703 | throw new \Exception(\sprintf('Can\'t create %s from "%s".', $type, $link['element']['attributes']['href'])); |
||
704 | } |
||
705 | |||
706 | /** |
||
707 | * Create an embedded video element from a link. |
||
708 | * |
||
709 | * $baseSrc is the base URL to embed the video |
||
710 | * $match is the video ID or the rest of the URL to append to $baseSrc |
||
711 | */ |
||
712 | private function createEmbeddedVideoFromLink(array $link, string $baseSrc, string $match): array |
||
713 | { |
||
714 | $iframe = [ |
||
715 | 'element' => [ |
||
716 | 'name' => 'iframe', |
||
717 | 'text' => $link['element']['text'], |
||
718 | 'attributes' => [ |
||
719 | 'width' => '640', |
||
720 | 'height' => '360', |
||
721 | 'title' => $link['element']['text'], |
||
722 | 'src' => Util::joinPath($baseSrc, $match), |
||
723 | 'frameborder' => '0', |
||
724 | 'allow' => 'accelerometer;autoplay;encrypted-media;gyroscope;picture-in-picture;', |
||
725 | 'allowfullscreen' => '', |
||
726 | 'style' => 'position:absolute;top:0;left:0;width:100%;height:100%;border:0;background-color:#d8d8d8;', |
||
727 | ], |
||
728 | ], |
||
729 | ]; |
||
730 | $div = [ |
||
731 | 'extent' => $link['extent'], |
||
732 | 'element' => [ |
||
733 | 'name' => 'div', |
||
734 | 'handler' => 'elements', |
||
735 | 'text' => [ |
||
736 | $iframe['element'], |
||
737 | ], |
||
738 | 'attributes' => [ |
||
739 | 'style' => 'position:relative;padding-bottom:56.25%;height:0;overflow:hidden;', |
||
740 | 'title' => $link['element']['attributes']['title'], |
||
741 | ], |
||
742 | ], |
||
743 | ]; |
||
744 | if ($this->config->isEnabled('pages.body.images.caption')) { |
||
745 | return $this->createFigure($div); |
||
746 | } |
||
747 | |||
748 | return $div; |
||
749 | } |
||
750 | |||
751 | /** |
||
752 | * Create a figure / caption element. |
||
753 | */ |
||
754 | private function createFigure(array $inline): array |
||
755 | { |
||
756 | if (empty($inline['element']['attributes']['title'])) { |
||
757 | return $inline; |
||
758 | } |
||
759 | |||
760 | $titleRawHtml = $this->line($inline['element']['attributes']['title']); // @phpstan-ignore method.notFound |
||
761 | $inline['element']['attributes']['title'] = strip_tags($titleRawHtml); |
||
762 | |||
763 | $figcaption = [ |
||
764 | 'element' => [ |
||
765 | 'name' => 'figcaption', |
||
766 | 'allowRawHtmlInSafeMode' => true, |
||
767 | 'rawHtml' => $titleRawHtml, |
||
768 | ], |
||
769 | ]; |
||
770 | $figure = [ |
||
771 | 'extent' => $inline['extent'], |
||
772 | 'element' => [ |
||
773 | 'name' => 'figure', |
||
774 | 'handler' => 'elements', |
||
775 | 'text' => [ |
||
776 | $inline['element'], |
||
777 | $figcaption['element'], |
||
778 | ], |
||
779 | ], |
||
780 | ]; |
||
781 | |||
782 | return $figure; |
||
783 | } |
||
784 | |||
785 | /** |
||
786 | * Handle an external link. |
||
787 | */ |
||
788 | private function handleExternalLink(array $link): array |
||
813 | } |
||
814 | } |
||
815 |