Skip to content

Commit

Permalink
1.fix bug for long word
Browse files Browse the repository at this point in the history
2.show summary in articles list page
  • Loading branch information
WUJISHANXIA committed Oct 11, 2017
1 parent 3567175 commit dd4bdba
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
43 changes: 42 additions & 1 deletion bootcamp/articles/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals

from HTMLParser import HTMLParser
from django.contrib.auth.models import User
from django.db import models
from autoslug import AutoSlugField
Expand Down Expand Up @@ -40,6 +41,7 @@ def __str__(self):
return self.title

def get_content_as_markdown(self):
self.content = self.insert_space_to_long_word(self.content)
return markdown.markdown(self.content, safe_mode='escape')

@staticmethod
Expand Down Expand Up @@ -69,11 +71,33 @@ def get_summary(self):
return self.content

def get_summary_as_markdown(self):
return markdown.markdown(self.get_summary(), safe_mode='escape')
markdown_html = markdown.markdown(self.get_summary(), safe_mode='escape')
summary_parser = SummaryParser()
summary_parser.feed(markdown_html)
summary = self.insert_space_to_long_word(summary_parser.summary)
return summary

def get_comments(self):
return ArticleComment.objects.filter(article=self)

@staticmethod
def insert_space_to_long_word(content):
count = 0
index_list = []
for index, character in enumerate(content):
if not character.isspace():
count += 1
else:
count = 0
if count >= 50:
index_list.append(index)
count = 0
content_list = list(content)
for index in index_list:
content_list.insert(index, ' ')
content = ''.join(content_list)
return content


@python_2_unicode_compatible
class ArticleComment(models.Model):
Expand All @@ -92,3 +116,20 @@ def __str__(self):

def get_comment_as_markdown(self):
return markdown.markdown(self.comment, safe_mode='escape')


class SummaryParser(HTMLParser):
"""Get a summary of the HTML-format document"""

def __init__(self):
HTMLParser.__init__(self)
self.summary = ''

def feed(self, data):
HTMLParser.feed(self, data)

def handle_data(self, data):
if self.summary:
self.summary = ' '.join([self.summary, data])
else:
self.summary = data
2 changes: 1 addition & 1 deletion bootcamp/articles/templates/articles/articles.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ <h1>{% trans 'Articles' %}</h1>
<div class="col-md-10">
<section class="articles">
{% for article in articles %}
{% include 'articles/partial_article.html' with article=article %}
{% include 'articles/partial_article.html' with article=article is_articles=forloop %}
{% empty %}
<h4 class="no-data">{% trans 'There is no published article yet' %}. <a href="{% url 'write' %}">{% trans 'Be the first one to publish' %}!</a></h4>
{% endfor %}
Expand Down
6 changes: 5 additions & 1 deletion bootcamp/articles/templates/articles/partial_article.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ <h2><a href="{% url 'article' article.slug %}">{{ article.title }}</a></h2>
</span>
</div>
<div class="content">
{{ article.get_content_as_markdown|safe }}
{% if is_articles %}
{{ article.get_summary_as_markdown|safe }}
{% else %}
{{ article.get_content_as_markdown|safe }}
{% endif %}
</div>
{% if article.tags.names %}
<div class="tags">
Expand Down
8 changes: 7 additions & 1 deletion bootcamp/articles/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def setUp(self):
everybody always wants the real deal.''',
create_user=self.user,
)
self.long_word_content = ('thisisalonglonglonglonglonglonglonglongword'
'longlonglonglonglonglonglonglonglonglongword')
self.normal_content = ('This is a really long long long long long long '
'long long long long long long long long content')

def test_object_instance(self):
self.assertTrue(isinstance(self.article, Article))
Expand All @@ -58,9 +62,11 @@ def test_return_values(self):
self.assertEqual(self.article.get_summary(), self.article.content)
self.assertEqual(len(self.not_p_article.get_summary()), 258)
self.assertEqual(self.article.get_summary_as_markdown(),
'<p>This is a really good content</p>')
'This is a really good content')
self.assertTrue(self.article_comment in self.article.get_comments())
self.assertEqual(str(self.article_comment),
'test_user - A really nice title')
self.assertEqual(self.article_comment.get_comment_as_markdown(),
'<p>A really nice comment</p>')
self.assertEqual(self.article.insert_space_to_long_word(self.normal_content),self.normal_content)
self.assertTrue(' ' in self.article.insert_space_to_long_word(self.long_word_content))

0 comments on commit dd4bdba

Please sign in to comment.