video.py 2.3 KB

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