pelican-peertube/pelican_peertube/peertube.py

105 lines
3.3 KiB
Python

# -*- 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}?"
width = self.options["width"] if "width" in self.options else 560
height = self.options["height"] if "height" in self.options else 315
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 = (
self.options["title_show"] if "title_show" in self.options else ""
)
privacy_show = (
self.options["privacy_show"] if "privacy_show" in self.options else ""
)
controls_show = (
self.options["controls_show"] if "controls_show" in self.options else ""
)
iframe_arguments = [
(width, "width={}"),
(height, "height={}"),
(fullscreen, "allowfullscreen"),
(seamless, "frameborder='0'"),
]
url_arguments = [
(autoplay, "autoplay={}"),
(muted, "muted={}"),
(loop, "loop={}"),
(title_show, "title={}"),
(privacy_show, "warningTitle={}"),
(controls_show, "controls={}"),
]
for value, format in url_arguments:
toggle = 1 if value == True else 0
url += format.format(toggle) if value else ""
url.replace("'", "")
embed_block = "<iframe src='{}' ".format(url)
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)