diff --git a/better_figures/__init__.py b/better_figures/__init__.py new file mode 100644 index 0000000..5db85a7 --- /dev/null +++ b/better_figures/__init__.py @@ -0,0 +1 @@ +from .better_figures import * diff --git a/better_figures/better_figures.py b/better_figures/better_figures.py new file mode 100644 index 0000000..e64dacd --- /dev/null +++ b/better_figures/better_figures.py @@ -0,0 +1,60 @@ +""" +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/readme.rst b/better_figures/readme.rst new file mode 100644 index 0000000..9e07bc3 --- /dev/null +++ b/better_figures/readme.rst @@ -0,0 +1,35 @@ +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. + + +Assuming that the image is 250px wide, it turns output like this: + +
+ map to buried treasure +

+ This is the caption of the figure. +

+
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod + tempor incididunt ut labore et dolore magna aliqua. +
+
+ +into output like this: + +
+ map to buried treasure +

+ This is the caption of the figure. +

+
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod + tempor incididunt ut labore et dolore magna aliqua. +
+
+ +TODO: Currently only does the first figure, not all of them