2024-06-20 14:10:42 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright 2024 Daniel Pimley
|
|
|
|
* @license https://github.com/xenocrat/chyrp-markdown/blob/master/LICENSE
|
|
|
|
* @link https://github.com/xenocrat/chyrp-markdown#readme
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace xenocrat\markdown\inline;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds images, embedded audio and video.
|
|
|
|
* This method overloads LinkTrait::renderImage().
|
|
|
|
*/
|
|
|
|
trait MediaLinkTrait
|
|
|
|
{
|
2025-01-13 09:56:01 +00:00
|
|
|
/**
|
|
|
|
* @var bool Render video and audio with a deferred loading attribute.
|
|
|
|
*/
|
|
|
|
public $renderLazyMedia = false;
|
|
|
|
|
2024-06-20 14:10:42 +00:00
|
|
|
protected function renderImage($block): string
|
|
|
|
{
|
|
|
|
if (isset($block['refkey'])) {
|
|
|
|
if (($ref = $this->lookupReference($block['refkey'])) !== false) {
|
|
|
|
$block = array_merge($block, $ref);
|
|
|
|
} else {
|
|
|
|
if (str_starts_with($block['orig'], '![')) {
|
|
|
|
return '!['
|
|
|
|
. $this->renderAbsy(
|
|
|
|
$this->parseInline(substr($block['orig'], 2))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $block['orig'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
preg_match('/\.(mpe?g|mp4|m4v|mov|webm|ogv)$/i', $block['url'])
|
|
|
|
) {
|
2025-01-13 09:56:01 +00:00
|
|
|
return '<video controls="" src="'
|
2024-06-20 14:10:42 +00:00
|
|
|
. $this->escapeHtmlEntities($block['url'], ENT_COMPAT) . '"'
|
2025-01-13 09:56:01 +00:00
|
|
|
. (
|
|
|
|
empty($block['width']) ?
|
|
|
|
'' :
|
|
|
|
' width="' . $block['width'] . '"'
|
|
|
|
)
|
|
|
|
. (
|
|
|
|
empty($block['height']) ?
|
|
|
|
'' :
|
|
|
|
' height="' . $block['height'] . '"'
|
|
|
|
)
|
|
|
|
. (
|
|
|
|
$this->renderLazyMedia ?
|
|
|
|
' preload="none"' :
|
|
|
|
' preload="metadata"'
|
|
|
|
)
|
2024-06-20 14:10:42 +00:00
|
|
|
. (
|
|
|
|
empty($block['title']) ?
|
|
|
|
'' :
|
|
|
|
' title="'
|
|
|
|
. $this->escapeHtmlEntities(
|
|
|
|
$block['title'],
|
|
|
|
ENT_COMPAT | ENT_SUBSTITUTE
|
|
|
|
)
|
|
|
|
. '"'
|
|
|
|
)
|
|
|
|
. '>'
|
|
|
|
. $this->renderAbsy($this->parseInline($block['text']))
|
|
|
|
. '</video>';
|
|
|
|
} elseif (
|
|
|
|
preg_match('/\.(mp3|m4a|oga|ogg|spx|wav|aiff?)$/i', $block['url'])
|
|
|
|
) {
|
2025-01-13 09:56:01 +00:00
|
|
|
return '<audio controls="" src="'
|
2024-06-20 14:10:42 +00:00
|
|
|
. $this->escapeHtmlEntities($block['url'], ENT_COMPAT) . '"'
|
2025-01-13 09:56:01 +00:00
|
|
|
. (
|
|
|
|
empty($block['width']) ?
|
|
|
|
'' :
|
|
|
|
' width="' . $block['width'] . '"'
|
|
|
|
)
|
|
|
|
. (
|
|
|
|
empty($block['height']) ?
|
|
|
|
'' :
|
|
|
|
' height="' . $block['height'] . '"'
|
|
|
|
)
|
|
|
|
. (
|
|
|
|
$this->renderLazyMedia ?
|
|
|
|
' preload="none"' :
|
|
|
|
' preload="metadata"'
|
|
|
|
)
|
2024-06-20 14:10:42 +00:00
|
|
|
. (
|
|
|
|
empty($block['title']) ?
|
|
|
|
'' :
|
|
|
|
' title="'
|
|
|
|
. $this->escapeHtmlEntities(
|
|
|
|
$block['title'],
|
|
|
|
ENT_COMPAT | ENT_SUBSTITUTE
|
|
|
|
)
|
|
|
|
. '"'
|
|
|
|
)
|
|
|
|
. '>'
|
|
|
|
. $this->renderAbsy($this->parseInline($block['text']))
|
|
|
|
. '</audio>';
|
|
|
|
} else {
|
|
|
|
return '<img src="'
|
|
|
|
. $this->escapeHtmlEntities($block['url'], ENT_COMPAT) . '"'
|
|
|
|
. ' alt="'
|
|
|
|
. $this->escapeHtmlEntities(
|
|
|
|
$block['text'],
|
|
|
|
ENT_COMPAT | ENT_SUBSTITUTE
|
|
|
|
)
|
|
|
|
. '"'
|
2025-01-13 09:56:01 +00:00
|
|
|
. (
|
|
|
|
empty($block['width']) ?
|
|
|
|
'' :
|
|
|
|
' width="' . $block['width'] . '"'
|
|
|
|
)
|
|
|
|
. (
|
|
|
|
empty($block['height']) ?
|
|
|
|
'' :
|
|
|
|
' height="' . $block['height'] . '"'
|
|
|
|
)
|
|
|
|
. (
|
|
|
|
$this->renderLazyImages ?
|
|
|
|
' loading="lazy"' :
|
|
|
|
''
|
|
|
|
)
|
2024-06-20 14:10:42 +00:00
|
|
|
. (
|
|
|
|
empty($block['title']) ?
|
|
|
|
'' :
|
|
|
|
' title="'
|
|
|
|
. $this->escapeHtmlEntities(
|
|
|
|
$block['title'],
|
|
|
|
ENT_COMPAT | ENT_SUBSTITUTE
|
|
|
|
)
|
|
|
|
. '"'
|
|
|
|
)
|
|
|
|
. ($this->html5 ? '>' : ' />');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract protected function parseImage($markdown);
|
|
|
|
abstract protected function parseInline($text);
|
|
|
|
abstract protected function renderAbsy($blocks);
|
|
|
|
abstract protected function escapeHtmlEntities($text, $flags = 0);
|
|
|
|
}
|