Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/TeddyDesTodes/twtxt into Te…
Browse files Browse the repository at this point in the history
…ddyDesTodes-master
  • Loading branch information
buckket committed Feb 8, 2016
2 parents 3375958 + 67ef9a5 commit 3fb1e7e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
2 changes: 2 additions & 0 deletions twtxt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from twtxt.helper import validate_created_at, validate_text
from twtxt.http import get_remote_tweets, get_remote_status
from twtxt.log import init_logging
from twtxt.mentions import expand_mentions
from twtxt.types import Tweet, Source

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -68,6 +69,7 @@ def cli(ctx, config, verbose):
@click.pass_context
def tweet(ctx, created_at, twtfile, text):
"""Append a new tweet to your twtxt file."""
text = expand_mentions(text)
tweet = Tweet(text, created_at) if created_at else Tweet(text)
if not add_local_tweet(tweet, twtfile):
click.echo("✗ Couldn’t write to file.")
Expand Down
4 changes: 4 additions & 0 deletions twtxt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ def twturl(self):
cfg = self.open_config()
return cfg.get("twtxt", "twturl", fallback=None)

@property
def source(self):
return Source(self.nick, self.twturl)

@property
def post_tweet_hook(self):
cfg = self.open_config()
Expand Down
6 changes: 5 additions & 1 deletion twtxt/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import shlex
import subprocess
import sys
import textwrap

import click

from twtxt.parser import parse_iso8601
from twtxt.mentions import format_mention


def style_timeline(tweets, porcelain=False):
Expand All @@ -31,9 +33,11 @@ def style_tweet(tweet, porcelain=False):
url=tweet.source.url,
tweet=str(tweet))
else:
styled_text = format_mention(tweet.text)
len_styling = len(styled_text) - len(click.unstyle(styled_text))

This comment has been minimized.

Copy link
@buckket

buckket Feb 8, 2016

Author Owner

This feels very wrong.

return "➤ {nick} ({time}):\n{tweet}".format(
nick=click.style(tweet.source.nick, bold=True),
tweet=tweet.limited_text,
tweet=textwrap.shorten(styled_text, 140 + len_styling),
time=click.style(tweet.relative_datetime, dim=True))


Expand Down
78 changes: 78 additions & 0 deletions twtxt/mentions.py
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)

0 comments on commit 3fb1e7e

Please sign in to comment.