gram.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """
  2. Instagram Image Tag
  3. -------------------
  4. By `Tom Spalding <https://github.com/digitalvapor>`_
  5. You can see a working example at `antivapor.net/instagram-tag.html <http://antivapor.net/instagram-tag.html>`_.
  6. Based on `Liquid Image Tag <https://github.com/getpelican/pelican-plugins/blob/master/liquid_tags/img.py>`_ by `Jake Vanderplas <https://github.com/jakevdp>`_.
  7. Optional Todo:
  8. * Query JSON to automatically include descriptions.
  9. http://api.instagram.com/oembed?url=http://instagr.am/p/olw8jXiz1_/
  10. and option to add wrapping anchor link to original http://instagram.com/p/olw8jXiz1_
  11. * Default to size m
  12. http://instagr.am/p/olw8jXiz1_/media/?size=t
  13. http://instagr.am/p/olw8jXiz1_/media
  14. * Provide examples using with [Better Figures and Images](https://github.com/getpelican/pelican-plugins/tree/master/better_figures_and_images).
  15. Syntax
  16. ------
  17. {% gram shortcode [size] [width] [class name(s)] [title text | "title text" ["alt text"]] %}
  18. where size is t, m, or l, and it defaults to m. see http://instagram.com/developer/embedding.
  19. Examples
  20. --------
  21. {% gram pFG7naIZkr t %}
  22. {% gram pFJE11IZnx %}
  23. {% gram pFI0CAIZna l 400 figure 'pretty turkey tail fungus' %}
  24. {% gram rOru21oZpe l 450 test_class instagram 'warehouse window title' 'alt text' %}
  25. Output
  26. ------
  27. <img src="http://photos-c.ak.instagram.com/hphotos-ak-xaf1/t51.2885-15/917172_604907902963826_254280879_n.jpg" width="450" title="warehouse window title" alt="alt text" class="test_class instagram">
  28. """
  29. import re
  30. import urllib
  31. from .mdx_liquid_tags import LiquidTags
  32. SYNTAX = '{% gram shortcode [size] [width] [class name(s)] [title text | "title text" ["alt text"]] %}'
  33. # Regular expression for full syntax
  34. # ReGram = re.compile("""(?P<shortcode>\S+)(?:\s+(?P<size>[tml]?))?(?:\s+(?P<width>\d*))?(?:\s+(?P<class>\S*))?(?P<title>\s+.+)?""")
  35. ReGram = re.compile("""(?P<shortcode>\S+)(?:\s+(?P<size>[tml]?))?(?:\s+(?P<width>\d*))?(?:\s+(?P<class>[^']*))?(?P<title>.+)?""")
  36. # Regular expression to split the title and alt text
  37. ReTitleAlt = re.compile("""(?:"|')(?P<title>[^"']+)?(?:"|')\s+(?:"|')(?P<alt>[^"']+)?(?:"|')""")
  38. @LiquidTags.register('gram')
  39. def gram(preprocessor, tag, markup):
  40. attrs = None
  41. # Parse the markup string
  42. match = ReGram.search(markup)
  43. if match:
  44. attrs = dict([(key, val.strip())
  45. for (key, val) in match.groupdict().iteritems() if val])
  46. else:
  47. raise ValueError('Error processing input. '
  48. 'Expected syntax: {0}'.format(SYNTAX))
  49. # Construct URI
  50. #print(attrs)
  51. shortcode = attrs['shortcode']
  52. url = 'http://instagr.am/p/'+shortcode+'/media/'
  53. del attrs['shortcode']
  54. if 'size' in attrs:
  55. size = '?size={0}'.format(attrs['size'])
  56. url = url+size
  57. del attrs['size']
  58. r = urllib.urlopen(url)
  59. if(r.getcode()==404):
  60. raise ValueError('%s isnt a photo.'%shortcode)
  61. gram_url = r.geturl()
  62. # Check if alt text is present -- if so, split it from title
  63. if 'title' in attrs:
  64. match = ReTitleAlt.search(attrs['title'])
  65. if match:
  66. attrs.update(match.groupdict())
  67. if not attrs.get('alt'):
  68. attrs['alt'] = attrs['title']
  69. #print('updated dict: '+repr(attrs))
  70. # Return the formatted text
  71. return '<img src="{0}"{1}>'.format(gram_url,' '.join(' {0}="{1}"'.format(key,val) for (key,val) in attrs.iteritems()))
  72. #----------------------------------------------------------------------
  73. # This import allows image tag to be a Pelican plugin
  74. from liquid_tags import register