Skip to content

Instantly share code, notes, and snippets.

@raek
Created September 9, 2022 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raek/7097fd949d80e58d3b7a410333957d72 to your computer and use it in GitHub Desktop.
Save raek/7097fd949d80e58d3b7a410333957d72 to your computer and use it in GitHub Desktop.
Sphinx extension for embedding a Youtube video player

Here is a video I like:

I2Q35uFCq8Q

Neat, huh?

from docutils import nodes
from docutils.parsers.rst import Directive
class youtube(nodes.General, nodes.Element):
pass
def html_visit_youtube(self, node):
self.body.append(f'<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/{node["video_id"]}" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>')
raise nodes.SkipNode()
class YoutubeDirective(Directive):
required_arguments = 1
def run(self):
video_id, = self.arguments
return[youtube('', video_id=video_id)]
def setup(app):
app.add_node(youtube, html=(html_visit_youtube, None))
app.add_directive("youtube", YoutubeDirective)
return {
"version": "0.1",
"parallel_read_safe": True,
"parallel_write_safe": True,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment