Updated to cope with multiple images and figures, and to deal with indivdual images.
Also renamed and updated readme. Signed-off-by: Duncan Lock <duncan.lock@gmail.com>
This commit is contained in:
@@ -1 +0,0 @@
|
||||
from .better_figures import *
|
||||
@@ -1,60 +0,0 @@
|
||||
"""
|
||||
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)
|
||||
1
better_figures_and_images/__init__.py
Normal file
1
better_figures_and_images/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .better_figures_and_images import *
|
||||
65
better_figures_and_images/better_figures_and_images.py
Normal file
65
better_figures_and_images/better_figures_and_images.py
Normal file
@@ -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.
|
||||
|
||||
|
||||
<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 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)
|
||||
@@ -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 `<img>` tags in the content, by checking
|
||||
the dimensions of the image file and adding the appropriate style="width: ???px;" to the `<img>` 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:
|
||||
</div>
|
||||
</div>
|
||||
|
||||
TODO: Currently only does the first figure, not all of them
|
||||
Reference in New Issue
Block a user