-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git://github.com/TeddyDesTodes/twtxt into Te…
…ddyDesTodes-master
- Loading branch information
Showing
4 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
twtxt.mentions | ||
~~~~~~~~~~~~~~ | ||
This module implements functions for handling mentions in twtxt. | ||
:copyright: (c) 2016 by buckket. | ||
:license: MIT, see LICENSE for more details. | ||
""" | ||
|
||
import re | ||
|
||
import click | ||
|
||
mention_re = re.compile(r'@<(?:(?P<name>.*?)\s)?(?P<url>.*?://.*?)>') | ||
short_mention_re = re.compile(r'@(?P<name>\S+)') | ||
|
||
|
||
def get_source_by_url(url): | ||
conf = click.get_current_context().obj["conf"] | ||
if url == conf.twturl: | ||
return conf.source | ||
return next((source for source in conf.following if source.url == url), None) | ||
|
||
|
||
def get_source_by_name(nick): | ||
conf = click.get_current_context().obj["conf"] | ||
if nick == conf.nick and conf.twturl: | ||
return conf.source | ||
return next((source for source in conf.following if source.nick == nick), None) | ||
|
||
|
||
def expand_mentions(text, embed_names=True): | ||
"""Searches the given text for mentions and expands them. | ||
For example: | ||
"@source.nick" will be expanded to "@<source.nick source.url>". | ||
""" | ||
if embed_names: | ||
mention_format = "@<{name} {url}>" | ||
else: | ||
mention_format = "@<{url}>" | ||
|
||
def handle_mention(match): | ||
source = get_source_by_name(match.group(1)) | ||
if source is None: | ||
return "@{}".format(match.group(1)) | ||
return mention_format.format( | ||
name=source.nick, | ||
url=source.url) | ||
|
||
return short_mention_re.sub(handle_mention, text) | ||
|
||
|
||
def format_mentions(text, embedded_names=False): | ||
"""Searches the given text for mentions generated by `expand_mention()` and returns a human-readable form. | ||
For example: | ||
"@<bob http://example.org/twtxt.txt>" will result in "@bob" | ||
If you follow a Source: source.nick will be bold | ||
If you are the mentioned Source: nick.nick will be bold and coloured | ||
If nothing from the above is true: nick will be unstyled | ||
""" | ||
|
||
def handle_mention(match): | ||
name, url = match.groups() | ||
source = get_source_by_url(url) | ||
if source is not None and (not name or embedded_names is False): | ||
if source.nick == click.get_current_context().obj["conf"].nick: | ||
mention = click.style("@{}".format(source.nick), fg="magenta", bold=True) | ||
else: | ||
mention = click.style("@{}".format(source.nick), bold=True) | ||
else: | ||
mention = "@{}".format(name) | ||
return mention | ||
|
||
return mention_re.sub(handle_mention, text) |
This feels very wrong.