Пример #1
0
    def summarise(self, length=150):
        long_field = None

        if hasattr(self, 'summary'):
            long_field = getattr(self, 'summary')

        if not long_field:
            for field in ('content', 'description'):
                if hasattr(self, field):
                    long_field = getattr(self, field)

        if not long_field:
            return ''

        if len(long_field) <= length:
            return long_field

        return truncate_html(long_field, length, end='...')
Пример #2
0
 def summarize_html(self, length: int = 150) -> str:
     return truncate_html(self.html, length, end="...")
Пример #3
0
def test_truncation_with_string():
    assert truncate_html(result_a, 98) == result_a
Пример #4
0
def test_truncation(etree):
    assert truncate_html(etree, 98) == result_a
    assert truncate_html(etree, 98, break_words=True) == result_b
    assert truncate_html(etree, 98, end='...') == result_c
    assert truncate_html(etree, 98, end='...', break_words=True) == result_d
Пример #5
0
 def as_html(self, max_length=None, ellipsis=''):
     html = self.content_type.as_html(self.content)
     if max_length is not None:
         html = truncate_html(
             html, max_length, end=ellipsis, break_words=True)
     return html
Пример #6
0
def test_truncation_with_string():
    assert truncate_html(result_a, 98) == result_a
Пример #7
0
def test_truncation(etree):
    assert truncate_html(etree, 98) == result_a
    assert truncate_html(etree, 98, break_words=True) == result_b
    assert truncate_html(etree, 98, end='...') == result_c
    assert truncate_html(etree, 98, end='...', break_words=True) == result_d