Browse Source

do not die if image external url

fix broken path

use os.path.join
create src candidates and find valid one.
if1live 11 years ago
parent
commit
6d683802b3
1 changed files with 14 additions and 4 deletions
  1. 14 4
      better_figures_and_images/better_figures_and_images.py

+ 14 - 4
better_figures_and_images/better_figures_and_images.py

@@ -15,6 +15,7 @@ TODO: Need to add a test.py for this plugin.
 
 from __future__ import unicode_literals
 from os import path, access, R_OK
+import os
 
 from pelican import signals
 
@@ -51,12 +52,21 @@ def content_object_init(instance):
                 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
+                # search src path list
+                # 1. Build the source image filename from PATH
+                # 2. Build the source image filename from STATIC_PATHS
+                src = os.path.join(instance.settings['PATH'], img_path, img_filename)
+                src_candidates = [src]
+                src_candidates += [os.path.join(instance.settings['PATH'], static_path, img_path, img_filename) for static_path in instance.settings['STATIC_PATHS']]
+                src_candidates = [f for f in src_candidates if path.isfile(f) and access(f, R_OK)]
 
-                logger.debug('Better Fig. src: %s', src)
-                if not (path.isfile(src) and access(src, R_OK)):
+                if not src_candidates:
                     logger.error('Better Fig. Error: image not found: %s', src)
+                    logger.debug('Better Fig. Skip src: %s', img_path + '/' + img_filename)
+                    continue
+
+                src = src_candidates[0]
+                logger.debug('Better Fig. src: %s', src)
 
                 # Open the source image and query dimensions; build style string
                 im = Image.open(src)