Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ def create_link_for_tag(self, tag, possible_matches, subreddit="vim"):
request = requests.head(link)

if request.ok:
text += "* [`{}`]({}) in _{}.txt_\n".format(topic, link, doc)
escaped, count = re.subn(r"([\[\]`])", r"\\\1", topic)
if count > 0:
topic = escaped
text += "* [{}]({}) in _{}.txt_\n".format(topic, link, doc)
else:
text += "* [`{}`]({}) in _{}.txt_\n".format(topic, link, doc)
return text

def start(self):
Expand Down
23 changes: 23 additions & 0 deletions test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ def test_non_exact_matches(self):
best_tag = next(iter(result.keys()))[1]
self.assertEqual(best_tag, v)

def test_difficult_markdown(self):
"""
Test that bot falls back to simpler formatting for difficult markdown.
"""

tests = [
{"topic": ":make", "link": r"[`:make`]"},
{"topic": "[I", "link": r"[\[I]"},
{"topic": "[_CTRL-I", "link": r"[\[_CTRL-I]"},
{"topic": "`]", "link": r"[\`\]]"},
{"topic": "c_CTRL-F", "link": r"[`c_CTRL-F`]"},
{"topic": "c_CTRL-G", "link": r"[`c_CTRL-G`]"},
{"topic": "c_CTRL-L", "link": r"[`c_CTRL-L`]"},
{"topic": "c_CTRL-R_CTRL-W", "link": r"[`c_CTRL-R_CTRL-W`]"},
{"topic": "pattern.txt", "link": r"[`pattern.txt`]"},
{"topic": "search()", "link": r"[`search"},
]

bot = Bot()
for t in tests:
reply = bot.create_comment("`:h {}`".format(t["topic"]), None, "vim")
self.assertIn(t["link"], reply)

def test_empty_comment(self):
"""
Test comment is empty if nothing is found
Expand Down