Skip to content

Instantly share code, notes, and snippets.

@brianhsu
Created December 2, 2011 10:47
Show Gist options
  • Save brianhsu/1422773 to your computer and use it in GitHub Desktop.
Save brianhsu/1422773 to your computer and use it in GitHub Desktop.
Pelican YouTube embed reStructuredText directive.
class YouTube(Directive):
""" Embed YouTube video in posts.
VIDEO_ID is required, with / height are optional integer,
and align could be left / center / right.
Usage:
.. youtube:: VIDEO_ID
:width: 640
:height: 480
:align: center
"""
def align(argument):
"""Conversion function for the "align" option."""
return directives.choice(argument, ('left', 'center', 'right'))
required_arguments = 1
optional_arguments = 2
option_spec = {
'width': directives.positive_int,
'height': directives.positive_int,
'align': align
}
final_argument_whitespace = False
has_content = False
def run(self):
videoID = self.arguments[0].strip()
width = 420
height = 315
align = 'left'
if 'width' in self.options:
width = self.options['width']
if 'height' in self.options:
height = self.options['height']
if 'align' in self.options:
align = self.options['align']
youtube_url = 'http://www.youtube.com/embed/%s' % videoID
div_block = '<div class="youtube" align="%s">' % align
embed_block = '<iframe width="%s" height="%s" src="%s" frameborder="0"></iframe>' % (width, height, youtube_url)
return [
nodes.raw('', div_block, format='html'),
nodes.raw('', embed_block, format='html'),
nodes.raw('', '</div>', format='html')
]
directives.register_directive('youtube', YouTube)
@Natim
Copy link

Natim commented Jun 7, 2012

This looks good, where do you put that ?

@brianhsu
Copy link
Author

brianhsu commented Jun 8, 2012

Just put it at the bottom of pelican/rstdirective.py, and you should be able to use the youtube directive.

@almet
Copy link

almet commented Jun 8, 2012

Is it okay if I take this and put it in pelican directly?

@brianhsu
Copy link
Author

brianhsu commented Jun 8, 2012

@ametaireau,

Of course! Feel free to take it. :)

@almet
Copy link

almet commented Jun 8, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment