b64img.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """
  2. Image Tag
  3. ---------
  4. This implements a Liquid-style image tag for Pelican,
  5. based on the liquid img tag which is based on the octopress image tag [1]_
  6. Syntax
  7. ------
  8. {% b64img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}
  9. Examples
  10. --------
  11. {% b64img /images/ninja.png Ninja Attack! %}
  12. {% b64img left half http://site.com/images/ninja.png Ninja Attack! %}
  13. {% b64img left half http://site.com/images/ninja.png 150 150 "Ninja Attack!" "Ninja in attack posture" %}
  14. Output
  15. ------
  16. <img src="data:;base64,....">
  17. <img class="left half" src="data:;base64,..." title="Ninja Attack!" alt="Ninja Attack!">
  18. <img class="left half" src="data:;base64,..." width="150" height="150" title="Ninja Attack!" alt="Ninja in attack posture">
  19. [1] https://github.com/imathis/octopress/blob/master/plugins/image_tag.rb
  20. """
  21. import re
  22. import base64
  23. import urllib2
  24. from .mdx_liquid_tags import LiquidTags
  25. import six
  26. SYNTAX = '{% b64img [class name(s)] [http[s]:/]/path/to/image [width [height]] [title text | "title text" ["alt text"]] %}'
  27. # Regular expression to match the entire syntax
  28. ReImg = re.compile("""(?P<class>\S.*\s+)?(?P<src>(?:https?:\/\/|\/|\S+\/)\S+)(?:\s+(?P<width>\d+))?(?:\s+(?P<height>\d+))?(?P<title>\s+.+)?""")
  29. # Regular expression to split the title and alt text
  30. ReTitleAlt = re.compile("""(?:"|')(?P<title>[^"']+)?(?:"|')\s+(?:"|')(?P<alt>[^"']+)?(?:"|')""")
  31. def _get_file(src):
  32. """ Return content from local or remote file. """
  33. try:
  34. if '://' in src or src[0:2] == '//': # Most likely this is remote file
  35. response = urllib2.urlopen(src)
  36. return response.read()
  37. else:
  38. with open(src, 'rb') as fh:
  39. return fh.read()
  40. except Exception as e:
  41. raise RuntimeError('Error generating base64image: {}'.format(e))
  42. def base64image(src):
  43. """ Generate base64 encoded image from srouce file. """
  44. return base64.b64encode(_get_file(src))
  45. @LiquidTags.register('b64img')
  46. def b64img(preprocessor, tag, markup):
  47. attrs = None
  48. # Parse the markup string
  49. match = ReImg.search(markup)
  50. if match:
  51. attrs = dict([(key, val.strip())
  52. for (key, val) in six.iteritems(match.groupdict()) if val])
  53. else:
  54. raise ValueError('Error processing input. '
  55. 'Expected syntax: {0}'.format(SYNTAX))
  56. # Check if alt text is present -- if so, split it from title
  57. if 'title' in attrs:
  58. match = ReTitleAlt.search(attrs['title'])
  59. if match:
  60. attrs.update(match.groupdict())
  61. if not attrs.get('alt'):
  62. attrs['alt'] = attrs['title']
  63. attrs['src'] = 'data:;base64,{}'.format(base64image(attrs['src']))
  64. # Return the formatted text
  65. return "<img {0}>".format(' '.join('{0}="{1}"'.format(key, val)
  66. for (key, val) in six.iteritems(attrs)))
  67. #----------------------------------------------------------------------
  68. # This import allows image tag to be a Pelican plugin
  69. from .liquid_tags import register