diff --git a/better_figures_and_images/better_figures_and_images.py b/better_figures_and_images/better_figures_and_images.py index 51f3376..186427b 100644 --- a/better_figures_and_images/better_figures_and_images.py +++ b/better_figures_and_images/better_figures_and_images.py @@ -13,13 +13,15 @@ TODO: Need to add a test.py for this plugin. """ -import os +from os import path, access, R_OK from pelican import signals from bs4 import BeautifulSoup from PIL import Image +import logging +logger = logging.getLogger(__name__) def content_object_init(instance): @@ -29,9 +31,30 @@ def content_object_init(instance): if 'img' in content: for img in soup('img'): - # TODO: Pretty sure this isn't the right way to do this, too hard coded. - # There must be a setting that I should be using? - src = instance.settings['PATH'] + '/images/' + os.path.split(img['src'])[1] + logger.debug('Better Fig. PATH: %s', instance.settings['PATH']) + logger.debug('Better Fig. img.src: %s', img['src']) + + img_path, img_filename = path.split(img['src']) + + logger.debug('Better Fig. img_path: %s', img_path) + logger.debug('Better Fig. img_fname: %s', img_filename) + + # Strip off {filename}, |filename| or /static + if img_path.startswith(('{filename}', '|filename|')): + img_path = img_path[10:] + elif img_path.startswith('/static'): + img_path = img_path[7:] + else: + logger.warning('Better Fig. Error: img_path should start with either {filename}, |filename| or /static') + + # Build the source image filename + src = instance.settings['PATH'] + img_path + '/' + img_filename + + logger.debug('Better Fig. src: %s', src) + if not (path.isfile(src) and access(src, R_OK)): + logger.error('Better Fig. Error: image not found: {}'.format(src)) + + # Open the source image and query dimensions; build style string im = Image.open(src) extra_style = 'width: {}px; height: auto;'.format(im.size[0])