Esempio n. 1
0
 def test_truncate_html_words(self):
     self.assertEqual(
         u"<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>",
         text.truncate_html_words(
             "<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>", 10
         ),
     )
     self.assertEqual(
         u"<p><strong><em>The quick brown fox ...</em></strong></p>",
         text.truncate_html_words(
             "<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>", 4
         ),
     )
     self.assertEqual(
         u"<p><strong><em>The quick brown fox ....</em></strong></p>",
         text.truncate_html_words(
             "<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>", 4, "...."
         ),
     )
     self.assertEqual(
         u"<p><strong><em>The quick brown fox</em></strong></p>",
         text.truncate_html_words(
             "<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>", 4, None
         ),
     )
 def test_truncate_words(self):
     self.assertEqual(
         u'The quick brown fox jumped over the lazy dog.',
         text.truncate_words(
             u'The quick brown fox jumped over the lazy dog.', 10))
     self.assertEqual(
         u'The quick brown fox ...',
         text.truncate_words(
             'The quick brown fox jumped over the lazy dog.', 4))
     self.assertEqual(
         u'The quick brown fox ....',
         text.truncate_words(
             'The quick brown fox jumped over the lazy dog.', 4, '....'))
     self.assertEqual(
         u'<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>',
         text.truncate_html_words(
             '<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>',
             10))
     self.assertEqual(
         u'<p><strong><em>The quick brown fox ...</em></strong></p>',
         text.truncate_html_words(
             '<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>',
             4))
     self.assertEqual(
         u'<p><strong><em>The quick brown fox ....</em></strong></p>',
         text.truncate_html_words(
             '<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>',
             4, '....'))
     self.assertEqual(
         u'<p><strong><em>The quick brown fox</em></strong></p>',
         text.truncate_html_words(
             '<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>',
             4, None))
Esempio n. 3
0
 def test_old_truncate_html_words(self):
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter("always")
         self.assertEqual(
             "<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>",
             text.truncate_html_words(
                 "<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>", 10
             ),
         )
         self.assertEqual(
             "<p><strong><em>The quick brown fox ...</em></strong></p>",
             text.truncate_html_words(
                 "<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>", 4
             ),
         )
         self.assertEqual(
             "<p><strong><em>The quick brown fox ....</em></strong></p>",
             text.truncate_html_words(
                 "<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>", 4, "...."
             ),
         )
         self.assertEqual(
             "<p><strong><em>The quick brown fox</em></strong></p>",
             text.truncate_html_words(
                 "<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>", 4, None
             ),
         )
         self.assertGreater(len(w), 0)
Esempio n. 4
0
File: text.py Progetto: mick/django
 def test_old_truncate_html_words(self):
     self.assertEqual(u'<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>',
         text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 10))
     self.assertEqual(u'<p><strong><em>The quick brown fox ...</em></strong></p>',
         text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4))
     self.assertEqual(u'<p><strong><em>The quick brown fox ....</em></strong></p>',
         text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, '....'))
     self.assertEqual(u'<p><strong><em>The quick brown fox</em></strong></p>',
         text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, None))
Esempio n. 5
0
File: text.py Progetto: rasca/django
 def test_old_truncate_html_words(self):
     with warnings.catch_warnings(record=True) as w:
         self.assertEqual(u'<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>',
             text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 10))
         self.assertEqual(u'<p><strong><em>The quick brown fox ...</em></strong></p>',
             text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4))
         self.assertEqual(u'<p><strong><em>The quick brown fox ....</em></strong></p>',
             text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, '....'))
         self.assertEqual(u'<p><strong><em>The quick brown fox</em></strong></p>',
             text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, None))
         self.assertEqual(len(w), 4)
Esempio n. 6
0
File: text.py Progetto: pvl/django
 def test_old_truncate_html_words(self):
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter("always")
         self.assertEqual(u'<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>',
             text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 10))
         self.assertEqual(u'<p><strong><em>The quick brown fox ...</em></strong></p>',
             text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4))
         self.assertEqual(u'<p><strong><em>The quick brown fox ....</em></strong></p>',
             text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, '....'))
         self.assertEqual(u'<p><strong><em>The quick brown fox</em></strong></p>',
             text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, None))
         self.assertGreater(len(w), 0)
Esempio n. 7
0
 def summary(self):
     """Returns a summary of the blog post."""
     match = re.search("<!--.*cut.*-->", self.body)
     if match:
         return self.body[:match.start(0)]
     else:
         return text.truncate_html_words(self.body, config.summary_length)
Esempio n. 8
0
def yvcut(string, o):
    n = len(string)
    s = postcut(string, o)
    if len(s) == len(string):
        return text.truncate_html_words(string, 256)
    else:
        return s
Esempio n. 9
0
def yvcut(string, o):
    n = len(string)
    s = postcut(string, o)
    if len(s) == len(string):
        return text.truncate_html_words(string, 256)
    else:
        return s
Esempio n. 10
0
def truncate_entry(entry, words=100):
    end_text = '... <a href="%s">more</a>' % entry.get_absolute_url()
    content = entry.html_content
    try:
        return truncate_html_words(content, int(words), end_text=end_text)
    except ValueError: # invalid literal for int()
        return value # Fail silently.
Esempio n. 11
0
def truncatewords_html_remove_end(value,arg):
    arg_list = arg.split(',')
    try:
        length = int(arg_list[0])
    except ValueError:
        return value
    return mark_safe(truncate_html_words(value,length,''))
Esempio n. 12
0
def readmore(value, arg):
    """ Add a Readmore link to the content"""
    from django.utils.text import truncate_html_words
    length = 100
    readmore = _('Read more')
    end_text = '...<a href="' + arg + '" style="font-size: 75%;">' + readmore + '</a>'
    return truncate_html_words(value, length, end_text)
Esempio n. 13
0
def render_excerpt(post):
    from bs4 import BeautifulSoup
    from django.utils import text, html
    
    VALID_TAGS = ['p']

    content = post.content

    has_more = content.find('<!-- more -->')
    if has_more == -1:
        has_more = content.find('<!--more-->') # Might be Wordpress style

    if has_more > -1:
        content = content[:has_more]

    content = re.sub(r"(\[caption)([^\]]*)(])(.*)(\[/caption\])", '', content)
    content = re.sub(r'(\[source((code)*? lang(uage)*?)*?=([\'"]*?)(python)([\'"]*?)])(.*?)(\[/source(code)*?\])', '', content, flags=re.MULTILINE|re.DOTALL)

    content = re.sub(r"(\[caption)([^\]]*)(])(.*)(\[/caption\])", '', content)
    content = re.sub(r"(\[youtube:)([^\]]*)(])", '', content)
    
    soup = BeautifulSoup(content, "html.parser")

    for tag in soup.findAll(True):
        if tag.name not in VALID_TAGS:
            tag.replaceWithChildren()
            
    stripped_html = force_unicode(soup.renderContents())
    return force_unicode(text.truncate_html_words(stripped_html, 50))
Esempio n. 14
0
def find_post_content(feed_obj, entry):
    """Find the correct content field for a post."""
    try:
        content = entry["content"][0]["value"]
    except (IndexError, KeyError):
        content = entry.get("description") or entry.get("summary") or ""

    if '<img' not in content:
        # if there's no image and the we add an image to the feed
        def build_img(img_dict):
            try:
                # The tag is url instead of src... pain
                img = "<img src='%s'" % img_dict.get("url")
            except KeyError:
                return ''
            img_dict.pop('url')
            for attr in img_dict.items():
                img += "%s='%s'" % (attr[0], attr[1])
            img += ">"
            return img

        try:
            thumbnail = entry["media_thumbnail"][0]
            img = build_img(thumbnail)
        except (IndexError, KeyError):
            img = ""
        content = img + content
    try:
        content = truncate_html_words(content, conf.DEFAULT_ENTRY_WORD_LIMIT)
    except UnicodeDecodeError:
        content = ""

    return feed_content_optimizer.optimize(content)
Esempio n. 15
0
    def get_teaser(self):
        if self.description:
            teaser = formatter(self.description, filter_name=self.markup)
        else:
            teaser = self.rendered_content

        return truncate_html_words(teaser, 50)
Esempio n. 16
0
def control_trunc(value, max_words):
  if BLOG['truncater'] in value:
    return value[:value.find(BLOG['truncater'])]
  if wordcount(value) > max_words :
    return truncate_html_words(value, max_words)
  else:
    return value
Esempio n. 17
0
def truncate_html_post(post_html):
    """truncates html if it is longer than 100 words"""
    post_html = truncate_html_words(post_html, 5)
    post_html = '<div class="truncated-post">' + post_html
    post_html += '<span class="expander">(<a>' + _("more") + "</a>)</span>"
    post_html += '<div class="clearfix"></div></div>'
    return post_html
Esempio n. 18
0
def truncate_html_post(post_html):
    """truncates html if it is longer than 100 words"""
    post_html = truncate_html_words(post_html, 5)
    post_html = '<div class="truncated-post">' + post_html
    post_html += '<span class="expander">(<a>' + _('more') + '</a>)</span>'
    post_html += '<div class="clearfix"></div></div>'
    return post_html
Esempio n. 19
0
 def summary(self):
     """Returns a summary of the blog post."""
     match = re.search("<!--.*cut.*-->", self.body)
     if match:
         return self.body[:match.start(0)]
     else:
         return text.truncate_html_words(self.body, config.summary_length)
Esempio n. 20
0
def find_post_content(feed_obj, entry):
    """Find the correct content field for a post."""
    try:
        content = entry["content"][0]["value"]
    except (IndexError, KeyError):
        content = entry.get("description") or entry.get("summary") or ""

    if '<img' not in content:
        # if there's no image and the we add an image to the feed
        def build_img(img_dict):
            try:
                # The tag is url instead of src... pain
                img = "<img src='%s'" % img_dict.get("url")
            except KeyError:
                return ''
            img_dict.pop('url')
            for attr in img_dict.items():
                img += "%s='%s'" % (attr[0], attr[1])
            img += ">"
            return img

        try:
            thumbnail = entry["media_thumbnail"][0]
            img = build_img(thumbnail)
        except (IndexError, KeyError):
            img = ""
        content = img + content
    try:
        content = truncate_html_words(content, conf.DEFAULT_ENTRY_WORD_LIMIT)
    except UnicodeDecodeError:
        content = ""

    return feed_content_optimizer.optimize(content)
Esempio n. 21
0
def truncatewords_html(value, length):
    try:
        from django.utils.text import Truncator
    except ImportError:
        from django.utils.text import truncate_html_words # Django < 1.6
    else:
        truncate_html_words = lambda value, length: Truncator(value).words(length, html=True)
    return truncate_html_words(value, int(length))
Esempio n. 22
0
def render_summary(post):
    """Return the post's summary rendered to HTML."""
    renderer = get_renderer(post)
    match = re.search(CUT_SEPARATOR_REGEX, post.body)
    if match:
        return renderer(post.body[: match.start(0)])
    else:
        return text.truncate_html_words(renderer(clean_content(post.body)), config.summary_length)
Esempio n. 23
0
def excerpt(html, num, end_text='...'):
    """Return excerpt of html data.
    """
    if truncate_html_words:
        excerpt = truncate_html_words(html, num, end_text)
    else:
        excerpt = html
    return excerpt
Esempio n. 24
0
def excerpt(html, num, end_text='...'):
    """Return excerpt of html data.
    """
    if truncate_html_words:
        excerpt = truncate_html_words(html, num, end_text)
    else:
        excerpt = html
    return excerpt
Esempio n. 25
0
def truncatewords_link(value, size, obj):
    """Truncates a string to the given size
    using the dots, and if it oversize the given length, is shows
    a Read More link"""
    content = {"value": truncate_html_words(value, size)}
    words = value.split()
    if len(words) > size:
        content["obj"] = obj
    return content
Esempio n. 26
0
def truncatewords_link(value, size, obj):
    """Truncates a string to the given size
    using the dots, and if it oversize the given length, is shows
    a Read More link"""
    content = {"value": truncate_html_words(value, size)}
    words = value.split()
    if len(words) > size:
        content["obj"] = obj
    return content
Esempio n. 27
0
def truncatewords_html(value, length):
    try:
        from django.utils.text import Truncator
    except ImportError:
        from django.utils.text import truncate_html_words  # Django < 1.6
    else:
        truncate_html_words = lambda value, length: Truncator(value).words(
            length, html=True)
    return truncate_html_words(value, int(length))
Esempio n. 28
0
def render_summary(post):
    """Return the post's summary rendered to HTML."""
    renderer = get_renderer(post)
    match = re.search(CUT_SEPARATOR_REGEX, post.body)
    if match:
        return renderer(post.body[:match.start(0)])
    else:
        return text.truncate_html_words(renderer(clean_content(post.body)),
                                        config.summary_length)
Esempio n. 29
0
    def get_teaser(self, truncate=30):
        if self.description:
            teaser = formatter(self.description, filter_name=self.markup)
        else:
            teaser = self.rendered_content

        if truncate:
            return truncate_html_words(teaser, truncate)
        else:
            return teaser
Esempio n. 30
0
def truncatehtml(string, length):
    """
    Truncate the text to a certain length, honoring html.
    
    Usage:
    
    {{ my_variable|truncatehtml:250 }}
    
    """

    return text.truncate_html_words(string, length)
Esempio n. 31
0
    def _get_teaser(self):
        """
        Retrieve some part of the article or the article's description.
        """
        if not self._teaser:
            if len(self.description.strip()):
                self._teaser = self.description
            else:
                self._teaser = truncate_html_words(self.rendered_content, WORD_LIMIT)

        return self._teaser
Esempio n. 32
0
def truncate_html_words(value, num_words):
    """Backwords compatibility shim."""
    try:
        # Django 1.4+
        from django.utils.text import Truncator
    except ImportError:
        # Django 1.3
        from django.utils.text import truncate_html_words
        return truncate_html_words(value, num_words)
    else:
        return Truncator(value).words(num_words, html=True)
Esempio n. 33
0
    def _get_teaser(self):
        """
        Retrieve some part of the article or the article's description.
        """
        if not self._teaser:
            if len(self.description.strip()):
                self._teaser = self.description
            else:
                self._teaser = truncate_html_words(self.rendered_content, WORD_LIMIT)

        return self._teaser
Esempio n. 34
0
def truncatehtml(string, length):
    """
    Truncate the text to a certain length, honoring html.
    
    Usage:
    
    {{ my_variable|truncatehtml:250 }}
    
    """
    
    return text.truncate_html_words(string, length)
Esempio n. 35
0
def truncatewords_html(value, arg):
    """
    Truncates HTML after a certain number of words.

    Argument: Number of words to truncate after.
    """
    from django.utils.text import truncate_html_words
    try:
        length = int(arg)
    except ValueError: # invalid literal for int()
        return value # Fail silently.
    return truncate_html_words(value, length)
def truncatewords_html(value, arg):
    """
    Truncates HTML after a certain number of words.

    Argument: Number of words to truncate after.
    """
    from django.utils.text import truncate_html_words
    try:
        length = int(arg)
    except ValueError: # invalid literal for int()
        return value # Fail silently.
    return truncate_html_words(value, length)
Esempio n. 37
0
def truncatewords_html(value, arg):
    """
    Truncates HTML after a certain number of words.

    Argument: Number of words to truncate after.

    Newlines in the HTML are preserved.
    """
    try:
        length = int(arg)
    except ValueError: # invalid literal for int()
        return value # Fail silently.
    return truncate_html_words(value, length)
Esempio n. 38
0
    def render(self, context):
        from django.utils.text import truncate_html_words
        length = self.length.resolve(context)
        context.push()
        original = self.nodelist.render(context)
        output = truncate_html_words(original, length)
        context.pop()

        if len(output) < len(original):
          context.push()
          output = output + self.more.render(context)
          context.pop()
        return output
Esempio n. 39
0
def format_history_text(text):
    """Run history text through some cleaning and add ellipsis if it's too long."""
    full = mark_safe(text)

    if text.startswith("This was part of a ballot set with:"):
        full = urlize_ietf_docs(full)

    full = mark_safe(keep_spacing(linebreaksbr(urlize(sanitize_html(full)))))
    snippet = truncate_html_words(full, 25)
    if snippet != full:
        return mark_safe(
            u'<div class="snippet">%s<span class="show-all">[show all]</span></div><div style="display:none" class="full">%s</div>'
            % (snippet, full))
    return full
Esempio n. 40
0
def getBlurb(request,blurbID,wordCount):
    """
    Retunr the conetnt of the blurb identified by the ID.
    Will return up to work count. IF wourd count is 0 
    return the entire blurb.
    """

    theBlurb  = Blurb.objects.get(id=blurbID)
    if int(wordCount) == 0:
        crap = theBlurb.crap
    else:
        from django.utils.text import truncate_html_words
        crap = truncate_html_words(theBlurb.crap, wordCount)

    return HttpResponse(crap)
Esempio n. 41
0
def short_desc(request, news_id):
    """
    view untuk menampilkan penjelasan singkat berita dengan format txt
    untuk id => news_id
    """
    news = get_object_or_404(News, id=news_id, active=True)
    return render_to_response('news/short_desc.html',
        {"lon": news.location.x,
            "lat": news.location.y,
            "title": news.title,
            "description": truncate_html_words(news.content_html, 10),
            "icon": "abc.png",
            "icon_width": 50,
            "icon_height": 50},
        context_instance=RequestContext(request))
Esempio n. 42
0
    def lead_in(self):
        """
        Returns a truncated version of the excerpt or main content with an
        appended 'read more...' link
        """
        if self.excerpt:
            html = self.excerpt
        else:
            html = truncate_html_words(self.body, 50, end_text='')
        permalink = self.permalink(text=ugettext("read more&hellip;"), title=ugettext("Read full article"))
        content = "%s %s" % (html, permalink)

        if self.markup == Entry.MARKDOWN:
            return mark_safe(markdown(self.excerpt, ['codehilite']))
        elif self.markup == Entry.TEXTILE:
            return mark_safe(textile.textile(content))
Esempio n. 43
0
 def __unicode__(self):
     if self.slug:
         return self.slug
     else:
         return strip_tags(truncate_html_words(self.content, 5))
Esempio n. 44
0
 def item_description(self, item):
     return truncate_html_words(item.body, 25)
Esempio n. 45
0
def truncatehtml(string, length):
    return text.truncate_html_words(string, length)
Esempio n. 46
0
 def item_description(self, item):
     return text.truncate_html_words(item.content, 50)
Esempio n. 47
0
def truncatewords_html(value, length):
    from django.utils.text import truncate_html_words
    return truncate_html_words(value, int(length))
Esempio n. 48
0
 def item_description(self, item):
     return truncate_html_words(item.body,25)
Esempio n. 49
0
def truncatewords_html(value, length):
    from django.utils.text import truncate_html_words
    return truncate_html_words(value, int(length))