| Total Complexity | 108 |
| Total Lines | 758 |
| Duplicated Lines | 0 % |
| Changes | 13 | ||
| Bugs | 8 | 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) |
||
| 60 | { |
||
| 61 | $this->builder = $builder; |
||
| 62 | $this->config = $builder->getConfig(); |
||
| 63 | |||
| 64 | // "insert" line block: ++text++ -> <ins>text</ins> |
||
| 65 | $this->InlineTypes['+'][] = 'Insert'; |
||
| 66 | $this->inlineMarkerList = implode('', array_keys($this->InlineTypes)); |
||
| 67 | $this->specialCharacters[] = '+'; |
||
| 68 | |||
| 69 | // Image block (to avoid paragraph) |
||
| 70 | $this->BlockTypes['!'][] = 'Image'; |
||
| 71 | |||
| 72 | // "notes" block |
||
| 73 | $this->BlockTypes[':'][] = 'Note'; |
||
| 74 | |||
| 75 | // code highlight |
||
| 76 | $this->highlighter = new Highlighter(); |
||
| 77 | |||
| 78 | // options |
||
| 79 | $options = array_merge(['selectors' => (array) $this->config->get('pages.body.toc')], $options ?? []); |
||
| 80 | |||
| 81 | parent::__construct(); |
||
| 82 | parent::setOptions($options); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Insert inline. |
||
| 87 | * e.g.: ++text++ -> <ins>text</ins>. |
||
| 88 | */ |
||
| 89 | protected function inlineInsert($Excerpt) |
||
| 90 | { |
||
| 91 | if (!isset($Excerpt['text'][1])) { |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($Excerpt['text'][1] === '+' && preg_match('/^\+\+(?=\S)(.+?)(?<=\S)\+\+/', $Excerpt['text'], $matches)) { |
||
| 96 | return [ |
||
| 97 | 'extent' => \strlen($matches[0]), |
||
| 98 | 'element' => [ |
||
| 99 | 'name' => 'ins', |
||
| 100 | 'text' => $matches[1], |
||
| 101 | 'handler' => 'line', |
||
| 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 | |||
| 149 | return $this->createFigure($video); |
||
| 150 | } |
||
| 151 | // audio? |
||
| 152 | if (\in_array($extension, $this->config->get('pages.body.links.embed.audio') ?? [])) { |
||
| 153 | if (!$embed) { |
||
| 154 | $link['element']['attributes']['href'] = new Url($this->builder, $link['element']['attributes']['href']); |
||
| 155 | |||
| 156 | return $link; |
||
| 157 | } |
||
| 158 | $audio = $this->createMediaFromLink($link, 'audio'); |
||
| 159 | |||
| 160 | return $this->createFigure($audio); |
||
| 161 | } |
||
| 162 | if (!$embed) { |
||
| 163 | return $link; |
||
| 164 | } |
||
| 165 | // Youtube link? |
||
| 166 | // https://regex101.com/r/gznM1j/1 |
||
| 167 | $pattern = '(?:https?:\/\/)?(?:www\.)?youtu(?:\.be\/|be.com\/\S*(?:watch|embed)(?:(?:(?=\/[-a-zA-Z0-9_]{11,}(?!\S))\/)|(?:\S*v=|v\/)))([-a-zA-Z0-9_]{11,})'; |
||
| 168 | if (preg_match('/' . $pattern . '/is', (string) $link['element']['attributes']['href'], $matches)) { |
||
| 169 | return $this->createFigure($this->createEmbeddedVideoFromLink($link, 'https://www.youtube-nocookie.com/embed/', $matches[1])); |
||
| 170 | } |
||
| 171 | // Vimeo link? |
||
| 172 | // https://regex101.com/r/wCEFhd/1 |
||
| 173 | $pattern = 'https:\/\/vimeo\.com\/([0-9]+)'; |
||
| 174 | if (preg_match('/' . $pattern . '/is', (string) $link['element']['attributes']['href'], $matches)) { |
||
| 175 | return $this->createFigure($this->createEmbeddedVideoFromLink($link, 'https://player.vimeo.com/video/', $matches[1])); |
||
| 176 | } |
||
| 177 | // GitHub Gist link? |
||
| 178 | // https://regex101.com/r/KWVMYI/1 |
||
| 179 | $pattern = 'https:\/\/gist\.github\.com\/[-a-zA-Z0-9_]+\/[-a-zA-Z0-9_]+'; |
||
| 180 | if (preg_match('/' . $pattern . '/is', (string) $link['element']['attributes']['href'], $matches)) { |
||
| 181 | $gist = [ |
||
| 182 | 'extent' => $link['extent'], |
||
| 183 | 'element' => [ |
||
| 184 | 'name' => 'script', |
||
| 185 | 'text' => $link['element']['text'], |
||
| 186 | 'attributes' => [ |
||
| 187 | 'src' => $matches[0] . '.js', |
||
| 188 | 'title' => $link['element']['attributes']['title'], |
||
| 189 | ], |
||
| 190 | ], |
||
| 191 | ]; |
||
| 192 | |||
| 193 | return $this->createFigure($gist); |
||
| 194 | } |
||
| 195 | |||
| 196 | return $link; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | protected function inlineUrl($Excerpt) |
||
| 203 | { |
||
| 204 | $link = parent::inlineUrl($Excerpt); // @phpstan-ignore staticMethod.notFound |
||
| 205 | |||
| 206 | if (!isset($link)) { |
||
| 207 | return; |
||
| 208 | } |
||
| 209 | |||
| 210 | // External link |
||
| 211 | return $this->handleExternalLink($link); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * {@inheritdoc} |
||
| 216 | */ |
||
| 217 | protected function inlineUrlTag($Excerpt) |
||
| 218 | { |
||
| 219 | $link = parent::inlineUrlTag($Excerpt); // @phpstan-ignore staticMethod.notFound |
||
| 220 | |||
| 221 | if (!isset($link)) { |
||
| 222 | return; |
||
| 223 | } |
||
| 224 | |||
| 225 | // External link |
||
| 226 | return $this->handleExternalLink($link); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * {@inheritdoc} |
||
| 231 | */ |
||
| 232 | protected function inlineImage($Excerpt) |
||
| 233 | { |
||
| 234 | $InlineImage = parent::inlineImage($Excerpt); // @phpstan-ignore staticMethod.notFound |
||
| 235 | if (!isset($InlineImage)) { |
||
| 236 | return null; |
||
| 237 | } |
||
| 238 | |||
| 239 | // remove link attributes |
||
| 240 | unset($InlineImage['element']['attributes']['target'], $InlineImage['element']['attributes']['rel']); |
||
| 241 | |||
| 242 | // normalize path |
||
| 243 | $InlineImage['element']['attributes']['src'] = $this->normalizePath($InlineImage['element']['attributes']['src']); |
||
| 244 | |||
| 245 | // should be lazy loaded? |
||
| 246 | if ($this->config->isEnabled('pages.body.images.lazy') && !isset($InlineImage['element']['attributes']['loading'])) { |
||
| 247 | $InlineImage['element']['attributes']['loading'] = 'lazy'; |
||
| 248 | } |
||
| 249 | // should be decoding async? |
||
| 250 | if ($this->config->isEnabled('pages.body.images.decoding') && !isset($InlineImage['element']['attributes']['decoding'])) { |
||
| 251 | $InlineImage['element']['attributes']['decoding'] = 'async'; |
||
| 252 | } |
||
| 253 | // add default class? |
||
| 254 | if ((string) $this->config->get('pages.body.images.class')) { |
||
| 255 | if (!\array_key_exists('class', $InlineImage['element']['attributes'])) { |
||
| 256 | $InlineImage['element']['attributes']['class'] = ''; |
||
| 257 | } |
||
| 258 | $InlineImage['element']['attributes']['class'] .= ' ' . (string) $this->config->get('pages.body.images.class'); |
||
| 259 | $InlineImage['element']['attributes']['class'] = trim($InlineImage['element']['attributes']['class']); |
||
| 260 | } |
||
| 261 | |||
| 262 | // disable remote image handling? |
||
| 263 | if (Util\File::isRemote($InlineImage['element']['attributes']['src']) && !$this->config->isEnabled('pages.body.images.remote')) { |
||
| 264 | return $this->createFigure($InlineImage); |
||
| 265 | } |
||
| 266 | |||
| 267 | // create asset |
||
| 268 | $assetOptions = ['leading_slash' => false]; |
||
| 269 | if ($this->config->isEnabled('pages.body.images.remote.fallback')) { |
||
| 270 | $assetOptions = ['leading_slash' => true]; |
||
| 271 | $assetOptions += ['fallback' => (string) $this->config->get('pages.body.images.remote.fallback')]; |
||
| 272 | } |
||
| 273 | $asset = new Asset($this->builder, $InlineImage['element']['attributes']['src'], $assetOptions); |
||
| 274 | $InlineImage['element']['attributes']['src'] = new Url($this->builder, $asset); |
||
| 275 | $width = $asset['width']; |
||
| 276 | |||
| 277 | /* |
||
| 278 | * Should be resized? |
||
| 279 | */ |
||
| 280 | $shouldResize = false; |
||
| 281 | $assetResized = null; |
||
| 282 | // pages.body.images.resize |
||
| 283 | if ( |
||
| 284 | \is_int($this->config->get('pages.body.images.resize')) |
||
| 285 | && $this->config->get('pages.body.images.resize') > 0 |
||
| 286 | && $width > $this->config->get('pages.body.images.resize') |
||
| 287 | ) { |
||
| 288 | $shouldResize = true; |
||
| 289 | $width = $this->config->get('pages.body.images.resize'); |
||
| 290 | } |
||
| 291 | // width attribute |
||
| 292 | if ( |
||
| 293 | isset($InlineImage['element']['attributes']['width']) |
||
| 294 | && $width > (int) $InlineImage['element']['attributes']['width'] |
||
| 295 | ) { |
||
| 296 | $shouldResize = true; |
||
| 297 | $width = (int) $InlineImage['element']['attributes']['width']; |
||
| 298 | } |
||
| 299 | // responsive images |
||
| 300 | if ( |
||
| 301 | $this->config->isEnabled('pages.body.images.responsive') |
||
| 302 | && !empty($this->config->getAssetsImagesWidths()) |
||
| 303 | && $width > max($this->config->getAssetsImagesWidths()) |
||
| 304 | ) { |
||
| 305 | $shouldResize = true; |
||
| 306 | $width = max($this->config->getAssetsImagesWidths()); |
||
| 307 | } |
||
| 308 | if ($shouldResize) { |
||
| 309 | try { |
||
| 310 | $assetResized = $asset->resize($width); |
||
| 311 | } catch (\Exception $e) { |
||
| 312 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 313 | |||
| 314 | return $this->createFigure($InlineImage); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | // set width |
||
| 319 | $InlineImage['element']['attributes']['width'] = $width; |
||
| 320 | // set height |
||
| 321 | $InlineImage['element']['attributes']['height'] = $assetResized['height'] ?? $asset['height']; |
||
| 322 | |||
| 323 | // placeholder |
||
| 324 | if ( |
||
| 325 | (!empty($this->config->get('pages.body.images.placeholder')) || isset($InlineImage['element']['attributes']['placeholder'])) |
||
| 326 | && \in_array($assetResized['subtype'] ?? $asset['subtype'], ['image/jpeg', 'image/png', 'image/gif']) |
||
| 327 | ) { |
||
| 328 | if (!\array_key_exists('placeholder', $InlineImage['element']['attributes'])) { |
||
| 329 | $InlineImage['element']['attributes']['placeholder'] = (string) $this->config->get('pages.body.images.placeholder'); |
||
| 330 | } |
||
| 331 | if (!\array_key_exists('style', $InlineImage['element']['attributes'])) { |
||
| 332 | $InlineImage['element']['attributes']['style'] = ''; |
||
| 333 | } |
||
| 334 | $InlineImage['element']['attributes']['style'] = trim($InlineImage['element']['attributes']['style'], ';'); |
||
| 335 | switch ($InlineImage['element']['attributes']['placeholder']) { |
||
| 336 | case 'color': |
||
| 337 | $InlineImage['element']['attributes']['style'] .= \sprintf(';max-width:100%%;height:auto;background-color:%s;', Image::getDominantColor($assetResized ?? $asset)); |
||
| 338 | break; |
||
| 339 | case 'lqip': |
||
| 340 | // aborts if animated GIF for performance reasons |
||
| 341 | if (Image::isAnimatedGif($assetResized ?? $asset)) { |
||
| 342 | break; |
||
| 343 | } |
||
| 344 | $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)); |
||
| 345 | break; |
||
| 346 | } |
||
| 347 | unset($InlineImage['element']['attributes']['placeholder']); |
||
| 348 | $InlineImage['element']['attributes']['style'] = trim($InlineImage['element']['attributes']['style']); |
||
| 349 | } |
||
| 350 | |||
| 351 | /* |
||
| 352 | * Should be responsive? |
||
| 353 | */ |
||
| 354 | $sizes = ''; |
||
| 355 | if ($this->config->isEnabled('pages.body.images.responsive')) { |
||
| 356 | try { |
||
| 357 | if ( |
||
| 358 | $srcset = Image::buildHtmlSrcsetW( |
||
| 359 | $assetResized ?? $asset, |
||
| 360 | $this->config->getAssetsImagesWidths() |
||
| 361 | ) |
||
| 362 | ) { |
||
| 363 | $InlineImage['element']['attributes']['srcset'] = $srcset; |
||
| 364 | $sizes = Image::getHtmlSizes($InlineImage['element']['attributes']['class'] ?? '', (array) $this->config->getAssetsImagesSizes()); |
||
| 365 | $InlineImage['element']['attributes']['sizes'] = $sizes; |
||
| 366 | } |
||
| 367 | } catch (\Exception $e) { |
||
| 368 | $this->builder->getLogger()->warning($e->getMessage()); |
||
| 369 | } |
||
| 370 | } |
||
| 371 | |||
| 372 | /* |
||
| 373 | <!-- if title: a <figure> is required to put in it a <figcaption> --> |
||
| 374 | <figure> |
||
| 375 | <!-- if formats: a <picture> is required for each <source> --> |
||
| 376 | <picture> |
||
| 377 | <source type="image/avif" |
||
| 378 | srcset="..." |
||
| 379 | sizes="..." |
||
| 380 | > |
||
| 381 | <source type="image/webp" |
||
| 382 | srcset="..." |
||
| 383 | sizes="..." |
||
| 384 | > |
||
| 385 | <img src="..." |
||
| 386 | srcset="..." |
||
| 387 | sizes="..." |
||
| 388 | > |
||
| 389 | </picture> |
||
| 390 | <figcaption><!-- title --></figcaption> |
||
| 391 | </figure> |
||
| 392 | */ |
||
| 393 | |||
| 394 | $image = $InlineImage; |
||
| 395 | |||
| 396 | // converts image to formats and put them in picture > source |
||
| 397 | if ( |
||
| 398 | \count($formats = ((array) $this->config->get('pages.body.images.formats'))) > 0 |
||
| 399 | && \in_array($assetResized['subtype'] ?? $asset['subtype'], ['image/jpeg', 'image/png', 'image/gif']) |
||
| 400 | ) { |
||
| 401 | try { |
||
| 402 | // abord if InlineImage is an animated GIF |
||
| 403 | if (Image::isAnimatedGif($assetResized ?? $asset)) { |
||
| 404 | $filepath = Util::joinFile($this->config->getOutputPath(), $assetResized['path'] ?? $asset['path'] ?? ''); |
||
| 405 | throw new RuntimeException(\sprintf('Asset "%s" is not converted (animated GIF).', $filepath)); |
||
| 406 | } |
||
| 407 | $sources = []; |
||
| 408 | foreach ($formats as $format) { |
||
| 409 | $srcset = ''; |
||
| 410 | try { |
||
| 411 | $assetConverted = ($assetResized ?? $asset)->convert($format); |
||
| 412 | // build responsive images? |
||
| 413 | if ($this->config->isEnabled('pages.body.images.responsive')) { |
||
| 414 | $srcset = Image::buildHtmlSrcset($assetConverted, $this->config->getAssetsImagesWidths()); |
||
| 415 | } |
||
| 416 | // if not, use default image as srcset |
||
| 417 | if (empty($srcset)) { |
||
| 418 | $srcset = (string) $assetConverted; |
||
| 419 | } |
||
| 420 | // add format to <sources> |
||
| 421 | $sources[] = [ |
||
| 422 | 'name' => 'source', |
||
| 423 | 'attributes' => [ |
||
| 424 | 'type' => "image/$format", |
||
| 425 | 'srcset' => $srcset, |
||
| 426 | 'sizes' => $sizes, |
||
| 427 | 'width' => $InlineImage['element']['attributes']['width'], |
||
| 428 | 'height' => $InlineImage['element']['attributes']['height'], |
||
| 429 | ], |
||
| 430 | ]; |
||
| 431 | } catch (\Exception $e) { |
||
| 432 | $this->builder->getLogger()->warning($e->getMessage()); |
||
| 433 | continue; |
||
| 434 | } |
||
| 435 | } |
||
| 436 | if (\count($sources) > 0) { |
||
| 437 | $picture = [ |
||
| 438 | 'extent' => $InlineImage['extent'], |
||
| 439 | 'element' => [ |
||
| 440 | 'name' => 'picture', |
||
| 441 | 'handler' => 'elements', |
||
| 442 | 'attributes' => [ |
||
| 443 | 'title' => $image['element']['attributes']['title'], |
||
| 444 | ], |
||
| 445 | ], |
||
| 446 | ]; |
||
| 447 | $picture['element']['text'] = $sources; |
||
| 448 | unset($image['element']['attributes']['title']); // phpstan-ignore unset.offset |
||
| 449 | $picture['element']['text'][] = $image['element']; |
||
| 450 | $image = $picture; |
||
| 451 | } |
||
| 452 | } catch (\Exception $e) { |
||
| 453 | $this->builder->getLogger()->debug($e->getMessage()); |
||
| 454 | } |
||
| 455 | } |
||
| 456 | |||
| 457 | return $this->createFigure($image); |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Image block. |
||
| 462 | */ |
||
| 463 | protected function blockImage($Excerpt) |
||
| 464 | { |
||
| 465 | if (1 !== preg_match($this->regexImage, $Excerpt['text'])) { |
||
| 466 | return; |
||
| 467 | } |
||
| 468 | |||
| 469 | $InlineImage = $this->inlineImage($Excerpt); |
||
| 470 | if (!isset($InlineImage)) { |
||
| 471 | return; |
||
| 472 | } |
||
| 473 | |||
| 474 | return $InlineImage; |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Note block-level markup. |
||
| 479 | * |
||
| 480 | * :::tip |
||
| 481 | * **Tip:** This is an advice. |
||
| 482 | * ::: |
||
| 483 | * |
||
| 484 | * Code inspired by https://github.com/sixlive/parsedown-alert from TJ Miller (@sixlive). |
||
| 485 | */ |
||
| 486 | protected function blockNote($block) |
||
| 487 | { |
||
| 488 | if (preg_match('/:::(.*)/', $block['text'], $matches)) { |
||
| 489 | $block = [ |
||
| 490 | 'char' => ':', |
||
| 491 | 'element' => [ |
||
| 492 | 'name' => 'aside', |
||
| 493 | 'text' => '', |
||
| 494 | 'attributes' => [ |
||
| 495 | 'class' => 'note', |
||
| 496 | ], |
||
| 497 | ], |
||
| 498 | ]; |
||
| 499 | if (!empty($matches[1])) { |
||
| 500 | $block['element']['attributes']['class'] .= " note-{$matches[1]}"; |
||
| 501 | } |
||
| 502 | |||
| 503 | return $block; |
||
| 504 | } |
||
| 505 | } |
||
| 506 | |||
| 507 | protected function blockNoteContinue($line, $block) |
||
| 520 | } |
||
| 521 | |||
| 522 | protected function blockNoteComplete($block) |
||
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Apply Highlight to code blocks. |
||
| 532 | */ |
||
| 533 | protected function blockFencedCodeComplete($block) |
||
| 558 | } |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * {@inheritdoc} |
||
| 563 | */ |
||
| 564 | protected function parseAttributeData($attributeString) |
||
| 565 | { |
||
| 566 | $attributes = preg_split('/[ ]+/', $attributeString, -1, PREG_SPLIT_NO_EMPTY); |
||
| 567 | $Data = []; |
||
| 568 | $HtmlAtt = []; |
||
| 569 | |||
| 570 | if (is_iterable($attributes)) { |
||
| 571 | foreach ($attributes as $attribute) { |
||
| 572 | switch ($attribute[0]) { |
||
| 573 | case '#': // ID |
||
| 574 | $Data['id'] = substr($attribute, 1); |
||
| 575 | break; |
||
| 576 | case '.': // Classes |
||
| 577 | $classes[] = substr($attribute, 1); |
||
| 578 | break; |
||
| 579 | default: // Attributes |
||
| 580 | parse_str($attribute, $parsed); |
||
| 581 | $HtmlAtt = array_merge($HtmlAtt, $parsed); |
||
| 582 | } |
||
| 583 | } |
||
| 584 | |||
| 585 | if (isset($classes)) { |
||
| 586 | $Data['class'] = implode(' ', $classes); |
||
| 587 | } |
||
| 588 | if (!empty($HtmlAtt)) { |
||
| 589 | foreach ($HtmlAtt as $a => $v) { |
||
| 590 | $Data[$a] = trim($v, '"'); |
||
| 591 | } |
||
| 592 | } |
||
| 593 | } |
||
| 594 | |||
| 595 | return $Data; |
||
| 596 | } |
||
| 597 | |||
| 598 | /** |
||
| 599 | * {@inheritdoc} |
||
| 600 | * |
||
| 601 | * Converts XHTML '<br />' tag to '<br>'. |
||
| 602 | * |
||
| 603 | * @return string |
||
| 604 | */ |
||
| 605 | protected function unmarkedText($text) |
||
| 606 | { |
||
| 607 | return str_replace('<br />', '<br>', parent::unmarkedText($text)); // @phpstan-ignore staticMethod.notFound |
||
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * {@inheritdoc} |
||
| 612 | * |
||
| 613 | * XHTML closing tag to HTML5 closing tag. |
||
| 614 | * |
||
| 615 | * @return string |
||
| 616 | */ |
||
| 617 | protected function element(array $Element) |
||
| 620 | } |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Turns a path relative to static or assets into a website relative path. |
||
| 624 | * |
||
| 625 | * "../../assets/images/img.jpeg" |
||
| 626 | * -> |
||
| 627 | * "/images/img.jpeg" |
||
| 628 | */ |
||
| 629 | private function normalizePath(string $path): string |
||
| 630 | { |
||
| 631 | // https://regex101.com/r/Rzguzh/1 |
||
| 632 | $pattern = \sprintf( |
||
| 633 | '(\.\.\/)+(\b%s|%s\b)+(\/.*)', |
||
| 634 | (string) $this->config->get('static.dir'), |
||
| 635 | (string) $this->config->get('assets.dir') |
||
| 636 | ); |
||
| 637 | $path = Util::joinPath($path); |
||
| 638 | if (!preg_match('/' . $pattern . '/is', $path, $matches)) { |
||
| 639 | return $path; |
||
| 640 | } |
||
| 641 | |||
| 642 | return $matches[3]; |
||
| 643 | } |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Create a media (video or audio) element from a link. |
||
| 647 | */ |
||
| 648 | private function createMediaFromLink(array $link, string $type = 'video'): array |
||
| 649 | { |
||
| 650 | $block = [ |
||
| 651 | 'extent' => $link['extent'], |
||
| 652 | 'element' => [ |
||
| 653 | 'text' => $link['element']['text'], |
||
| 654 | ], |
||
| 655 | ]; |
||
| 656 | $block['element']['attributes'] = $link['element']['attributes']; |
||
| 657 | unset($block['element']['attributes']['href']); |
||
| 658 | $block['element']['attributes']['src'] = new Url($this->builder, new Asset($this->builder, $link['element']['attributes']['href'])); |
||
| 659 | switch ($type) { |
||
| 660 | case 'video': |
||
| 661 | $block['element']['name'] = 'video'; |
||
| 662 | // no controls = autoplay, loop, muted, playsinline |
||
| 663 | if (!isset($block['element']['attributes']['controls'])) { |
||
| 664 | $block['element']['attributes']['autoplay'] = ''; |
||
| 665 | $block['element']['attributes']['loop'] = ''; |
||
| 666 | $block['element']['attributes']['muted'] = ''; |
||
| 667 | $block['element']['attributes']['playsinline'] = ''; |
||
| 668 | } |
||
| 669 | if (isset($block['element']['attributes']['poster'])) { |
||
| 670 | $block['element']['attributes']['poster'] = new Url($this->builder, new Asset($this->builder, $block['element']['attributes']['poster'])); |
||
| 671 | } |
||
| 672 | if (!\array_key_exists('style', $block['element']['attributes'])) { |
||
| 673 | $block['element']['attributes']['style'] = ''; |
||
| 674 | } |
||
| 675 | $block['element']['attributes']['style'] .= ';max-width:100%;height:auto;background-color:#d8d8d8;'; // background color if offline |
||
| 676 | |||
| 677 | return $block; |
||
| 678 | case 'audio': |
||
| 679 | $block['element']['name'] = 'audio'; |
||
| 680 | |||
| 681 | return $block; |
||
| 682 | } |
||
| 683 | |||
| 684 | throw new \Exception(\sprintf('Unable to create %s from "%s".', $type, $link['element']['attributes']['href'])); |
||
| 685 | } |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Create an embedded video element from a link. |
||
| 689 | * |
||
| 690 | * $baseSrc is the base URL to embed the video |
||
| 691 | * $match is the video ID or the rest of the URL to append to $baseSrc |
||
| 692 | */ |
||
| 693 | private function createEmbeddedVideoFromLink(array $link, string $baseSrc, string $match): array |
||
| 694 | { |
||
| 695 | $iframe = [ |
||
| 696 | 'element' => [ |
||
| 697 | 'name' => 'iframe', |
||
| 698 | 'text' => $link['element']['text'], |
||
| 699 | 'attributes' => [ |
||
| 700 | 'width' => '640', |
||
| 701 | 'height' => '360', |
||
| 702 | 'title' => $link['element']['text'], |
||
| 703 | 'src' => Util::joinPath($baseSrc, $match), |
||
| 704 | 'frameborder' => '0', |
||
| 705 | 'allow' => 'accelerometer;autoplay;encrypted-media;gyroscope;picture-in-picture;', |
||
| 706 | 'allowfullscreen' => '', |
||
| 707 | 'style' => 'position:absolute;top:0;left:0;width:100%;height:100%;border:0;background-color:#d8d8d8;', |
||
| 708 | ], |
||
| 709 | ], |
||
| 710 | ]; |
||
| 711 | |||
| 712 | return [ |
||
| 713 | 'extent' => $link['extent'], |
||
| 714 | 'element' => [ |
||
| 715 | 'name' => 'div', |
||
| 716 | 'handler' => 'elements', |
||
| 717 | 'text' => [ |
||
| 718 | $iframe['element'], |
||
| 719 | ], |
||
| 720 | 'attributes' => [ |
||
| 721 | 'style' => 'position:relative;padding-bottom:56.25%;height:0;overflow:hidden;', |
||
| 722 | 'title' => $link['element']['attributes']['title'], |
||
| 723 | ], |
||
| 724 | ], |
||
| 725 | ]; |
||
| 726 | } |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Create a figure > figcaption element. |
||
| 730 | */ |
||
| 731 | private function createFigure(array $inline): array |
||
| 732 | { |
||
| 733 | if (!$this->config->isEnabled('pages.body.images.caption')) { |
||
| 734 | return $inline; |
||
| 735 | } |
||
| 736 | |||
| 737 | if (empty($inline['element']['attributes']['title'])) { |
||
| 738 | return $inline; |
||
| 739 | } |
||
| 740 | |||
| 741 | $titleRawHtml = $this->line($inline['element']['attributes']['title']); // @phpstan-ignore method.notFound |
||
| 742 | $inline['element']['attributes']['title'] = strip_tags($titleRawHtml); |
||
| 743 | |||
| 744 | $figcaption = [ |
||
| 745 | 'element' => [ |
||
| 746 | 'name' => 'figcaption', |
||
| 747 | 'allowRawHtmlInSafeMode' => true, |
||
| 748 | 'rawHtml' => $titleRawHtml, |
||
| 749 | ], |
||
| 750 | ]; |
||
| 751 | $figure = [ |
||
| 752 | 'extent' => $inline['extent'], |
||
| 753 | 'element' => [ |
||
| 754 | 'name' => 'figure', |
||
| 755 | 'handler' => 'elements', |
||
| 756 | 'text' => [ |
||
| 757 | $inline['element'], |
||
| 758 | $figcaption['element'], |
||
| 759 | ], |
||
| 760 | ], |
||
| 761 | ]; |
||
| 762 | |||
| 763 | return $figure; |
||
| 764 | } |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Handle an external link. |
||
| 768 | */ |
||
| 769 | private function handleExternalLink(array $link): array |
||
| 794 | } |
||
| 795 | } |
||
| 796 |