2
0
Fork 0
textout/textoutpc/builtin/_Video.py

145 lines
3.7 KiB
Python
Raw Normal View History

2018-01-02 18:57:04 +01:00
#!/usr/bin/env python3
#******************************************************************************
# Copyright (C) 2018 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the textoutpc project, which is MIT-licensed.
#******************************************************************************
2018-01-02 18:57:04 +01:00
import urllib.parse as _urlparse
2018-01-22 20:33:25 +01:00
from html import escape as _htmlescape
2018-01-02 18:57:04 +01:00
from .. import BlockTag as _BlockTag
2018-07-28 19:36:43 +02:00
2018-07-30 14:03:40 +02:00
__all__ = ["VideoTag", "YoutubeTag"]
2018-01-02 18:57:04 +01:00
2018-07-29 22:14:53 +02:00
_defaultratio_w = 16
_defaultratio_h = 9
2018-08-25 17:38:24 +02:00
class VideoTag(_BlockTag):
2018-01-02 18:57:04 +01:00
""" The video tag, puts a preview of the video whose URL is given.
Only a few 'big' services are supported for now.
Example uses:
2018-08-25 17:38:24 +02:00
2018-01-02 18:57:04 +01:00
[video]video_url[/video]
2018-07-29 19:51:42 +02:00
[video=4:3]video_url[/video]
2018-01-02 18:57:04 +01:00
[video tiny]video_url[/video tiny]
[video]https://www.youtube.com/watch?v=yhXpV8hRKxQ[/video]
"""
aliases = ('[video]', '[video tiny]')
raw = True
noinline = True
2018-01-20 10:26:23 +01:00
def prepare(self, name, value):
""" Prepare the video tag. """
2018-07-29 19:51:42 +02:00
_align = {
'center': ('center', False),
'centre': ('center', False),
'left': ('left', False),
'gauche': ('left', False),
2018-07-29 19:51:42 +02:00
'right': ('right', False),
'droite': ('right', False),
2018-07-29 19:51:42 +02:00
'float': (None, True),
'floating': (None, True),
'flotte': (None, True),
'flottant': (None, True),
2018-07-29 19:51:42 +02:00
'float-left': ('left', True),
'float-center': ('center', True),
'float-centre': ('center', True),
'float-right': ('right', True),
}
self._sizeclass = "video-tiny" if "tiny" in name \
2018-07-29 19:51:42 +02:00
else None
self._align = None
self._float = False
2018-07-30 14:29:17 +02:00
self._ratio = None
2018-07-29 19:51:42 +02:00
for arg in map(str.strip, (value or "").split('|')):
if not arg:
pass
elif arg[0] in '0123456789:':
2018-07-29 22:14:53 +02:00
rx, ry = _defaultratio_w, _defaultratio_h
2018-07-30 14:29:17 +02:00
rn = 0
2018-07-29 19:51:42 +02:00
rat = arg.split(':')
2018-07-30 14:29:17 +02:00
try: rx = int(rat[0]); rn += 1
2018-07-29 19:51:42 +02:00
except: pass
2018-07-30 14:29:17 +02:00
try: ry = int(rat[1]); rn += 1
2018-07-29 19:51:42 +02:00
except: pass
2018-07-30 14:29:17 +02:00
if rn:
self._ratio = round(ry / rx, 4)
2018-07-29 19:51:42 +02:00
elif arg in _align:
al, fl = _align[arg]
if al != None:
self._align = al
if fl:
self._float = True
2018-01-20 10:26:23 +01:00
def preprocess(self, content):
try:
self._video = self.video(content)
except:
url = _urlparse.urlparse(content)
2018-08-25 17:38:24 +02:00
if url.scheme not in ('http', 'https'):
raise Exception("No allowed prefix!")
self._video = content
2018-01-02 18:57:04 +01:00
def content_html(self):
""" Produce the embed code for the given type. """
if isinstance(self._video, str):
url = _htmlescape(self._video)
2020-07-01 12:10:35 +02:00
target = self.tweak("link_target", "").casefold()
tattrs = ''
if target == 'blank':
tattrs = ' target="_blank" rel="noopener"'
return '<p><a href="{}"{}>{}</a></p>'.format(url, tattrs, url)
2018-01-20 10:26:23 +01:00
2018-07-29 19:51:42 +02:00
align = "float-" + (self._align or "left") if self._align \
else self._align
2018-07-30 14:29:17 +02:00
if self._ratio:
ratio = self._ratio * 100
elif hasattr(self._video, 'ratio'):
ratio = self._video.ratio * 100
2018-07-29 22:14:53 +02:00
else:
ratio = round(_defaultratio_h / _defaultratio_w, 4) * 100
2018-07-30 14:29:17 +02:00
iratio = int(ratio)
if ratio == iratio:
ratio = iratio
ratio = str(ratio)
2018-07-29 22:14:53 +02:00
2018-07-29 19:51:42 +02:00
code = '<div class="video-wrapper{}{}"{}>' \
.format(f" {self._sizeclass}" if self._sizeclass else "",
f' img-{align}' if align else "",
2018-07-30 14:29:17 +02:00
f' style="padding-bottom: {ratio}%"')
2018-01-20 10:26:23 +01:00
code += '<iframe src="{}" frameborder="0" allowfullscreen>' \
2018-07-30 14:29:17 +02:00
'</iframe>'.format(self._video.embed)
2018-01-20 10:26:23 +01:00
return code + '</div>'
2018-01-02 18:57:04 +01:00
def content_lightscript(self):
url = self._url.replace('[', '%5B').replace(']', '%5D')
return '[[image:{}]]'.format(url)
2018-08-25 17:38:24 +02:00
2018-07-30 14:03:40 +02:00
class YoutubeTag(VideoTag):
""" Alias for the video tag with only the Youtube possibility.
Example uses:
2018-08-25 17:38:24 +02:00
2018-07-30 14:03:40 +02:00
[youtube]okMK1NYRySI[/youtube] """
aliases = ('[youtube]',)
def preprocess(self, content):
super().preprocess(f'https://www.youtube.com/watch?v={content}')
2018-01-02 18:57:04 +01:00
# End of file.