Skip to content

Commit

Permalink
URL truncation and fragmention embeds
Browse files Browse the repository at this point in the history
Now takes a maxUrlLength for abbreviated urls, and if it finds a
fragmention when auto_embed is on, it inserts a block quote
  • Loading branch information
kevinmarks committed May 23, 2015
1 parent d1349e0 commit aba9c2f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
30 changes: 23 additions & 7 deletions cassis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# -*- coding: utf-8 -*-

import re
import sys
import os.path
import urllib

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


def auto_link(text, do_embed=False):
def auto_link(text, do_embed=False,maxUrlLength=0):
""" auto_link: param 1: text; param 2: do embeds or not
auto_link is idempotent, works on plain text or typical markup.
"""
Expand Down Expand Up @@ -67,7 +70,14 @@ def auto_link(text, do_embed=False):
hn = wp.netloc
pa = wp.path
ih = wmi.startswith('http')

displayUrl = mi
if maxUrlLength:
displayUrl = wp.netloc+wp.path
if wp.query:
displayUrl = displayUrl + '?'+wp.query
if len(displayUrl)> maxUrlLength:
shortUrl = unicode(displayUrl[:maxUrlLength])+u'…'
displayUrl = shortUrl.encode('utf-8')
if (fe and
(fe == '.jpeg' or fe == '.jpg' or fe == '.png' or
fe == '.gif' or fe == '.svg')):
Expand All @@ -81,7 +91,7 @@ def auto_link(text, do_embed=False):
wmi, '"></video></a>' + afterlink)
elif ih and hn == 'vimeo.com' and pa[1:].isdigit():
text = (text + '<a class="auto-link" href="' +
wmi + '">' + mi + '</a> <iframe class="vimeo-player auto-link figure" width="480" height="385" style="border:0" src="' + prot + '//player.vimeo.com/video/' +
wmi + '">' + displayUrl + '</a> <iframe class="vimeo-player auto-link figure" width="480" height="385" style="border:0" src="' + prot + '//player.vimeo.com/video/' +
pa[1:] + '"></iframe>' + afterlink)
elif (hn == 'youtu.be' or
((hn == 'youtube.com' or hn == 'www.youtube.com')
Expand All @@ -93,24 +103,30 @@ def auto_link(text, do_embed=False):
yvid = mi[offs + 8:].split('&', 1)[0]

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

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

elif wp.fragment:
fragmentioned = urllib.unquote_plus(wp.fragment)
if ' ' in fragmentioned and do_embed:
text = (text + '<blockquote class="auto-mention"><a class="auto-link" href="' +
wmi + '"><cite>' + wp.netloc +'</cite><p>' + fragmentioned
+ '</p></a></blockquote>' + afterlink)
else:
text = (text + '<a class="auto-link" href="' +
wmi + '">' + mi + '</a>' +
wmi + '">' + displayUrl + '</a>' +
afterlink)
else:
text = text + mi
Expand Down
16 changes: 16 additions & 0 deletions cassistest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

'''unit tests for cassis.py '''

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

class CheckShortURL(unittest.TestCase):
def test_checkname(self):
self.assertEqual(cassis.auto_link('kevinmarks.com',maxUrlLength=10), '<a class="auto-link" href="http://kevinmarks.com">kevinmarks…</a>')
self.assertEqual(cassis.auto_link('kevinmarks.com',maxUrlLength=20), '<a class="auto-link" href="http://kevinmarks.com">kevinmarks.com</a>')
self.assertEqual(cassis.auto_link('kevinmarks.com',maxUrlLength=14), '<a class="auto-link" href="http://kevinmarks.com">kevinmarks.com</a>')
self.assertEqual(cassis.auto_link('http://kevinmarks.com',maxUrlLength=10), '<a class="auto-link" href="http://kevinmarks.com">kevinmarks…</a>')
self.assertEqual(cassis.auto_link('https://kevinmarks.com',maxUrlLength=20), '<a class="auto-link" href="https://kevinmarks.com">kevinmarks.com</a>')
self.assertEqual(cassis.auto_link('http://kevinmarks.com',maxUrlLength=14), '<a class="auto-link" href="http://kevinmarks.com">kevinmarks.com</a>')


class CheckEmail(unittest.TestCase):
def test_checkemail(self):
self.assertEqual(cassis.auto_link('kevinmarks@gmail.com'), 'kevinmarks@gmail.com')
Expand All @@ -49,6 +61,10 @@ class CheckMixed(unittest.TestCase):
def test_checkgif(self):
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>')

class CheckFragmentions(unittest.TestCase):
def test_checkspacestyle(self):
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>.')

if __name__ == '__main__':
unittest.main()

0 comments on commit aba9c2f

Please sign in to comment.