Skip to content

Commit

Permalink
Merge branch 'feature/lead-improvements' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Fantomas42 committed Jan 15, 2015
2 parents 76b72a1 + abce668 commit 04860c7
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 16 deletions.
30 changes: 22 additions & 8 deletions zinnia/models_bases/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,27 @@ def html_content(self):
"""
Returns the "content" field formatted in HTML.
"""
if '</p>' in self.content:
return self.content
content = self.content
if not content:
return ''
elif '</p>' in content:
return content
elif MARKUP_LANGUAGE == 'markdown':
return markdown(self.content)
return markdown(content)
elif MARKUP_LANGUAGE == 'textile':
return textile(self.content)
return textile(content)
elif MARKUP_LANGUAGE == 'restructuredtext':
return restructuredtext(self.content)
return linebreaks(self.content)
return restructuredtext(content)
return linebreaks(content)

@property
def html_preview(self):
"""
Returns a preview of the "content" field formmated in HTML.
Returns a preview of the "content" field or
the "lead" field if defined, formatted in HTML.
"""
return HTMLPreview(self.html_content)
return HTMLPreview(self.html_content,
getattr(self, 'html_lead', ''))

@property
def word_count(self):
Expand Down Expand Up @@ -366,6 +371,15 @@ class LeadEntry(models.Model):
_('lead'), blank=True,
help_text=_('Lead paragraph'))

@property
def html_lead(self):
"""
Returns the "lead" field formatted in HTML.
"""
if self.lead:
return linebreaks(self.lead)
return ''

class Meta:
abstract = True

Expand Down
15 changes: 10 additions & 5 deletions zinnia/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ class HTMLPreview(object):
Build an HTML preview of an HTML content.
"""

def __init__(self, content,
def __init__(self, content, lead='',
splitters=PREVIEW_SPLITTERS,
max_words=PREVIEW_MAX_WORDS,
more_string=PREVIEW_MORE_STRING):
self._preview = None

self.lead = lead
self.content = content
self.splitters = splitters
self.max_words = max_words
Expand All @@ -45,7 +46,7 @@ def has_more(self):
"""
Boolean telling if the preview has hidden content.
"""
return self.preview != self.content
return bool(self.content and self.preview != self.content)

def __str__(self):
"""
Expand All @@ -57,10 +58,13 @@ def build_preview(self):
"""
Build the preview by:
- Returning the lead attribut if not empty.
- Checking if a split marker is present in the content
Then split the content with the marker to build the preview.
- Splitting the content to a fixed number of words.
"""
if self.lead:
return self.lead
for splitter in self.splitters:
if splitter in self.content:
return self.split(splitter)
Expand All @@ -87,17 +91,18 @@ def split(self, splitter):
@cached_property
def total_words(self):
"""
Return the total of words contained in the content.
Return the total of words contained
in the content and in the lead.
"""
return len(strip_tags(self.content).split())
return len(strip_tags('%s %s' % (self.lead, self.content)).split())

@cached_property
def displayed_words(self):
"""
Return the number of words displayed in the preview.
"""
return (len(strip_tags(self.preview).split()) -
len(self.more_string.split()))
(len(self.more_string.split()) * int(not bool(self.lead))))

@cached_property
def remaining_words(self):
Expand Down
3 changes: 3 additions & 0 deletions zinnia/static/zinnia/theme/css/screen.css
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ table {
text-align: justify;
font-size: 1.2em;
font-weight: bold; }
.zinnia .entry-summary p:first-of-type:first-letter {
font-size: 1.4em;
line-height: 0; }
.zinnia .entry-content {
text-align: justify; }
.zinnia .entry-content p {
Expand Down
4 changes: 4 additions & 0 deletions zinnia/static/zinnia/theme/sass/partials/_entry.scss
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ h2.entry-title {
text-align: justify;
font-size: 1.2em;
font-weight: bold;
&:first-of-type:first-letter {
font-size: 1.4em;
line-height: 0;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion zinnia/templates/feeds/entry_description.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{{ obj.lead|linebreaks }}
{{ obj.html_lead|safe }}
{{ obj.html_content|safe }}
4 changes: 2 additions & 2 deletions zinnia/templates/zinnia/_entry_detail_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ <h2 class="entry-title">
{% endblock entry-image %}

{% block entry-lead %}
{% if object.lead %}
{% if not continue_reading and object.lead %}
<div class="entry-summary">
{{ object.lead|linebreaks }}
{{ object.html_lead|safe }}
</div>
{% endif %}
{% endblock entry-lead %}
Expand Down
12 changes: 12 additions & 0 deletions zinnia/tests/test_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ def test_save_excerpt(self):
self.assertEqual(self.entry.excerpt,
' '.join(['word-%s' % i for i in range(50)]) + '...')

def test_html_lead(self):
self.assertEquals(self.entry.html_lead, '')
self.entry.lead = 'Lead paragraph'
self.assertEquals(self.entry.html_lead,
'<p>Lead paragraph</p>')


class EntryHtmlContentTestCase(TestCase):

Expand All @@ -379,6 +385,8 @@ def test_html_content_default(self):
' this is my content'
self.assertEqual(self.entry.html_content,
'<p>Hello world !<br /> this is my content</p>')
self.entry.content = ''
self.assertEqual(self.entry.html_content, '')

@skipUnless(is_lib_available('textile'), 'Textile is not available')
def test_html_content_textitle(self):
Expand Down Expand Up @@ -424,6 +432,10 @@ def test_html_preview(self):
preview = self.entry.html_preview
self.assertEqual(str(preview), '<p>My content</p>')
self.assertEqual(preview.has_more, False)
self.entry.lead = 'Lead paragraph'
preview = self.entry.html_preview
self.assertEqual(str(preview), '<p>Lead paragraph</p>')
self.assertEqual(preview.has_more, True)


class EntryAbsoluteUrlTestCase(TestCase):
Expand Down
29 changes: 29 additions & 0 deletions zinnia/tests/test_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def test_has_more_with_long_more_text(self):
self.assertEqual(str(preview), '<p>Hello the .........</p>')
self.assertEqual(preview.has_more, True)

def test_has_more_with_lead(self):
text = '<p>Hello the World</p>'
lead = '<p>Lead paragraph</p>'
preview = HTMLPreview(text, lead)
self.assertEqual(str(preview), '<p>Lead paragraph</p>')
self.assertEqual(preview.has_more, True)
preview = HTMLPreview('', lead)
self.assertEqual(str(preview), '<p>Lead paragraph</p>')
self.assertEqual(preview.has_more, False)

def test_str_non_ascii_issue_314(self):
text = '<p>тест non ascii</p>'
preview = HTMLPreview(text, splitters=[],
Expand All @@ -54,3 +64,22 @@ def test_metrics(self):
self.assertEqual(preview.remaining_words, 2)
self.assertEqual(preview.displayed_percent, 50.0)
self.assertEqual(preview.remaining_percent, 50.0)

def test_metrics_with_lead(self):
text = '<p>Hello World</p> <p>Hello dude</p>'
lead = '<p>Lead paragraph</p>'
preview = HTMLPreview(text, lead, splitters=[],
max_words=2, more_string=' ...')
self.assertEqual(preview.total_words, 6)
self.assertEqual(preview.displayed_words, 2)
self.assertEqual(preview.remaining_words, 4)
self.assertEqual('%.2f' % preview.displayed_percent, '33.33')
self.assertEqual('%.2f' % preview.remaining_percent, '66.67')

def test_empty_text(self):
preview = HTMLPreview('')
self.assertEqual(str(preview), '')
self.assertEqual(preview.has_more, False)
preview = HTMLPreview('', '')
self.assertEqual(str(preview), '')
self.assertEqual(preview.has_more, False)

0 comments on commit 04860c7

Please sign in to comment.