Quellcode durchsuchen

Added suuport for responsive images; added initial support for SVG

Signed-off-by: Duncan Lock <duncan.lock@gmail.com>
Duncan Lock vor 12 Jahren
Ursprung
Commit
c99a2102f8
1 geänderte Dateien mit 19 neuen und 12 gelöschten Zeilen
  1. 19 12
      better_figures_and_images/better_figures_and_images.py

+ 19 - 12
better_figures_and_images/better_figures_and_images.py

@@ -11,7 +11,6 @@ TODO: Need to add a test.py for this plugin.
 
 """
 
-import types
 import os
 
 from pelican import signals
@@ -22,23 +21,31 @@ from PIL import Image
 
 def content_object_init(instance):
 
-    def _get_content(self):
-        content = self._content
-        return content
-
-    instance._get_content = types.MethodType(_get_content, instance)
-
     if instance._content is not None:
         content = instance._content
         soup = BeautifulSoup(content)
 
+        # if '<object type="image/svg+xml"' in content:
+        #     for obj in soup('object'):
+        #         extra_style = 'max-width: 100%;'
+        #         fig = obj.find_parent('div', 'figure')
+        #         if fig:
+        #             if obj.get('style'):
+        #                 obj['style'] += extra_style
+        #             else:
+        #                 obj['style'] = extra_style
+
         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]
-                im = Image.open(src)
-                extra_style = 'width: {}px; height: auto;'.format(im.size[0])
+                if instance.settings['RESPONSIVE_IMAGES']:
+                    extra_style = 'max-width: 100%;'
+                else:
+                    # 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]
+                    im = Image.open(src)
+                    extra_style = 'width: {}px; height: auto;'.format(im.size[0])
+
                 if img.get('style'):
                     img['style'] += extra_style
                 else: