X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/1a949f7c97dc2f34c135f5cdf088df2927d3d652..e1bb6d86117346dc3af5a0d3cdb11523860873b1:/forum/utils/html.py diff --git a/forum/utils/html.py b/forum/utils/html.py index 25a74a4..70fe1b3 100644 --- a/forum/utils/html.py +++ b/forum/utils/html.py @@ -1,6 +1,9 @@ """Utilities for working with HTML.""" import html5lib from html5lib import sanitizer, serializer, tokenizer, treebuilders, treewalkers +from forum.utils.html2text import HTML2Text +from django.template import mark_safe +from forum import settings class HTMLSanitizerMixin(sanitizer.HTMLSanitizerMixin): acceptable_elements = ('a', 'abbr', 'acronym', 'address', 'b', 'big', @@ -26,11 +29,6 @@ class HTMLSanitizerMixin(sanitizer.HTMLSanitizerMixin): allowed_svg_properties = () class HTMLSanitizer(tokenizer.HTMLTokenizer, HTMLSanitizerMixin): - def __init__(self, stream, encoding=None, parseMeta=True, useChardet=True, - lowercaseElementName=True, lowercaseAttrName=True): - tokenizer.HTMLTokenizer.__init__(self, stream, encoding, parseMeta, - useChardet, lowercaseElementName, - lowercaseAttrName) def __iter__(self): for token in tokenizer.HTMLTokenizer.__iter__(self): @@ -49,3 +47,25 @@ def sanitize_html(html): quote_attr_values=True) output_generator = s.serialize(stream) return u''.join(output_generator) + + +def html2text(s, ignore_tags=(), indent_width=4, page_width=80): + ignore_tags = [t.lower() for t in ignore_tags] + parser = HTML2Text(ignore_tags, indent_width, page_width) + parser.feed(s) + parser.close() + parser.generate() + return mark_safe(parser.result) + +def buildtag(name, content, **attrs): + return mark_safe('<%s %s>%s' % (name, " ".join('%s="%s"' % i for i in attrs.items()), str(content), name)) + +def hyperlink(url, title, **attrs): + return mark_safe('%s' % (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) + + + +