pelican-peertube/pelican_peertube/peertube.py

105 lines
3.3 KiB
Python
Raw Permalink Normal View History

2019-12-22 23:19:23 -05:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from docutils import nodes
from docutils.parsers.rst import directives, Directive
class PeerTube(Directive):
""" Embeds PeerTube video in posts.
Based on pelican_peertube by kura
https://github.com/kura/pelican_youtube/
INSTANCE_URL & VIDEO_ID is required, other arguments are optional
Usage:
..peertube:: INSTANCE_URL VIDEO_ID
"""
def boolean(self, argument):
"""Conversion function for yes/no Trues/False."""
value = directives.choice(argument, ("yes", "True", "no", "False"))
return value in ("yes", "True")
required_arguments = 2
optional_arguments = 8
optional_spec = {
"width": directives.positive_int,
"height": directives.positive_int,
"fullscreen": boolean,
"seamless": boolean,
"autoplay": boolean,
"muted": boolean,
"loop": boolean,
"title_show": boolean,
"privacy_show": boolean,
"controls_show": boolean,
}
final_argument_whitespace = False
has_content = False
def run(self):
instanceURL = self.arguments[0].strip()
videoID = self.arguments[1].strip()
url = f"https://{instanceURL}/videos/embed/{videoID}?"
2019-12-24 10:26:36 -05:00
width = self.options["width"] if "width" in self.options else 560
height = self.options["height"] if "height" in self.options else 315
2019-12-22 23:19:23 -05:00
fullscreen = (
self.options["fullscreen"] if "fullscreen" in self.options else True
)
seamless = self.options["seamless"] if "seamless" in self.options else True
autoplay = self.options["autoplay"] if "autoplay" in self.options else False
muted = self.options["muted"] if "muted" in self.options else False
loop = self.options["loop"] if "loop" in self.options else False
title_show = (
2019-12-24 10:26:36 -05:00
self.options["title_show"] if "title_show" in self.options else ""
2019-12-22 23:19:23 -05:00
)
privacy_show = (
2019-12-24 10:26:36 -05:00
self.options["privacy_show"] if "privacy_show" in self.options else ""
2019-12-22 23:19:23 -05:00
)
controls_show = (
2019-12-24 10:26:36 -05:00
self.options["controls_show"] if "controls_show" in self.options else ""
2019-12-22 23:19:23 -05:00
)
iframe_arguments = [
2019-12-24 10:26:36 -05:00
(width, "width={}"),
(height, "height={}"),
2019-12-22 23:19:23 -05:00
(fullscreen, "allowfullscreen"),
(seamless, "frameborder='0'"),
]
url_arguments = [
2019-12-24 10:26:36 -05:00
(autoplay, "autoplay={}"),
(muted, "muted={}"),
(loop, "loop={}"),
(title_show, "title={}"),
(privacy_show, "warningTitle={}"),
(controls_show, "controls={}"),
2019-12-22 23:19:23 -05:00
]
for value, format in url_arguments:
toggle = 1 if value == True else 0
2019-12-24 10:26:36 -05:00
url += format.format(toggle) if value else ""
url.replace("'", "")
embed_block = "<iframe src='{}' ".format(url)
2019-12-22 23:19:23 -05:00
for value, format in iframe_arguments:
embed_block += (format + " ").format(value) if value else ""
embed_block = embed_block[:-1] + "></iframe>"
return [
nodes.raw("", "<div class=peertube>", format="html"),
nodes.raw("", embed_block, format="html"),
nodes.raw("", "</div>", format="html"),
]
def register():
directives.register_directive("peertube", PeerTube)