better_figures_and_images.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """
  2. Better Figures & Images
  3. ------------------------
  4. This plugin:
  5. - Adds a style="width: ???px; height: auto;" to each image in the content
  6. - Also adds the width of the contained image to any parent div.figures.
  7. - If RESPONSIVE_IMAGES == True, also adds style="max-width: 100%;"
  8. - Corrects alt text: if alt == image filename, set alt = ''
  9. TODO: Need to add a test.py for this plugin.
  10. """
  11. from __future__ import unicode_literals
  12. from os import path, access, R_OK
  13. from pelican import signals
  14. from bs4 import BeautifulSoup
  15. from PIL import Image
  16. import logging
  17. logger = logging.getLogger(__name__)
  18. def content_object_init(instance):
  19. if instance._content is not None:
  20. content = instance._content
  21. soup = BeautifulSoup(content, 'html.parser')
  22. if 'img' in content:
  23. for img in soup('img'):
  24. logger.debug('Better Fig. PATH: %s', instance.settings['PATH'])
  25. logger.debug('Better Fig. img.src: %s', img['src'])
  26. img_path, img_filename = path.split(img['src'])
  27. logger.debug('Better Fig. img_path: %s', img_path)
  28. logger.debug('Better Fig. img_fname: %s', img_filename)
  29. # Strip off {filename}, |filename| or /static
  30. if img_path.startswith(('{filename}', '|filename|')):
  31. img_path = img_path[10:]
  32. elif img_path.startswith('/static'):
  33. img_path = img_path[7:]
  34. elif img_path.startswith('data:image'):
  35. # Image is encoded in-line (not a file).
  36. continue
  37. else:
  38. logger.warning('Better Fig. Error: img_path should start with either {filename}, |filename| or /static')
  39. # Build the source image filename
  40. src = instance.settings['PATH'] + img_path + '/' + img_filename
  41. logger.debug('Better Fig. src: %s', src)
  42. if not (path.isfile(src) and access(src, R_OK)):
  43. logger.error('Better Fig. Error: image not found: %s', src)
  44. # Open the source image and query dimensions; build style string
  45. im = Image.open(src)
  46. extra_style = 'width: {}px; height: auto;'.format(im.size[0])
  47. if 'RESPONSIVE_IMAGES' in instance.settings and instance.settings['RESPONSIVE_IMAGES']:
  48. extra_style += ' max-width: 100%;'
  49. if img.get('style'):
  50. img['style'] += extra_style
  51. else:
  52. img['style'] = extra_style
  53. if img['alt'] == img['src']:
  54. img['alt'] = ''
  55. fig = img.find_parent('div', 'figure')
  56. if fig:
  57. if fig.get('style'):
  58. fig['style'] += extra_style
  59. else:
  60. fig['style'] = extra_style
  61. instance._content = soup.decode()
  62. def register():
  63. signals.content_object_init.connect(content_object_init)