Procházet zdrojové kódy

Merge pull request #218 from jkrshw/feature/custom-image

[representative_image] Add Image metadata attribute for article to specify a custom image
Justin Mayer před 10 roky
rodič
revize
75497649b0

+ 19 - 4
representative_image/Readme.md

@@ -1,8 +1,8 @@
 # Summary
 
-This plugin extracts a representative image (i.e, featured image) from the article's summary or article's content. The image can be access at `article.featured_image`. 
+This plugin extracts a representative image (i.e, featured image) from the article's summary or content if not specifed in the metadata. 
 
-The plugin also remove any images from the summary after extraction to avoid duplication. 
+The plugin also removes any images from the summary after extraction to avoid duplication. 
 
 It allows the flexibility on where and how to display the featured image of an article together with its summary in a template page. For example, the article metadata can be displayed in thumbnail format, in which there is a short summary and an image. The layout of the summary and the image can be varied for aesthetical purpose. It doesn't have to depend on article's content format. 
 
@@ -23,8 +23,23 @@ To enable, add the following to your settings.py:
 Usage
 -----
 
-To include a representative image add the following to the template
-	
+To override the default behaviour of selecting the first image in the article's summary or content, set the image property the article's metadata to the url of the image to display, e.g:
+
+```markdown
+Title: My super title
+Date: 2010-12-03 10:20
+Category: Python
+Tags: pelican, publishing
+Slug: my-super-post
+Author: Alexis Metaireau
+Summary: Short version for index and feeds
+Image: /images/my-super-image.png
+
+Article content...
+```
+
+To include a representative image in a page add the following to the template:
+
     {% if  article.featured_image %}
         <img src="{{ article.featured_image }}">
     {% endif %}

+ 3 - 0
representative_image/representative_image.py

@@ -5,6 +5,9 @@ from bs4 import BeautifulSoup
 def images_extraction(instance):
     representativeImage = None
     if type(instance) == Article:
+        if 'image' in instance.metadata:
+            representativeImage = instance.metadata['image']
+
         # Process Summary:
         # If summary contains images, extract one to be the representativeImage and remove images from summary
         soup = BeautifulSoup(instance.summary, 'html.parser')

+ 14 - 0
representative_image/test_representative_image.py

@@ -9,6 +9,7 @@ TEST_CONTENT = str(generate_lorem_ipsum(n=3, html=True)) + '<img src="' + TEST_C
 TEST_SUMMARY_IMAGE_URL = 'https://testimage.com/summary.jpg'
 TEST_SUMMARY_WITHOUTIMAGE = str(generate_lorem_ipsum(n=1, html=True))
 TEST_SUMMARY_WITHIMAGE = TEST_SUMMARY_WITHOUTIMAGE + '<img src="' + TEST_SUMMARY_IMAGE_URL + '"/>'
+TEST_CUSTOM_IMAGE_URL = 'https://testimage.com/custom.jpg' 
 
 
 from pelican.contents import Article
@@ -43,6 +44,19 @@ class TestRepresentativeImage(unittest.TestCase):
         self.assertEqual(article.featured_image, TEST_SUMMARY_IMAGE_URL)
         self.assertEqual(article.summary, TEST_SUMMARY_WITHOUTIMAGE)
 
+    def test_extract_image_from_summary_with_custom_image(self):
+        args = {
+            'content': TEST_CONTENT,
+            'metadata': {
+                'summary': TEST_SUMMARY_WITHIMAGE,
+                'image': TEST_CUSTOM_IMAGE_URL,
+            },
+        }
+
+        article = Article(**args)
+        self.assertEqual(article.featured_image, TEST_CUSTOM_IMAGE_URL)
+        self.assertEqual(article.summary, TEST_SUMMARY_WITHOUTIMAGE)
+
 if __name__ == '__main__':
     unittest.main()