youtube.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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
  15. width="640" height="480" src="https://www.youtube.com/embed/dQw4w9WgXcQ"
  16. frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen>
  17. </iframe>
  18. [1] https://gist.github.com/jamieowen/2063748
  19. """
  20. import re
  21. from .mdx_liquid_tags import LiquidTags
  22. SYNTAX = "{% youtube id [width height] %}"
  23. YOUTUBE = re.compile(r'([\S]+)(\s+(\d+)\s(\d+))?')
  24. @LiquidTags.register('youtube')
  25. def youtube(preprocessor, tag, markup):
  26. width = 640
  27. height = 390
  28. youtube_id = None
  29. match = YOUTUBE.search(markup)
  30. if match:
  31. groups = match.groups()
  32. youtube_id = groups[0]
  33. width = groups[2] or width
  34. height = groups[3] or height
  35. if youtube_id:
  36. youtube_out = """
  37. <div class="videobox">
  38. <iframe width="{width}" height="{height}"
  39. src='https://www.youtube.com/embed/{youtube_id}'
  40. frameborder='0' webkitAllowFullScreen mozallowfullscreen
  41. allowFullScreen>
  42. </iframe>
  43. </div>
  44. """.format(width=width, height=height, youtube_id=youtube_id).strip()
  45. else:
  46. raise ValueError("Error processing input, "
  47. "expected syntax: {0}".format(SYNTAX))
  48. return youtube_out
  49. # ---------------------------------------------------
  50. # This import allows image tag to be a Pelican plugin
  51. from liquid_tags import register # noqa