Skip to content

Commit

Permalink
linkify, utf-8 for humans.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
mimecuvalo committed Oct 6, 2011
1 parent cb3a33a commit 5da9059
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions 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")
10 changes: 10 additions & 0 deletions controllers/view.py
Expand Up @@ -3,6 +3,8 @@
import re
import urllib

from BeautifulSoup import BeautifulSoup
import tornado.escape
import tornado.web

from base import BaseHandler
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion helloworld.py
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
11 changes: 9 additions & 2 deletions logic/users.py
Expand Up @@ -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()

Expand Down

0 comments on commit 5da9059

Please sign in to comment.