Skip to content

Commit

Permalink
Merge branch 'bugfixes-0.1.2' into prepare-release
Browse files Browse the repository at this point in the history
Conflicts:
	docs/conf.py
	setup.py
  • Loading branch information
Pietro Albini committed Feb 25, 2016
2 parents 720335e + 7dc390e commit 1a470a8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 5 deletions.
11 changes: 10 additions & 1 deletion botogram/syntaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import re

from . import utils


_markdown_re = re.compile(r".*("
r"\*(.*)\*|"
Expand All @@ -30,11 +32,16 @@

def is_markdown(message):
"""Check if a string is actually markdown"""
# Don't mark part of URLs or email addresses as Markdown
message = utils.strip_urls(message)

return bool(_markdown_re.match(message))


def is_html(message):
"""Check if a string is actually HTML"""
# Here URLs are not stripped because no sane URL contains HTML tags in it,
# and for a few cases the speed penality is not worth
return bool(_html_re.match(message))


Expand All @@ -48,7 +55,9 @@ def guess_syntax(message, provided):
else:
return None

if provided in ("md", "markdown", "Markdown"):
if provided in ("plain",):
return None
elif provided in ("md", "markdown", "Markdown"):
return "Markdown"
elif provided in ("html", "HTML"):
return "HTML"
Expand Down
14 changes: 12 additions & 2 deletions botogram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
import logbook
import functools

# URLs regex created by http://twitter.com/imme_emosol

_username_re = re.compile(r"\@([a-zA-Z0-9_]{5}[a-zA-Z0-9_]*)")
_command_re = re.compile(r"^\/[a-zA-Z0-9_]+(\@[a-zA-Z0-9_]{5}[a-zA-Z0-9_]*)?$")
_email_re = re.compile(r"[a-zA-Z0-9_\.\+\-]+\@[a-zA-Z0-9_\.\-]+\.[a-zA-Z]+")
_url_re = re.compile(r"https?://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?")

# This small piece of global state will track if logbook was configured
_logger_configured = False
Expand Down Expand Up @@ -139,15 +142,22 @@ def docstring_of(func, bot=None, component_id=None):
return format_docstr(docstring)


def strip_urls(string):
"""Strip URLs and emails from a string"""
string = _url_re.sub("", string)
string = _email_re.sub("", string)
return string


def usernames_in(message):
"""Return all the matched usernames in the message"""
# Don't parse usernames in the commands
if _command_re.match(message.split(" ", 1)[0]):
message = message.split(" ", 1)[1]

# Strip email addresses from the message, in order to avoid matching the
# user's domain. This also happens to match username/passwords in URLs
message = _email_re.sub("", message)
# user's domain. Also strip URLs, in order to avoid usernames in them.
message = strip_urls(message)

results = []
for result in _username_re.finditer(message):
Expand Down
2 changes: 1 addition & 1 deletion docs/buildthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ versions:
source:
provider: git
url: .
checkout: release-0.1
checkout: v0.1.1
directory: docs
title: botogram 0.1
notice: alpha
Expand Down
22 changes: 22 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ botogram changelog

Here you can see what changed in every botogram release.

.. _changelog-0.2:

botogram 0.2
=============

*Alpha release, not yet released*

No changes yet.

.. _changelog-0.1.2:

botogram 0.1.2
==============

*Bugfix release, not yet released*

* Add a way to disable the syntax detector (`issue 27`_)
* Fix automatic syntax detector recognizing markdown in URLs (`issue 28`_)

.. _issue 27: https://github.com/pietroalbini/botogram/issues/27
.. _issue 28: https://github.com/pietroalbini/botogram/issues/28

.. _changelog-0.1.1:

botogram 0.1.1
Expand Down
7 changes: 7 additions & 0 deletions docs/tricks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ That parameter accepts the following values:
* ``markdown``, or its aliases ``md`` and ``Markdown``
* ``html``, or its alias ``HTML``

Also, if you don't want to use any rich formatting but the detector spots
something, you can disable it providing the special syntax ``plain`` to it:

.. code-block:: python
chat.send("*I don't want this to be detected*", syntax="plain")
.. note::

Support for rich formatting depends on your users' Telegram client. If
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def hello_command(chat, message, args):
if __name__ == "__main__":
bot.run()
Want to get started? `Go to the documentation_`
Want to get started? `Go to the documentation`_
.. _Telegram bots: https://core.telegram.org/bots
.. _Go to the documentation: https://botogram.pietroalbini.io/docs
Expand Down
6 changes: 6 additions & 0 deletions tests/test_syntaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def test_is_markdown():
assert botogram.syntaxes.is_markdown("[a](b)")
assert botogram.syntaxes.is_markdown("![a](b)!")

assert not botogram.syntaxes.is_markdown("hey@this_is_awesome.com")
assert not botogram.syntaxes.is_markdown("https://www.this_is_awesome.com")


def test_is_html():
assert not botogram.syntaxes.is_html("not HTML, sorry!")
Expand All @@ -38,6 +41,9 @@ def test_is_html():

def test_guess_syntax():
# Provided syntax name
for name in ("plain",):
assert botogram.syntaxes.guess_syntax("", name) is None

for name in ("md", "markdown", "Markdown"):
assert botogram.syntaxes.guess_syntax("", name) == "Markdown"

Expand Down

0 comments on commit 1a470a8

Please sign in to comment.