From f3549dd296fea1232418a352b0a553407c218b1a Mon Sep 17 00:00:00 2001 From: Alistair Magee Date: Thu, 30 Jan 2014 22:38:04 +0000 Subject: [PATCH] plugin to specify maxmium number of images to appear in summary --- clean_summary/README.md | 34 ++++++++++++++++++++++++++++++ clean_summary/__init__.py | 1 + clean_summary/clean_summary.py | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 clean_summary/README.md create mode 100644 clean_summary/__init__.py create mode 100644 clean_summary/clean_summary.py diff --git a/clean_summary/README.md b/clean_summary/README.md new file mode 100644 index 0000000..d7572b4 --- /dev/null +++ b/clean_summary/README.md @@ -0,0 +1,34 @@ +#Clean Summary Plugin# + +Plugin to clean your summary of excess images. Images can take up much more +space than text and lead to summaries being different sizes on archive and +index pages. With this plugin you can specify a maximum number of images that +will appear in your summaries. + +There is also an option to include a minimum of one image. + +##Settings## + +plugin has two settings. `CLEAN_SUMMARY_MAXIMUM` which takes an int, and +`CLEAN_SUMMARY_MINIMUM_ONE` which takes a boolean. They deafult to 0 and False. + +`CLEAN_SUMMARY_MAXIMUM` sets the maximum number of images that will appear in +your summary. + +if `CLEAN_SUMMARY_MINIMUM_ONE` is set to `True` and your summary doesn't already +contain an image, the plugin will add the first image in your article(if one +exists) to the beginning of the summary. + +##Requirements## + +requires Beautiful Soup + + pip install BeautifulSoup4 + + +##Usage with Summary Plugin## + +if using the summary plugin, make sure summary appears in your plugins before +clean summary. Eg. + + PLUGINS = ['summary', 'clean_summary', ... ] diff --git a/clean_summary/__init__.py b/clean_summary/__init__.py new file mode 100644 index 0000000..1b96611 --- /dev/null +++ b/clean_summary/__init__.py @@ -0,0 +1 @@ +from .clean_summary import * diff --git a/clean_summary/clean_summary.py b/clean_summary/clean_summary.py new file mode 100644 index 0000000..c09702d --- /dev/null +++ b/clean_summary/clean_summary.py @@ -0,0 +1,38 @@ +""" +Clean Summary +------------- + +adds option to specify maximum number of images to appear in article summary +also adds option to include an image by default if one exists in your article +""" + +from pelican import signals +from pelican.contents import Content, Article +from bs4 import BeautifulSoup +from six import text_type + +def clean_summary(instance): + if "CLEAN_SUMMARY_MAXIMUM" in instance.settings: + maximum_images = instance.settings["CLEAN_SUMMARY_MAXIMUM"] + else: + maximum_images = 0 + if "CLEAN_SUMMARY_MINIMUM_ONE" in instance.settings: + minimum_one = instance.settings['CLEAN_SUMMARY_MINIMUM_ONE'] + else: + minimum_one = False + if type(instance) == Article: + summary = instance.summary + summary = BeautifulSoup(instance.summary, 'html.parser') + images = summary.findAll('img') + if (len(images) > maximum_images): + for image in images[maximum_images:]: + image.extract() + if len(images) < 1 and minimum_one: #try to find one + content = BeautifulSoup(instance.content, 'html.parser') + first_image = content.find('img') + if first_image: + summary.insert(0, first_image) + instance._summary = text_type(summary) + +def register(): + signals.content_object_init.connect(clean_summary)