Ensure tags are found also if wrapped within HTML blocks

merge-requests/160/head
Jason Robinson 2020-04-13 11:35:54 +03:00
rodzic 13f0cf0db6
commit f704175a21
2 zmienionych plików z 25 dodań i 11 usunięć

Wyświetl plik

@ -49,6 +49,14 @@ class TestFindTags:
tags, text = find_tags(source, replacer=self._replacer)
assert text == "#post/post **Foobar** #tag/tag #OtherTag/othertag #third/third\n#fourth/fourth"
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_postfixed_tags(self):
source = "#foo) #bar] #hoo, #hee."
tags, text = find_tags(source)

Wyświetl plik

@ -49,17 +49,23 @@ def find_tags(text: str, replacer: callable = None) -> Tuple[Set, str]:
# Check each word separately
words = line.split(" ")
for word in words:
candidate = word.strip().strip("([]),.!?:")
if candidate.startswith("#"):
candidate = candidate.strip("#")
if test_tag(candidate.lower()):
found_tags.add(candidate.lower())
if replacer:
try:
tag_word = word.replace("#%s" % candidate, replacer(candidate))
final_words.append(tag_word)
except Exception:
final_words.append(word)
if word.find('#') > -1:
candidate = word.strip().strip("([]),.!?:")
if candidate.find('<') > -1 or candidate.find('>') > -1:
# Strip html
candidate = bleach.clean(word, strip=True)
if candidate.startswith("#"):
candidate = candidate.strip("#")
if test_tag(candidate.lower()):
found_tags.add(candidate.lower())
if replacer:
try:
tag_word = word.replace("#%s" % candidate, replacer(candidate))
final_words.append(tag_word)
except Exception:
final_words.append(word)
else:
final_words.append(word)
else:
final_words.append(word)
else: