youtube.py 1.7 KB

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