Skip to content
Browse files

linkify, utf-8 for humans.txt

  • Loading branch information...
1 parent cb3a33a commit 5da905981f25fd35325794cde192807a7c28d135 @mimecuvalo committed Oct 6, 2011
Showing with 26 additions and 3 deletions.
  1. +5 −0 controllers/humans.py
  2. +10 −0 controllers/view.py
  3. +2 −1 helloworld.py
  4. +9 −2 logic/users.py
View
5 controllers/humans.py
@@ -0,0 +1,5 @@
+import tornado.web
+
+class HumansTxtHandler(tornado.web.StaticFileHandler):
+ def set_extra_headers(self, path):
+ self.set_header("Content-Type", "text/plain; charset=UTF-8")
View
10 controllers/view.py
@@ -3,6 +3,8 @@
import re
import urllib
+from BeautifulSoup import BeautifulSoup
+import tornado.escape
import tornado.web
from base import BaseHandler
@@ -353,6 +355,14 @@ def save_content(self, content, content_url, new):
content.code = self.get_argument('code', "")
content.view = self.get_argument('view', "")
+ # linkify
+ soup = BeautifulSoup(content.view)
+ for text in soup.findAll(text=True):
+ if text.parent.name != 'a':
+ text.replaceWith(tornado.escape.linkify(text, shorten=True, extra_params='rel="nofollow"'))
+
+ content.view = soup.renderContents()
+
template = self.get_argument('template', "")
if template:
View
3 helloworld.py
@@ -25,6 +25,7 @@
import controllers.feed
import controllers.foaf
import controllers.host_meta
+import controllers.humans
import controllers.media
import controllers.opensearch
import controllers.private
@@ -107,7 +108,7 @@
(prefix + r"/data_liberation\.zip", controllers.data_liberation.DataLiberationDownloadHandler, {"path": "/tmp"}),
(prefix + r"/?[^/]+/feed", controllers.feed.FeedHandler),
(prefix + r"/?[^/]+/foaf", controllers.foaf.FoafHandler),
- (prefix + r"/(humans\.txt)", tornado.web.StaticFileHandler, {"path": settings['static_path']}),
+ (prefix + r"/(humans\.txt)", controllers.humans.HumansTxtHandler, {"path": settings['static_path']}),
(prefix + r"/login", controllers.auth.AuthHandler),
(prefix + r"/logout", controllers.auth.AuthLogoutHandler),
(prefix + r"/media", controllers.media.MediaHandler),
View
11 logic/users.py
@@ -81,8 +81,15 @@ def sanitize(value):
soup = BeautifulSoup(value)
for tag in soup.findAll(True):
- if tag.name not in VALID_TAGS:
- tag.hidden = True
+ if tag.name not in VALID_TAGS:
+ tag.hidden = True
+ elif tag.name == 'a':
+ tag['rel'] = 'nofollow'
+
+ # linkify
+ for text in soup.findAll(text=True):
+ if text.parent.name != 'a':
+ text.replaceWith(tornado.escape.linkify(text, shorten=True, extra_params='rel="nofollow"'))
return soup.renderContents()

0 comments on commit 5da9059

Please sign in to comment.
Something went wrong with that request. Please try again.