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."
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>'
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"
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"
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"
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"
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
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"
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"
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
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
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)