Merge pull request #247 from saimn/summary

Summary - Fix for #245
This commit is contained in:
Alexis Metaireau 2012-03-15 14:15:01 -07:00
commit 4714276e2c
2 changed files with 23 additions and 8 deletions

View file

@ -89,9 +89,9 @@ class Page(object):
if hasattr(self, 'date') and self.date > datetime.now():
self.status = 'draft'
# set summary
if not hasattr(self, 'summary'):
self.summary = truncate_html_words(self.content, 50)
# store the :summary: metadata if it is set
if 'summary' in metadata:
self._summary = metadata['summary']
def check_properties(self):
"""test that each mandatory property is set."""
@ -126,8 +126,12 @@ class Page(object):
return content
def _get_summary(self):
"""Returns the summary of an article, based on to the content"""
return truncate_html_words(self.content, 50)
"""Returns the summary of an article, based on the :summary: metadata
if it is set, else troncate the content."""
if hasattr(self, '_summary'):
return self._summary
else:
return truncate_html_words(self.content, 50)
def _set_summary(self, summary):
"""Dummy function"""

View file

@ -8,14 +8,20 @@ except ImportError, e:
from pelican.contents import Page
from pelican.settings import _DEFAULT_CONFIG
from jinja2.utils import generate_lorem_ipsum
# generate one paragraph, enclosed with <p>
TEST_CONTENT = str(generate_lorem_ipsum(n=1))
TEST_SUMMARY = generate_lorem_ipsum(n=1, html=False)
class TestPage(TestCase):
def setUp(self):
super(TestPage, self).setUp()
self.page_kwargs = {
'content': 'content',
'content': TEST_CONTENT,
'metadata': {
'summary': TEST_SUMMARY,
'title': 'foo bar',
'author': 'Blogger',
},
@ -27,11 +33,11 @@ class TestPage(TestCase):
"""
metadata = {'foo': 'bar', 'foobar': 'baz', 'title': 'foobar', }
page = Page('content', metadata=metadata)
page = Page(TEST_CONTENT, metadata=metadata)
for key, value in metadata.items():
self.assertTrue(hasattr(page, key))
self.assertEqual(value, getattr(page, key))
self.assertEqual(page.content, 'content')
self.assertEqual(page.content, TEST_CONTENT)
def test_mandatory_properties(self):
"""If the title is not set, must throw an exception."""
@ -39,6 +45,11 @@ class TestPage(TestCase):
page = Page(**self.page_kwargs)
page.check_properties()
def test_summary_from_metadata(self):
"""If a :summary: metadata is given, it should be used."""
page = Page(**self.page_kwargs)
self.assertEqual(page.summary, TEST_SUMMARY)
def test_slug(self):
"""If a title is given, it should be used to generate the slug."""
page = Page(**self.page_kwargs)