- Author:
- VincentLoy
- Posted:
- June 28, 2015
- Language:
- Python
- Version:
- 1.7
- Score:
- 0 (after 0 ratings)
Django Template Filter that parse a tweet in plain text and turn it with working Urls
Ceck it on GitHub
tweetParser Django Template Filter
this is a port of tweetParser.js to work as a Django template filter
How does it work ?
Once installed, just :
<p>{{ your_tweet|tweetparser }}</p>
Installation
Take a look at the Django Documentation
You can change the classes given to each anchor tags
USER_CLASS = 'tweet_user'
URL_CLASS = 'tweet_url'
HASHTAG_CLASS = 'hashtag'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | # vim: fileencoding=utf-8 tw=100 expandtab ts=4 sw=4 :
# Django Template Filter that parse a tweet in plain text and turn it with working Urls
# tweet_parser.py
# version : 1.0.0
# License : BSD
# Author : Vincent Loy <[email protected]>
# copyright (c) 2015 Vincent Loy
import re
from django import template
from django.utils import safestring
register = template.Library()
USER_CLASS = 'tweet_user'
URL_CLASS = 'tweet_url'
HASHTAG_CLASS = 'hashtag'
@register.filter(name='tweetparser')
def tweetparser(value):
def parse_url(tweet):
url_regex = '(^|\s)((f|ht)tps?://([^ \t\r\n]*[^ \t\r\n\)*_,\.]))'
match = re.findall(url_regex, value)
if match:
for m in match:
link = '<a href="{url}" target="_blank" class="{tweetclass}">{text}</a>' \
.format(url=m[1], text=m[1], tweetclass=URL_CLASS)
tweet = tweet.replace(m[1], link)
return tweet
def parse_users(tweet):
user_regex = '(\B@([a-zA-Z0-9_]+))'
match = re.findall(user_regex, tweet)
if match:
for m in match:
base_url = 'https://twitter.com/'
link = '<a href="{base_url}{user_only}" target="_blank" class="{tweetclass}">{text}</a>' \
.format(base_url=base_url, user_only=m[1], text=m[0], tweetclass=USER_CLASS)
tweet = tweet.replace(m[0], link)
return tweet
def parse_hashtags(tweet):
hashtag_regex = '(\B#([á-úÁ-Úä-üÄ-Üa-zA-Z0-9_]+))'
match = re.findall(hashtag_regex, tweet)
if match:
for m in match:
base_url = 'https://twitter.com/hashtag/'
link = '<a href="{base_url}{hashtag_text}" target="_blank" class="{tweetclass}">{hashtag}</a>' \
.format(base_url=base_url, hashtag_text=m[1], hashtag=m[0], tweetclass=HASHTAG_CLASS)
tweet = tweet.replace(m[0], link)
return tweet
value = parse_url(value)
value = parse_users(value)
value = parse_hashtags(value)
return safestring.mark_safe(value)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.