spotify.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """
  2. Spotify Tag
  3. ---------
  4. This implements a Liquid-style spotify tag for Pelican,
  5. based on the jekyll / octopress youtube tag [1]_
  6. Syntax
  7. ------
  8. {% spotify id %}
  9. Example
  10. -------
  11. {% spotify 1HNZcRFlIKwHAJD3LxvX4d %}
  12. Output
  13. ------
  14. <iframe src='https://embed.spotify.com/?uri=spotify:track:1HNZcRFlIKwHAJD3LxvX4d' width='300' height='380' frameborder='0' allowtransparency='true'></iframe>
  15. """
  16. import os
  17. import re
  18. from .mdx_liquid_tags import LiquidTags
  19. SYNTAX = "{% spotify id %}"
  20. SPOTIFY = re.compile(r'(\w+)(\s+(\d+)\s(\d+))?')
  21. @LiquidTags.register('spotify')
  22. def spotify(preprocessor, tag, markup):
  23. spotify_id = None
  24. match = SPOTIFY.search(markup)
  25. if match:
  26. groups = match.groups()
  27. spotify_id = groups[0]
  28. if spotify_id:
  29. spotify_out = """
  30. <iframe src='https://embed.spotify.com/?uri=spotify:track:{}'
  31. width='300'
  32. height='380'
  33. frameborder='0'
  34. allowtransparency='true'></iframe>""".format(spotify_id).strip()
  35. else:
  36. raise ValueError("Error processing input, "
  37. "expected syntax: {0}".format(SYNTAX))
  38. return spotify_out
  39. #----------------------------------------------------------------------
  40. # This import allows image tag to be a Pelican plugin
  41. from liquid_tags import register