summaryrefslogtreecommitdiffstats
path: root/askbot/utils
diff options
context:
space:
mode:
authorEvgeny Fadeev <evgeny.fadeev@gmail.com>2012-09-27 15:09:37 -0400
committerEvgeny Fadeev <evgeny.fadeev@gmail.com>2012-09-27 15:09:37 -0400
commit1e47fb23634dbdf406a3e8893120793b7e065da5 (patch)
tree24a8ae6aa62fa51b374c63c3cf8b4b842a78d1ec /askbot/utils
parent56f56f2077d4e4f5fc149eb222e33801ad036033 (diff)
downloadaskbot-1e47fb23634dbdf406a3e8893120793b7e065da5.tar.gz
askbot-1e47fb23634dbdf406a3e8893120793b7e065da5.tar.bz2
askbot-1e47fb23634dbdf406a3e8893120793b7e065da5.zip
refactored the absolutize_urls function and the with_settings decorator for the test cases
Diffstat (limited to 'askbot/utils')
-rw-r--r--askbot/utils/html.py18
1 files changed, 18 insertions, 0 deletions
diff --git a/askbot/utils/html.py b/askbot/utils/html.py
index 44e3f1df..49eddee2 100644
--- a/askbot/utils/html.py
+++ b/askbot/utils/html.py
@@ -6,6 +6,7 @@ import htmlentitydefs
from urlparse import urlparse
from django.core.urlresolvers import reverse
from django.utils.html import escape
+from askbot.conf import settings as askbot_settings
class HTMLSanitizerMixin(sanitizer.HTMLSanitizerMixin):
acceptable_elements = ('a', 'abbr', 'acronym', 'address', 'b', 'big',
@@ -43,6 +44,23 @@ class HTMLSanitizer(tokenizer.HTMLTokenizer, HTMLSanitizerMixin):
if token:
yield token
+def absolutize_urls(html):
+ """turns relative urls in <img> and <a> tags to absolute,
+ starting with the ``askbot_settings.APP_URL``"""
+ #temporal fix for bad regex with wysiwyg editor
+ url_re1 = re.compile(r'(?P<prefix><img[^<]+src=)"(?P<url>/[^"]+)"', re.I)
+ url_re2 = re.compile(r"(?P<prefix><img[^<]+src=)'(?P<url>/[^']+)'", re.I)
+ url_re3 = re.compile(r'(?P<prefix><a[^<]+href=)"(?P<url>/[^"]+)"', re.I)
+ url_re4 = re.compile(r"(?P<prefix><a[^<]+href=)'(?P<url>/[^']+)'", re.I)
+ img_replacement = '\g<prefix>"%s/\g<url>" style="max-width:500px;"' % askbot_settings.APP_URL
+ replacement = '\g<prefix>"%s\g<url>"' % askbot_settings.APP_URL
+ html = url_re1.sub(img_replacement, html)
+ html= url_re2.sub(img_replacement, html)
+ html = url_re3.sub(replacement, html)
+ #temporal fix for bad regex with wysiwyg editor
+ return url_re4.sub(replacement, html).replace('%s//' % askbot_settings.APP_URL,
+ '%s/' % askbot_settings.APP_URL)
+
def sanitize_html(html):
"""Sanitizes an HTML fragment."""
p = html5lib.HTMLParser(tokenizer=HTMLSanitizer,