Exemplo n.º 1
0
def truncate(content, max_length=DEFAULT_TRUNCATE_LENGTH, allowed_tags=ALLOWED_TAGS, full_link=None):
    """ truncate a body of text to the expected 'max_length' and strip
        the body of text of all html tags that are not in 'allowed tags'. You
        can also specify a 'strip' value (True -> strip html tags, False ->
        escape html tags and leave them in text)
    """
    if not content:
        return ''

    cleaner = Cleaner(
        page_structure=False,
        links=True,
        safe_attrs_only=True,
        remove_unknown_tags=False,
        allow_tags=allowed_tags
    )

    content = defaultfilters.truncatechars_html(cleaner.clean_html(content), max_length)
    if full_link:
        try:
            insert_point = content.rindex('</p>')
        except ValueError:
            insert_point = content.rindex('<')

        ending = content[insert_point:]
        content = content[:insert_point]

        content += '&nbsp;<a href="' + full_link + '">(Read More)</a>' + ending
    return content
Exemplo n.º 2
0
def truncatechars_content(content):
    """
    获得文章的摘要
    :param content:
    :return:
    """
    from django.template.defaultfilters import truncatechars_html
    return truncatechars_html(content,settings.ARTICLE_SUB_LENGTH)
Exemplo n.º 3
0
def truncatechars_content(content):
    """
    获得文章内容的摘要
    :param content:
    :return:
    """
    from django.template.defaultfilters import truncatechars_html
    from DjangoBlog.utils import get_blog_setting
    blogsetting = get_blog_setting()
    return truncatechars_html(content, blogsetting.article_sub_length)
Exemplo n.º 4
0
 def format_result(self, request, item):
     result = dict()
     result['pk'] = item.pk
     result['name'] = item.name
     result['creation_date'] = localize(item.creation_date)
     result['resting_place'] = item.resting_place
     if item.rating:
         result['rating'] = range(item.rating)
     if item.photo:
         result['photo'] = item.photo.url
     result['evaluation'] = striptags(truncatechars_html(
         item.general_evaluation, 100))
     return result
Exemplo n.º 5
0
 def test_truncatechars_html(self):
     self.assertEqual(truncatechars_html(
         '<p>one <a href="#">two - three <br>four</a> five</p>', 0), '...')
     self.assertEqual(truncatechars_html('<p>one <a href="#">two - '
         'three <br>four</a> five</p>', 6),
         '<p>one...</p>')
     self.assertEqual(truncatechars_html(
         '<p>one <a href="#">two - three <br>four</a> five</p>', 11),
         '<p>one <a href="#">two ...</a></p>')
     self.assertEqual(truncatechars_html(
         '<p>one <a href="#">two - three <br>four</a> five</p>', 100),
         '<p>one <a href="#">two - three <br>four</a> five</p>')
     self.assertEqual(truncatechars_html(
         '<b>\xc5ngstr\xf6m</b> was here', 5), '<b>\xc5n...</b>')
     self.assertEqual(truncatechars_html(
         'a<b>b</b>c', 3), 'a<b>b</b>c')
Exemplo n.º 6
0
 def description_from_content(self):
     """
     Returns the first block or sentence of the first content-like
     field.
     """
     description = ""
     # Use the first RichTextField, or TextField if none found.
     for field_type in (RichTextField, models.TextField):
         if not description:
             for field in self._meta.fields:
                 if (isinstance(field, field_type) and
                         field.name != "description"):
                     description = getattr(self, field.name)
                     if description:
                         from mezzanine.core.templatetags.mezzanine_tags \
                                                 import richtext_filters
                         description = richtext_filters(description)
                         break
     # Fall back to the title if description couldn't be determined.
     if not description:
         description = str(self)
     # Strip everything after the first block or sentence.
     '''
     ends = ("</p>", "<br />", "<br/>", "<br>", "</ul>",
             "\n", ". ", "! ", "? ")
     for end in ends:
         pos = description.lower().find(end)
         if pos > -1:
             description = TagCloser(description[:pos]).html
             break
     else:
         description = truncatewords_html(description, 100)
     try:
         description = unicode(description)
     except NameError:
         pass  # Python 3.
     '''
     #TODO make it configurable
     description = truncatechars_html(description, 260)
     return description
 def test_invalid_arg(self):
     html = '<p>one <a href="#">two - three <br>four</a> five</p>'
     self.assertEqual(truncatechars_html(html, 'a'), html)
 def test_invalid_arg(self):
     html = '<p>one <a href="#">two - three <br>four</a> five</p>'
     self.assertEqual(truncatechars_html(html, 'a'), html)
Exemplo n.º 9
0
 def resumen(self):
     if self.summary:
         return (striptags(self.summary))
     else:
         return (striptags(truncatechars_html(self.body_html, 250)))
Exemplo n.º 10
0
 def test_truncate_zero(self):
     self.assertEqual(truncatechars_html('<p>one <a href="#">two - three <br>four</a> five</p>', 0), '...')
Exemplo n.º 11
0
 def get_truncated_html(self, instance: 'Quote') -> str:
     """Return truncated HTML content"""
     if instance.bite is not None:
         return truncatechars_html(instance.bite, 100)
     else:
         return truncatechars_html(instance.text, 100)
Exemplo n.º 12
0
 def get_meta_abstract(self):
     return truncatechars_html(self.abstract, 180)
Exemplo n.º 13
0
 def test_truncate_unicode(self):
     self.assertEqual(truncatechars_html('<b>\xc5ngstr\xf6m</b> was here', 5), '<b>\xc5n...</b>')
Exemplo n.º 14
0
 def test_truncate_something(self):
     self.assertEqual(truncatechars_html('a<b>b</b>c', 3), 'a<b>b</b>c')
Exemplo n.º 15
0
 def test_truncate_zero(self):
     self.assertEqual(truncatechars_html('<p>one <a href="#">two - three <br>four</a> five</p>', 0), '...')
Exemplo n.º 16
0
 def test_truncate_unicode(self):
     self.assertEqual(truncatechars_html('<b>\xc5ngstr\xf6m</b> was here', 5), '<b>\xc5n...</b>')
Exemplo n.º 17
0
 def test_truncate2(self):
     self.assertEqual(
         truncatechars_html('<p>one <a href="#">two - three <br>four</a> five</p>', 11),
         '<p>one <a href="#">two ...</a></p>',
     )
Exemplo n.º 18
0
def summarize_html(text, length):
    filtered = clean_html(text, with_media=False)

    return truncatechars_html(filtered, length)
Exemplo n.º 19
0
def truncate_content(content, length=300):
    return truncatechars_html(content, length)
Exemplo n.º 20
0
 def get_introtext(self):
     return truncatechars_html(self.introtext, 80)
Exemplo n.º 21
0
 def test_truncate2(self):
     self.assertEqual(
         truncatechars_html('<p>one <a href="#">two - three <br>four</a> five</p>', 11),
         '<p>one <a href="#">two ...</a></p>',
     )
Exemplo n.º 22
0
 def item_description(self, item):
     return truncatechars_html(item.text, 20)
Exemplo n.º 23
0
 def test_truncate_something(self):
     self.assertEqual(truncatechars_html('a<b>b</b>c', 3), 'a<b>b</b>c')
Exemplo n.º 24
0
def truncatechars_content(content):
    from django.template.defaultfilters import truncatechars_html
    from blog.context_processors import seo_processor
    blogsetting = seo_processor(request)
    return truncatechars_html(content, blogsetting['ARTICLE_SUB_LENGTH'])
Exemplo n.º 25
0
 def get_descr(self, obj):
     if obj.description:
         return truncatechars_html(obj.description, 150)
Exemplo n.º 26
0
 def get_bite(self, instance: 'Quote') -> str:
     """Return the user-facing bite HTML."""
     # "bite" is set to truncated text if it does not exist
     # TODO: Add "truncated" field to model to distinguish true bites from auto bites
     return (instance.bite.html if instance.bite else truncatechars_html(
         instance.text, 100))
Exemplo n.º 27
0
def truncatechars_content(content):
    from django.template.defaultfilters import truncatechars_html

    return truncatechars_html(content, 50)
Exemplo n.º 28
0
 def get_content(self, announcement):
     return truncatechars_html(announcement.content, 200)