Beispiel #1
0
 def test_postfixed_tags(self):
     source = "#foo) #bar] #hoo, #hee."
     tags, text = find_tags(source)
     assert tags == {"foo", "bar", "hoo", "hee"}
     assert text == source
     tags, text = find_tags(source, replacer=self._replacer)
     assert text == "#foo/foo) #bar/bar] #hoo/hoo, #hee/hee."
Beispiel #2
0
 def test_start_of_paragraph_in_html_content(self):
     source = '<p>First line</p><p>#foobar #barfoo</p>'
     tags, text = find_tags(source)
     assert tags == {"foobar", "barfoo"}
     assert text == source
     tags, text = find_tags(source, replacer=self._replacer)
     assert text == '<p>First line</p><p>#foobar/foobar #barfoo/barfoo</p>'
Beispiel #3
0
 def test_prefixed_tags(self):
     source = "(#foo [#bar"
     tags, text = find_tags(source)
     assert tags == {"foo", "bar"}
     assert text == source
     tags, text = find_tags(source, replacer=self._replacer)
     assert text == "(#foo/foo [#bar/bar"
Beispiel #4
0
 def test_ok_with_html_tags_in_text(self):
     source = "<p>#starting and <span>#MixED</span> however not <#>this</#> or <#/>that"
     tags, text = find_tags(source)
     assert tags == {"starting", "mixed"}
     assert text == source
     tags, text = find_tags(source, replacer=self._replacer)
     assert text == "<p>#starting/starting and <span>#MixED/mixed</span> however not <#>this</#> or <#/>that"
Beispiel #5
0
 def test_finds_tags(self):
     source = "#post **Foobar** #tag #OtherTag #third\n#fourth"
     tags, text = find_tags(source)
     assert tags == {"third", "fourth", "post", "othertag", "tag"}
     assert text == source
     tags, text = find_tags(source, replacer=self._replacer)
     assert text == "#post/post **Foobar** #tag/tag #OtherTag/othertag #third/third\n#fourth/fourth"
Beispiel #6
0
 def test_code_block_tags_ignored(self):
     source = "foo\n```\n#code\n```\n#notcode\n\n    #alsocode\n"
     tags, text = find_tags(source)
     assert tags == {"notcode"}
     assert text == source
     tags, text = find_tags(source, replacer=self._replacer)
     assert text == "foo\n```\n#code\n```\n#notcode/notcode\n\n    #alsocode\n"
Beispiel #7
0
 def test_invalid_text_returns_no_tags(self):
     source = "#a!a #a#a #a$a #a%a #a^a #a&a #a*a #a+a #a.a #a,a #a@a #a£a #a(a #a)a #a=a " \
              "#a?a #a`a #a'a #a\\a #a{a #a[a #a]a #a}a #a~a #a;a #a:a #a\"a #a’a #a”a #\xa0cd"
     tags, text = find_tags(source)
     assert tags == set()
     assert text == source
     tags, text = find_tags(source, replacer=self._replacer)
     assert text == source
Beispiel #8
0
 def test_all_tags_are_parsed_from_text(self):
     source = "#starting and #MixED with some #line\nendings also tags can\n#start on new line"
     tags, text = find_tags(source)
     assert tags == {"starting", "mixed", "line", "start"}
     assert text == source
     tags, text = find_tags(source, replacer=self._replacer)
     assert text == "#starting/starting and #MixED/mixed with some #line/line\nendings also tags can\n" \
                    "#start/start on new line"
Beispiel #9
0
 def test_endings_are_filtered_out(self):
     source = "#parenthesis) #exp! #list] *#doh* _#bah_ #gah% #foo/#bar"
     tags, text = find_tags(source)
     assert tags == {
         "parenthesis", "exp", "list", "doh", "bah", "gah", "foo", "bar"
     }
     assert text == source
     tags, text = find_tags(source, replacer=self._replacer)
     assert text == "#parenthesis/parenthesis) #exp/exp! #list/list] *#doh/doh* _#bah/bah_ #gah/gah% " \
                    "#foo/foo/#bar/bar"
Beispiel #10
0
    def get_and_linkify_tags(self):
        """Find tags in text and convert them to Markdown links.

        Save found tags to the content.
        """
        def linkifier(tag: str) -> str:
            return "[#%s](%s)" % (
                tag, reverse("streams:tag", kwargs={"name": tag.lower()}))

        found_tags, text = find_tags(self.text, replacer=linkifier)
        self.save_tags(found_tags)
        return text
Beispiel #11
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
Beispiel #12
0
 def tags(self) -> List[str]:
     """Returns a `list` of unique tags contained in `raw_content`."""
     if not self.raw_content:
         return []
     tags, _text = find_tags(self.raw_content)
     return sorted(tags)