spotify.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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
  15. src='https://embed.spotify.com/?uri=spotify:track:1HNZcRFlIKwHAJD3LxvX4d'
  16. width='300' height='380' frameborder='0' allowtransparency='true'>
  17. </iframe>
  18. """
  19. import re
  20. from .mdx_liquid_tags import LiquidTags
  21. SYNTAX = "{% spotify id %}"
  22. SPOTIFY = re.compile(r'(\w+)(\s+(\d+)\s(\d+))?')
  23. @LiquidTags.register('spotify')
  24. def spotify(preprocessor, tag, markup):
  25. spotify_id = None
  26. match = SPOTIFY.search(markup)
  27. if match:
  28. groups = match.groups()
  29. spotify_id = groups[0]
  30. if spotify_id:
  31. spotify_out = """
  32. <iframe src='https://embed.spotify.com/?uri=spotify:track:{}'
  33. width='300'
  34. height='380'
  35. frameborder='0'
  36. allowtransparency='true'></iframe>""".format(spotify_id).strip()
  37. else:
  38. raise ValueError("Error processing input, "
  39. "expected syntax: {0}".format(SYNTAX))
  40. return spotify_out
  41. # ---------------------------------------------------
  42. # This import allows image tag to be a Pelican plugin
  43. from liquid_tags import register # noqa