From b998ad60e0ca0bb9fc97e85f73e65ecc22c42ce0 Mon Sep 17 00:00:00 2001 From: Duncan Lock Date: Tue, 23 Apr 2013 16:16:04 -0700 Subject: [PATCH] Updated to cope with multiple images and figures, and to deal with indivdual images. Also renamed and updated readme. Signed-off-by: Duncan Lock --- better_figures/__init__.py | 1 - better_figures/better_figures.py | 60 ----------------- better_figures_and_images/__init__.py | 1 + .../better_figures_and_images.py | 65 +++++++++++++++++++ .../readme.rst | 9 +-- 5 files changed, 71 insertions(+), 65 deletions(-) delete mode 100644 better_figures/__init__.py delete mode 100644 better_figures/better_figures.py create mode 100644 better_figures_and_images/__init__.py create mode 100644 better_figures_and_images/better_figures_and_images.py rename {better_figures => better_figures_and_images}/readme.rst (74%) diff --git a/better_figures/__init__.py b/better_figures/__init__.py deleted file mode 100644 index 5db85a7..0000000 --- a/better_figures/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .better_figures import * diff --git a/better_figures/better_figures.py b/better_figures/better_figures.py deleted file mode 100644 index e64dacd..0000000 --- a/better_figures/better_figures.py +++ /dev/null @@ -1,60 +0,0 @@ -""" -Better Figures -------- - -This plugin adds the width of the contained image to .figures. - - -
- map to buried treasure -

- This is the caption of the figure (a simple paragraph). -

-
- The legend consists of all elements after the caption. In this - case, the legend consists of this paragraph. -
-
- -""" - -import types -import os - -from pelican import signals - -from bs4 import BeautifulSoup -from PIL import Image - - -def initialized(pelican): - pass - - -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 - if 'figure' in content: - soup = BeautifulSoup(content) - fig = soup.find("div", {"class": "figure"}) - img = instance.settings['PATH'] + '/images/' + os.path.split(fig.img['src'])[1] - im = Image.open(img) - # if im.size[0] >= 500 and im.size[1] >= 500: - - style = 'width: {}px;'.format(im.size[0]) - fig['style'] = style - fig.img['style'] = style - - instance._content = soup.decode() - - -def register(): - signals.initialized.connect(initialized) - signals.content_object_init.connect(content_object_init) diff --git a/better_figures_and_images/__init__.py b/better_figures_and_images/__init__.py new file mode 100644 index 0000000..b8fefc4 --- /dev/null +++ b/better_figures_and_images/__init__.py @@ -0,0 +1 @@ +from .better_figures_and_images import * diff --git a/better_figures_and_images/better_figures_and_images.py b/better_figures_and_images/better_figures_and_images.py new file mode 100644 index 0000000..7e9f45f --- /dev/null +++ b/better_figures_and_images/better_figures_and_images.py @@ -0,0 +1,65 @@ +""" +Better Figures & Images +------- + +This plugin: + +- Adds a style="width: ???px;" to each image in the content +- Also adds the width of the contained image to any parent div.figures. + + +
+ map to buried treasure +

+ This is the caption of the figure (a simple paragraph). +

+
+ The legend consists of all elements after the caption. In this + case, the legend consists of this paragraph. +
+
+ +""" + +import types +import os + +from pelican import signals + +from bs4 import BeautifulSoup +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 'img' in content: + for img in soup('img'): + src = instance.settings['PATH'] + '/images/' + os.path.split(img['src'])[1] + im = Image.open(src) + extra_style = 'width: {}px;'.format(im.size[0]) + if img.get('style'): + img['style'] += extra_style + else: + img['style'] = extra_style + fig = img.find_parent('div', 'figure') + if fig: + if fig.get('style'): + fig['style'] += extra_style + else: + fig['style'] = extra_style + + instance._content = soup.decode() + + +def register(): + signals.content_object_init.connect(content_object_init) diff --git a/better_figures/readme.rst b/better_figures_and_images/readme.rst similarity index 74% rename from better_figures/readme.rst rename to better_figures_and_images/readme.rst index 9e07bc3..78270b5 100644 --- a/better_figures/readme.rst +++ b/better_figures_and_images/readme.rst @@ -1,9 +1,11 @@ Summary =========== -This plug-in finds any `div class="figures"` tags in the output, finds the image contained inside each one, -then checks the dimensions of the image file and adds the appropriate style="width: ???px;" to both the img tag -and it's containing div.figure tag. +This plug-in: + +- Adds a `style="width: ???px;"` attribute to any `` tags in the content, by checking +the dimensions of the image file and adding the appropriate style="width: ???px;" to the `` tag. +- Also finds any `div class="figures"` tags in the content, that contain images and adds the same style to them too. Assuming that the image is 250px wide, it turns output like this: @@ -32,4 +34,3 @@ into output like this: -TODO: Currently only does the first figure, not all of them