youtube.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. """
  2. Youtube Tag
  3. ---------
  4. This implements a Liquid-style youtube tag for Pelican,
  5. based on the jekyll / octopress youtube tag [1]_
  6. Syntax
  7. ------
  8. {% youtube id [width height] %}
  9. Example
  10. -------
  11. {% youtube dQw4w9WgXcQ 640 480 %}
  12. Output
  13. ------
  14. <iframe width="640" height="480" src="http://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
  15. [1] https://gist.github.com/jamieowen/2063748
  16. """
  17. import os
  18. import re
  19. from .mdx_liquid_tags import LiquidTags
  20. SYNTAX = "{% youtube id [width height] %}"
  21. YOUTUBE = re.compile(r'([\S]+)(\s+(\d+)\s(\d+))?')
  22. @LiquidTags.register('youtube')
  23. def youtube(preprocessor, tag, markup):
  24. width = 640
  25. height = 390
  26. youtube_id = None
  27. match = YOUTUBE.search(markup)
  28. if match:
  29. groups = match.groups()
  30. youtube_id = groups[0]
  31. width = groups[2] or width
  32. height = groups[3] or height
  33. if youtube_id:
  34. youtube_out = """
  35. <div class="videobox">
  36. <iframe width="{width}" height="{height}"
  37. src='http://www.youtube.com/v/{youtube_id}'
  38. frameborder='0'
  39. webkitAllowFullScreen mozallowfullscreen allowFullScreen>
  40. </iframe>
  41. </div>
  42. """.format(width=width, height=height, youtube_id=youtube_id).strip()
  43. else:
  44. raise ValueError("Error processing input, "
  45. "expected syntax: {0}".format(SYNTAX))
  46. return youtube_out
  47. #----------------------------------------------------------------------
  48. # This import allows image tag to be a Pelican plugin
  49. from liquid_tags import register