/usr/share/pyshared/gluon/contrib/autolinks.py is in python-gluon 2.12.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | """
Developed by Massimo Di Pierro
Released under the web2py license (LGPL)
What does it do?
if html is a variable containing HTML text and urls in the text, when you call
html = expend_html(html)
it automatically converts the url to links but when possible it embeds the object being linked.
In particular it can embed images, videos, audio files, documents (it uses the google code player),
as well as pages to a oembed service.
Google Doc Support
==================
Microsoft Word (.DOC, .DOCX)
Microsoft Excel (.XLS and .XLSX)
Microsoft PowerPoint 2007 / 2010 (.PPTX)
Apple Pages (.PAGES)
Adobe PDF (.PDF)
Adobe Illustrator (.AI)
Adobe Photoshop (.PSD)
Autodesk AutoCad (.DXF)
Scalable Vector Graphics (.SVG)
PostScript (.EPS, .PS)
TrueType (.TTF)
XML Paper Specification (.XPS)
Oembed Support
==============
flickr.com
youtube.com
hulu.com
vimeo.com
slideshare.net
qik.com
polleverywhere.com
wordpress.com
revision3.com
viddler.com
"""
import re
import cgi
import sys
from simplejson import loads
import urllib
import uuid
try:
from BeautifulSoup import BeautifulSoup, Comment
have_soup = True
except ImportError:
have_soup = False
regex_link = re.compile('https?://\S+')
EMBED_MAPS = [
(re.compile('http://\S*?flickr.com/\S*'),
'http://www.flickr.com/services/oembed/'),
(re.compile('http://\S*.youtu(\.be|be\.com)/watch\S*'),
'http://www.youtube.com/oembed'),
(re.compile('http://www.hulu.com/watch/\S*'),
'http://www.hulu.com/api/oembed.json'),
(re.compile('http://vimeo.com/\S*'),
'http://vimeo.com/api/oembed.json'),
(re.compile('http://www.slideshare.net/[^\/]+/\S*'),
'http://www.slideshare.net/api/oembed/2'),
(re.compile('http://qik.com/\S*'),
'http://qik.com/api/oembed.json'),
(re.compile('http://www.polleverywhere.com/\w+/\S+'),
'http://www.polleverywhere.com/services/oembed/'),
(re.compile('http://\S+.wordpress.com/\S+'),
'http://public-api.wordpress.com/oembed/'),
(re.compile('http://*.revision3.com/\S+'),
'http://revision3.com/api/oembed/'),
(re.compile('http://\S+.viddler.com/\S+'),
'http://lab.viddler.com/services/oembed/'),
]
def image(url):
return '<img src="%s" style="max-width:100%%"/>' % url
def audio(url):
return '<audio controls="controls" style="max-width:100%%"><source src="%s" /></audio>' % url
def video(url):
return '<video controls="controls" style="max-width:100%%"><source src="%s" /></video>' % url
def googledoc_viewer(url):
return '<iframe src="http://docs.google.com/viewer?url=%s&embedded=true" style="max-width:100%%"></iframe>' % urllib.quote(url)
def web2py_component(url):
code = str(uuid.uuid4())
return '<div id="%s"></div><script>\nweb2py_component("%s","%s");\n</script>' % (code, url, code)
EXTENSION_MAPS = {
'png': image,
'gif': image,
'jpg': image,
'jpeg': image,
'wav': audio,
'ogg': audio,
'mp3': audio,
'mov': video,
'mpe': video,
'mp4': video,
'mpg': video,
'mpg2': video,
'mpeg': video,
'mpeg4': video,
'movie': video,
'wmv': video,
'load': web2py_component,
'pdf': googledoc_viewer,
'doc': googledoc_viewer,
'docx': googledoc_viewer,
'ppt': googledoc_viewer,
'pptx': googledoc_viewer,
'xls': googledoc_viewer,
'xlsx': googledoc_viewer,
'pages': googledoc_viewer,
'ai': googledoc_viewer,
'psd': googledoc_viewer,
'xdf': googledoc_viewer,
'svg': googledoc_viewer,
'ttf': googledoc_viewer,
'xps': googledoc_viewer,
}
class VimeoURLOpener(urllib.FancyURLopener):
"Vimeo blocks the urllib user agent for some reason"
version = "Mozilla/4.0"
urllib._urlopener = VimeoURLOpener()
def oembed(url):
for k, v in EMBED_MAPS:
if k.match(url):
oembed = v + '?format=json&url=' + cgi.escape(url)
try:
data = urllib.urlopen(oembed).read()
return loads(data) # json!
except:
pass
return {}
def extension(url):
return url.split('?')[0].split('.')[-1].lower()
def expand_one(url, cdict):
# try ombed but first check in cache
if '@' in url and not '://'in url:
return '<a href="mailto:%s">%s</a>' % (url, url)
if cdict and url in cdict:
r = cdict[url]
else:
r = oembed(url)
if isinstance(cdict, dict):
cdict[url] = r
# if oembed service
if 'html' in r:
html = r['html'].encode('utf8')
if html.startswith('<object'):
return '<embed style="max-width:100%%">%s</embed>' % html
else:
return html
elif 'url' in r:
url = r['url'].encode('utf8')
# embed images, video, audio files
ext = extension(url)
if ext in EXTENSION_MAPS:
return EXTENSION_MAPS[ext](url)
# else regular link
return '<a href="%(u)s">%(u)s</a>' % dict(u=url)
def expand_html(html, cdict=None):
if not have_soup:
raise RuntimeError("Missing BeautifulSoup")
soup = BeautifulSoup(html)
comments = soup.findAll(text=lambda text: isinstance(text, Comment))
[comment.extract() for comment in comments]
for txt in soup.findAll(text=True):
if not txt.parent.name in ('a', 'script', 'pre', 'code', 'embed', 'object', 'audio', 'video'):
ntxt = regex_link.sub(
lambda match: expand_one(match.group(0), cdict), txt)
txt.replaceWith(BeautifulSoup(ntxt))
return str(soup)
def test():
example = """
<h3>Fringilla nisi parturient nullam</h3>
<p>http://www.youtube.com/watch?v=IWBFiI5RrA0</p>
<p>http://www.web2py.com/examples/static/images/logo_bw.png</p>
<p>http://www.web2py.com/examples/default/index.load</p>
<p>http://www.web2py.com/examples/static/web2py_manual_cutl.pdf</p>
<p>Elementum sodales est varius magna leo sociis erat. Nascetur pretium non
ultricies gravida. Condimentum at nascetur tempus. Porttitor viverra ipsum
accumsan neque aliquet. Ultrices vestibulum tempor quisque eget sem eget.
Ornare malesuada tempus dolor dolor magna consectetur. Nisl dui non curabitur
laoreet tortor.</p>
"""
return expand_html(example)
if __name__ == "__main__":
if len(sys.argv) > 1:
print expand_html(open(sys.argv[1]).read())
else:
print test()
|