video.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """
  2. Video Tag
  3. ---------
  4. This implements a Liquid-style video tag for Pelican,
  5. based on the octopress video tag [1]_
  6. Syntax
  7. ------
  8. {% video url/to/video [width height] [url/to/poster] %}
  9. Example
  10. -------
  11. {% video http://site.com/video.mp4 720 480 http://site.com/poster-frame.jpg %}
  12. Output
  13. ------
  14. <video width='720' height='480' preload='none' controls poster='http://site.com/poster-frame.jpg'>
  15. <source src='http://site.com/video.mp4' type='video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"'/>
  16. </video>
  17. [1] https://github.com/imathis/octopress/blob/master/plugins/video_tag.rb
  18. """
  19. import os
  20. import re
  21. from .mdx_liquid_tags import LiquidTags
  22. SYNTAX = "{% video url/to/video [url/to/video] [url/to/video] [width height] [url/to/poster] %}"
  23. VIDEO = re.compile(r'(/\S+|https?:\S+)(\s+(/\S+|https?:\S+))?(\s+(/\S+|https?:\S+))?(\s+(\d+)\s(\d+))?(\s+(/\S+|https?:\S+))?')
  24. VID_TYPEDICT = {'.mp4':"type='video/mp4; codecs=\"avc1.42E01E, mp4a.40.2\"'",
  25. '.ogv':"type='video/ogg; codecs=theora, vorbis'",
  26. '.webm':"type='video/webm; codecs=vp8, vorbis'"}
  27. @LiquidTags.register('video')
  28. def video(preprocessor, tag, markup):
  29. videos = []
  30. width = None
  31. height = None
  32. poster = None
  33. match = VIDEO.search(markup)
  34. if match:
  35. groups = match.groups()
  36. videos = [g for g in groups[0:6:2] if g]
  37. width = groups[6]
  38. height = groups[7]
  39. poster = groups[9]
  40. if any(videos):
  41. video_out = "<video width='{width}' height='{height}' preload='none' controls poster='{poster}'>".format(width=width, height=height, poster=poster)
  42. for vid in videos:
  43. base, ext = os.path.splitext(vid)
  44. if ext not in VID_TYPEDICT:
  45. raise ValueError("Unrecognized video extension: "
  46. "{0}".format(ext))
  47. video_out += ("<source src='{0}' "
  48. "{1}>".format(vid, VID_TYPEDICT[ext]))
  49. video_out += "</video>"
  50. else:
  51. raise ValueError("Error processing input, "
  52. "expected syntax: {0}".format(SYNTAX))
  53. return video_out
  54. #----------------------------------------------------------------------
  55. # This import allows image tag to be a Pelican plugin
  56. from liquid_tags import register