]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/utils/html.py
Further fixes for subfolder hosting
[osqa.git] / forum / utils / html.py
index 86a4ef9119cf417f876d1ceb25c7c04460640ba7..3657ef6af0a6ecd6813a9d4eaf35840c76e94060 100644 (file)
@@ -1,8 +1,9 @@
 """Utilities for working with HTML."""
-import html5lib
-from html5lib import sanitizer, serializer, tokenizer, treebuilders, treewalkers
+from html5lib import sanitizer, serializer, tokenizer, treebuilders, treewalkers, HTMLParser
+from urllib import quote_plus
+from django.utils.html import strip_tags
 from forum.utils.html2text import HTML2Text
-from django.template import mark_safe
+from django.utils.safestring import mark_safe
 from forum import settings
 
 class HTMLSanitizerMixin(sanitizer.HTMLSanitizerMixin):
@@ -38,7 +39,7 @@ class HTMLSanitizer(tokenizer.HTMLTokenizer, HTMLSanitizerMixin):
 
 def sanitize_html(html):
     """Sanitizes an HTML fragment."""
-    p = html5lib.HTMLParser(tokenizer=HTMLSanitizer,
+    p = HTMLParser(tokenizer=HTMLSanitizer,
                             tree=treebuilders.getTreeBuilder("dom"))
     dom_tree = p.parseFragment(html)
     walker = treewalkers.getTreeWalker("dom")
@@ -48,6 +49,9 @@ def sanitize_html(html):
     output_generator = s.serialize(stream)
     return u''.join(output_generator)
 
+def cleanup_urls(url):
+    return quote_plus(strip_tags(url))
+
 
 def html2text(s, ignore_tags=(), indent_width=4, page_width=80):
     ignore_tags = [t.lower() for t in ignore_tags]
@@ -64,8 +68,7 @@ def hyperlink(url, title, **attrs):
     return mark_safe('<a href="%s" %s>%s</a>' % (url, " ".join('%s="%s"' % i for i in attrs.items()), title))
 
 def objlink(obj, **attrs):
-    return hyperlink(settings.APP_URL + obj.get_absolute_url(), unicode(obj), **attrs)
-
-    
-
-
+    link = obj.get_absolute_url()
+    if not link.startswith(settings.APP_URL):
+        link = settings.APP_URL + link
+    return hyperlink(link, unicode(obj), **attrs)