Skip to content

Commit 75a84db

Browse files
author
Pietro Albini
committed
Fix GH-40 -- Syntax detector checking only the first line
Before this commit, if you didn't use any syntaxes in the first line of a message but you used some in the following ones, botogram's syntax detector didn't recognize anything in your message.
1 parent 60d7493 commit 75a84db

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

botogram/syntaxes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ def is_markdown(message):
3535
# Don't mark part of URLs or email addresses as Markdown
3636
message = utils.strip_urls(message)
3737

38-
return bool(_markdown_re.match(message))
38+
return bool(_markdown_re.match(message.replace("\n", "")))
3939

4040

4141
def is_html(message):
4242
"""Check if a string is actually HTML"""
4343
# Here URLs are not stripped because no sane URL contains HTML tags in it,
4444
# and for a few cases the speed penality is not worth
45-
return bool(_html_re.match(message))
45+
return bool(_html_re.match(message.replace("\n", "")))
4646

4747

4848
def guess_syntax(message, provided):

docs/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Bug fixes
4949
---------
5050

5151
* Fix the syntax detector checking URLs with dashes in the domain (`issue 32`_)
52+
* Fix the syntax detector checking only the first line of a message (`issue
53+
40`_)
5254
* Fix inability to send messages to channels from a running bot (`issue 35`_)
5355
* Fix inability to download stickers (`issue 36`_)
5456

@@ -63,6 +65,7 @@ Deprecated features will be removed in botogram 1.0!
6365
.. _issue 32: https://github.com/pietroalbini/botogram/issues/32
6466
.. _issue 35: https://github.com/pietroalbini/botogram/issues/35
6567
.. _issue 36: https://github.com/pietroalbini/botogram/issues/36
68+
.. _issue 40: https://github.com/pietroalbini/botogram/issues/40
6669

6770
.. _changelog-0.1.2:
6871

tests/test_syntaxes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ def test_is_markdown():
1717
for delimiter in "*", "_", "`", "```":
1818
assert botogram.syntaxes.is_markdown(delimiter+"a"+delimiter)
1919
assert botogram.syntaxes.is_markdown("!"+delimiter+"a"+delimiter+"!")
20+
assert botogram.syntaxes.is_markdown("a\n"+delimiter+"a"+delimiter)
21+
assert botogram.syntaxes.is_markdown("a"+delimiter+"a\na"+delimiter)
2022

2123
assert botogram.syntaxes.is_markdown("[a](b)")
2224
assert botogram.syntaxes.is_markdown("![a](b)!")
25+
assert botogram.syntaxes.is_markdown("a\n[a](b)")
26+
assert botogram.syntaxes.is_markdown("a[a\na](b)")
27+
assert botogram.syntaxes.is_markdown("a[a](b\nb)")
2328

2429
assert not botogram.syntaxes.is_markdown("hey@this_is_awesome.com")
2530
assert not botogram.syntaxes.is_markdown("https://www.this_is_awesome.com")
@@ -32,11 +37,16 @@ def test_is_html():
3237
for tag in "b", "strong", "i", "em", "pre", "code":
3338
assert botogram.syntaxes.is_html("<"+tag+">a</"+tag+">")
3439
assert botogram.syntaxes.is_html("!<"+tag+">a</"+tag+">!")
40+
assert botogram.syntaxes.is_html("a\n<"+tag+">a</"+tag+">")
41+
assert botogram.syntaxes.is_html("a<"+tag+">a\na</"+tag+">")
3542

3643
assert not botogram.syntaxes.is_html("<a>a</a>")
3744
assert not botogram.syntaxes.is_html("<a test=\"b\">a</a>")
3845
assert botogram.syntaxes.is_html("<a href=\"b\">a</a>")
3946
assert botogram.syntaxes.is_html("!<a href=\"b\">a</a>!")
47+
assert botogram.syntaxes.is_html("a\n<a href=\"b\">a</a>")
48+
assert botogram.syntaxes.is_html("a<a href=\"b\">a\na</a>")
49+
assert botogram.syntaxes.is_html("a<a href=\"b\nb\">a</a>")
4050

4151

4252
def test_guess_syntax():

0 commit comments

Comments
 (0)