Skip to content

Commit aba9c2f

Browse files
committed
URL truncation and fragmention embeds
Now takes a maxUrlLength for abbreviated urls, and if it finds a fragmention when auto_embed is on, it inserts a block quote
1 parent d1349e0 commit aba9c2f

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

‎cassis.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
# -*- coding: utf-8 -*-
2+
13
import re
24
import sys
35
import os.path
6+
import urllib
47

58
if sys.version < '3':
69
from urlparse import urlparse
@@ -21,7 +24,7 @@ def auto_link_re():
2124
# - Tantek 2010-046 (moved to auto_link_re 2012-062)
2225

2326

24-
def auto_link(text, do_embed=False):
27+
def auto_link(text, do_embed=False,maxUrlLength=0):
2528
""" auto_link: param 1: text; param 2: do embeds or not
2629
auto_link is idempotent, works on plain text or typical markup.
2730
"""
@@ -67,7 +70,14 @@ def auto_link(text, do_embed=False):
6770
hn = wp.netloc
6871
pa = wp.path
6972
ih = wmi.startswith('http')
70-
73+
displayUrl = mi
74+
if maxUrlLength:
75+
displayUrl = wp.netloc+wp.path
76+
if wp.query:
77+
displayUrl = displayUrl + '?'+wp.query
78+
if len(displayUrl)> maxUrlLength:
79+
shortUrl = unicode(displayUrl[:maxUrlLength])+u'…'
80+
displayUrl = shortUrl.encode('utf-8')
7181
if (fe and
7282
(fe == '.jpeg' or fe == '.jpg' or fe == '.png' or
7383
fe == '.gif' or fe == '.svg')):
@@ -81,7 +91,7 @@ def auto_link(text, do_embed=False):
8191
wmi, '"></video></a>' + afterlink)
8292
elif ih and hn == 'vimeo.com' and pa[1:].isdigit():
8393
text = (text + '<a class="auto-link" href="' +
84-
wmi + '">' + mi + '</a> <iframe class="vimeo-player auto-link figure" width="480" height="385" style="border:0" src="' + prot + '//player.vimeo.com/video/' +
94+
wmi + '">' + displayUrl + '</a> <iframe class="vimeo-player auto-link figure" width="480" height="385" style="border:0" src="' + prot + '//player.vimeo.com/video/' +
8595
pa[1:] + '"></iframe>' + afterlink)
8696
elif (hn == 'youtu.be' or
8797
((hn == 'youtube.com' or hn == 'www.youtube.com')
@@ -93,24 +103,30 @@ def auto_link(text, do_embed=False):
93103
yvid = mi[offs + 8:].split('&', 1)[0]
94104

95105
text = (text + '<a class="auto-link" href="' +
96-
wmi + '">' + mi + '</a> <iframe class="youtube-player auto-link figure" width="480" height="385" style="border:0" src="' + prot + '//www.youtube.com/embed/' +
106+
wmi + '">' + displayUrl + '</a> <iframe class="youtube-player auto-link figure" width="480" height="385" style="border:0" src="' + prot + '//www.youtube.com/embed/' +
97107
yvid + '"></iframe>' +
98108
afterlink)
99109
elif mi.startswith('@'):
100110
if (sp[i + 1][:1] == '.' and
101111
spliti != '' and ctype_email_local(spliti[-1])):
102112
# if email address, simply append info, no linking
103-
text = text + mi + afterlink
113+
text = text + displayUrl + afterlink
104114

105115
else:
106116
# treat it as a Twitter @-username reference and link it
107117
text = (text + '<a class="auto-link h-x-username" href="' +
108-
wmi + '">' + mi + '</a>' +
118+
wmi + '">' + displayUrl + '</a>' +
109119
afterlink)
110120

121+
elif wp.fragment:
122+
fragmentioned = urllib.unquote_plus(wp.fragment)
123+
if ' ' in fragmentioned and do_embed:
124+
text = (text + '<blockquote class="auto-mention"><a class="auto-link" href="' +
125+
wmi + '"><cite>' + wp.netloc +'</cite><p>' + fragmentioned
126+
+ '</p></a></blockquote>' + afterlink)
111127
else:
112128
text = (text + '<a class="auto-link" href="' +
113-
wmi + '">' + mi + '</a>' +
129+
wmi + '">' + displayUrl + '</a>' +
114130
afterlink)
115131
else:
116132
text = text + mi

‎cassistest.py

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# -*- coding: utf-8 -*-
2+
13
'''unit tests for cassis.py '''
24

35
import cassis
@@ -31,6 +33,16 @@ def test_checknourl(self):
3133
def test_checknamewithttp(self):
3234
self.assertEqual(cassis.auto_link('http://kevinmarks.com'), '<a class="auto-link" href="http://kevinmarks.com">http://kevinmarks.com</a>')
3335

36+
class CheckShortURL(unittest.TestCase):
37+
def test_checkname(self):
38+
self.assertEqual(cassis.auto_link('kevinmarks.com',maxUrlLength=10), '<a class="auto-link" href="http://kevinmarks.com">kevinmarks…</a>')
39+
self.assertEqual(cassis.auto_link('kevinmarks.com',maxUrlLength=20), '<a class="auto-link" href="http://kevinmarks.com">kevinmarks.com</a>')
40+
self.assertEqual(cassis.auto_link('kevinmarks.com',maxUrlLength=14), '<a class="auto-link" href="http://kevinmarks.com">kevinmarks.com</a>')
41+
self.assertEqual(cassis.auto_link('http://kevinmarks.com',maxUrlLength=10), '<a class="auto-link" href="http://kevinmarks.com">kevinmarks…</a>')
42+
self.assertEqual(cassis.auto_link('https://kevinmarks.com',maxUrlLength=20), '<a class="auto-link" href="https://kevinmarks.com">kevinmarks.com</a>')
43+
self.assertEqual(cassis.auto_link('http://kevinmarks.com',maxUrlLength=14), '<a class="auto-link" href="http://kevinmarks.com">kevinmarks.com</a>')
44+
45+
3446
class CheckEmail(unittest.TestCase):
3547
def test_checkemail(self):
3648
self.assertEqual(cassis.auto_link('kevinmarks@gmail.com'), 'kevinmarks@gmail.com')
@@ -49,6 +61,10 @@ class CheckMixed(unittest.TestCase):
4961
def test_checkgif(self):
5062
self.assertEqual(cassis.auto_link('see kevinmarks.com and kevinmarks.com/km.jpg and kevinmarks.com'), 'see <a class="auto-link" href="http://kevinmarks.com">kevinmarks.com</a> and <a class="auto-link" href="http://kevinmarks.com/km.jpg">kevinmarks.com/km.jpg</a> and <a class="auto-link" href="http://kevinmarks.com">kevinmarks.com</a>')
5163

64+
class CheckFragmentions(unittest.TestCase):
65+
def test_checkspacestyle(self):
66+
self.assertEqual(cassis.auto_link('“In the case of digital content, the artifact, once created and published, is not static.” https://kartikprabhu.com/article/marginalia#In%20the%20case%20of%20digital%20content,%20the%20artifact,%20once%20created%20and%20published,%20is%20not%20static.',do_embed=True),'“In the case of digital content, the artifact, once created and published, is not static.” <blockquote class="auto-mention"><a class="auto-link" href="https://kartikprabhu.com/article/marginalia#In%20the%20case%20of%20digital%20content,%20the%20artifact,%20once%20created%20and%20published,%20is%20not%20static"><cite>kartikprabhu.com</cite><p>In the case of digital content, the artifact, once created and published, is not static</p></a></blockquote>.')
67+
5268
if __name__ == '__main__':
5369
unittest.main()
5470

0 commit comments

Comments
 (0)