Added nicovideo embedding support to [video] tag, closes #171.

This commit is contained in:
flash 2019-04-02 21:35:30 +02:00
parent 0af1216e8f
commit a3862b3d45

View file

@ -5,9 +5,11 @@ use Misuzu\Parsers\BBCode\BBCodeTag;
final class VideoTag extends BBCodeTag
{
private const YOUTUBE_URL_REGEX = '#^(?:www\.)?youtube(?:-nocookie)?\.(?:[a-z]{2,63})$#u';
private const YOUTUBE_REGEX = '#^(?:www\.)?youtube(?:-nocookie)?\.(?:[a-z]{2,63})$#u';
private const YOUTUBE_EMBED = '<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/%s?rel=0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';
private const NICODOUGA_EMBED = '<script async type="application/javascript" src="https://embed.nicovideo.jp/watch/%1$s/script?w=560&h=315" width="560" height="315"></script><noscript><a href="https://www.nicovideo.jp/watch/%1$s">Embedded Video</a></noscript>';
public function parseText(string $text): string
{
return preg_replace_callback(
@ -27,7 +29,7 @@ final class VideoTag extends BBCodeTag
return sprintf(self::YOUTUBE_EMBED, $url['path']);
}
if (!empty($url['query']) && ($url['path'] ?? '') === '/watch' && preg_match(self::YOUTUBE_URL_REGEX, $url['host'])) {
if (!empty($url['query']) && ($url['path'] ?? '') === '/watch' && preg_match(self::YOUTUBE_REGEX, $url['host'])) {
parse_str(html_entity_decode($url['query']), $ytQuery);
if (!empty($ytQuery['v']) && preg_match('#^([a-zA-Z0-9_-]+)$#u', $ytQuery['v'])) {
@ -35,6 +37,14 @@ final class VideoTag extends BBCodeTag
}
}
if ($url['host'] === 'nicovideo.jp' || $url['host'] === 'www.nicovideo.jp') {
$splitPath = explode('/', trim($url['path'], '/'));
if (count($splitPath) > 1 && $splitPath[0] === 'watch') {
return sprintf(self::NICODOUGA_EMBED, $splitPath[1]);
}
}
$mediaUrl = url_proxy_media($matches[1]);
return sprintf('<video controls src="%s" style="max-width:100%%;max-height:100%%;"></video>', $mediaUrl);
},