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 += ' <a href="' + full_link + '">(Read More)</a>' + ending return content
def truncatechars_content(content): """ 获得文章的摘要 :param content: :return: """ from django.template.defaultfilters import truncatechars_html return truncatechars_html(content,settings.ARTICLE_SUB_LENGTH)
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)
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
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')
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 resumen(self): if self.summary: return (striptags(self.summary)) else: return (striptags(truncatechars_html(self.body_html, 250)))
def test_truncate_zero(self): self.assertEqual(truncatechars_html('<p>one <a href="#">two - three <br>four</a> five</p>', 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)
def get_meta_abstract(self): return truncatechars_html(self.abstract, 180)
def test_truncate_unicode(self): self.assertEqual(truncatechars_html('<b>\xc5ngstr\xf6m</b> was here', 5), '<b>\xc5n...</b>')
def test_truncate_something(self): self.assertEqual(truncatechars_html('a<b>b</b>c', 3), 'a<b>b</b>c')
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>', )
def summarize_html(text, length): filtered = clean_html(text, with_media=False) return truncatechars_html(filtered, length)
def truncate_content(content, length=300): return truncatechars_html(content, length)
def get_introtext(self): return truncatechars_html(self.introtext, 80)
def item_description(self, item): return truncatechars_html(item.text, 20)
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'])
def get_descr(self, obj): if obj.description: return truncatechars_html(obj.description, 150)
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))
def truncatechars_content(content): from django.template.defaultfilters import truncatechars_html return truncatechars_html(content, 50)
def get_content(self, announcement): return truncatechars_html(announcement.content, 200)