youtube.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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'(\w+)(\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 = "<iframe width='{width}' height='{height}' src='http://www.youtube.com/embed/{youtube_id}' frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>".format(width=width, height=height, youtube_id=youtube_id)
  35. else:
  36. raise ValueError("Error processing input, "
  37. "expected syntax: {0}".format(SYNTAX))
  38. return youtube_out
  39. #----------------------------------------------------------------------
  40. # This import allows image tag to be a Pelican plugin
  41. from liquid_tags import register