Added initial stab at figures and image widths plugin.
Signed-off-by: Duncan Lock <duncan.lock@gmail.com>
This commit is contained in:
1
better_figures/__init__.py
Normal file
1
better_figures/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .better_figures import *
|
||||
60
better_figures/better_figures.py
Normal file
60
better_figures/better_figures.py
Normal file
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
Better Figures
|
||||
-------
|
||||
|
||||
This plugin adds the width of the contained image to .figures.
|
||||
|
||||
|
||||
<div class="figure">
|
||||
<img alt="map to buried treasure" src="/static/images/dunc_smiling_192x192.jpg" />
|
||||
<p class="caption">
|
||||
This is the caption of the figure (a simple paragraph).
|
||||
</p>
|
||||
<div class="legend">
|
||||
The legend consists of all elements after the caption. In this
|
||||
case, the legend consists of this paragraph.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
"""
|
||||
|
||||
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)
|
||||
35
better_figures/readme.rst
Normal file
35
better_figures/readme.rst
Normal file
@@ -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:
|
||||
|
||||
<div class="figure" style="width: 250px;">
|
||||
<img style="width: 250px;" alt="map to buried treasure" src="/static/images/image.jpg" />
|
||||
<p class="caption">
|
||||
This is the caption of the figure.
|
||||
</p>
|
||||
<div class="legend">
|
||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
|
||||
tempor incididunt ut labore et dolore magna aliqua.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
into output like this:
|
||||
|
||||
<div class="figure" style="width: 250px;">
|
||||
<img style="width: 250px;" alt="map to buried treasure" src="/static/images/image.jpg" />
|
||||
<p class="caption">
|
||||
This is the caption of the figure.
|
||||
</p>
|
||||
<div class="legend">
|
||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
|
||||
tempor incididunt ut labore et dolore magna aliqua.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
TODO: Currently only does the first figure, not all of them
|
||||
Reference in New Issue
Block a user