Exemplo n.º 1
0
 def render(self):
     """Pre-render text to Content.rendered."""
     text = self.get_and_linkify_tags()
     rendered = commonmark(text).strip()
     rendered = process_text_links(rendered)
     if self.show_preview:
         if self.oembed:
             rendered = "%s<br>%s" % (rendered, self.oembed.oembed)
         if self.opengraph:
             image_in_text = self.opengraph.image and self.text.find(
                 self.opengraph.image) > -1
             rendered = "%s%s" % (rendered,
                                  render_to_string(
                                      "content/_og_preview.html", {
                                          "image_in_text": image_in_text,
                                          "opengraph": self.opengraph,
                                      }))
     self.rendered = rendered
     Content.objects.filter(id=self.id).update(rendered=rendered)
Exemplo n.º 2
0
    def rendered_content(self) -> str:
        """Returns the rendered version of raw_content, or just raw_content."""
        from federation.utils.django import get_configuration
        try:
            config = get_configuration()
            if config["tags_path"]:

                def linkifier(tag: str) -> str:
                    return f'<a href="{config["base_url"]}{config["tags_path"].replace(":tag:", tag.lower())}" ' \
                           f'class="mention hashtag" rel="noopener noreferrer">' \
                           f'#<span>{tag}</span></a>'
            else:
                linkifier = None
        except ImportError:
            linkifier = None

        if self._rendered_content:
            return self._rendered_content
        elif self._media_type == "text/markdown" and self.raw_content:
            # Do tags
            _tags, rendered = find_tags(self.raw_content, replacer=linkifier)
            # Render markdown to HTML
            rendered = commonmark(rendered).strip()
            # Do mentions
            if self._mentions:
                for mention in self._mentions:
                    # Only linkify mentions that are URL's
                    if not mention.startswith("http"):
                        continue
                    display_name = get_name_for_profile(mention)
                    if not display_name:
                        display_name = mention
                    rendered = rendered.replace(
                        "@{%s}" % mention,
                        f'@<a href="{mention}" class="mention"><span>{display_name}</span></a>',
                    )
            # Finally linkify remaining URL's that are not links
            rendered = process_text_links(rendered)
            return rendered
        return self.raw_content
Exemplo n.º 3
0
 def test_link_at_start_or_end(self):
     assert process_text_links('https://example.org example.org\nhttp://example.org') == \
            '<a href="https://example.org" rel="nofollow" target="_blank">https://example.org</a> ' \
            '<a href="http://example.org" rel="nofollow" target="_blank">example.org</a>\n' \
            '<a href="http://example.org" rel="nofollow" target="_blank">http://example.org</a>'
Exemplo n.º 4
0
 def test_does_not_remove_mention_classes(self):
     assert process_text_links('<p><span class="h-card"><a href="https://dev.jasonrobinson.me/u/jaywink/" '
                               'class="u-url mention">@<span>jaywink</span></a></span> boom</p>') == \
        '<p><span class="h-card"><a class="u-url mention" href="https://dev.jasonrobinson.me/u/jaywink/" ' \
        'rel="nofollow" target="_blank">@<span>jaywink</span></a></span> boom</p>'
Exemplo n.º 5
0
 def test_does_not_add_target_blank_if_link_is_internal(self):
     assert process_text_links('<a href="/streams/tag/foobar">#foobar</a>') == \
            '<a href="/streams/tag/foobar">#foobar</a>'
Exemplo n.º 6
0
 def test_emails_are_skipped(self):
     assert process_text_links('*****@*****.**') == '*****@*****.**'
Exemplo n.º 7
0
 def test_code_sections_are_skipped(self):
     assert process_text_links('<code>https://example.org</code><code>\nhttps://example.org\n</code>') == \
            '<code>https://example.org</code><code>\nhttps://example.org\n</code>'
Exemplo n.º 8
0
 def test_existing_links_get_attrs_added(self):
     assert process_text_links('<a href="https://example.org">https://example.org</a>') == \
            '<a href="https://example.org" rel="nofollow" target="_blank">https://example.org</a>'