bgneal@124: """ bgneal@128: Markup related utitlities useful for the entire project. bgneal@848: bgneal@124: """ bgneal@124: import markdown as _markdown bgneal@124: from django.utils.encoding import force_unicode bgneal@124: bgneal@925: from smiley.utils import SmilifyMarkdown bgneal@848: from core.mdexts.urlize import UrlizeExtension bgneal@848: from core.mdexts.deleted import DelExtension bgneal@849: from core.html import clean_html bgneal@849: bgneal@124: bgneal@124: class Markdown(object): bgneal@128: """ bgneal@792: This is a thin wrapper around the Markdown class. bgneal@792: bgneal@128: """ bgneal@849: def __init__(self): bgneal@849: self.md = _markdown.Markdown(extensions=[ bgneal@848: UrlizeExtension(), bgneal@848: 'markdown.extensions.nl2br', bgneal@848: DelExtension(), bgneal@848: ]) bgneal@124: bgneal@124: def convert(self, s): bgneal@849: return clean_html(self.md.convert(force_unicode(s))) bgneal@124: bgneal@124: bgneal@124: def markdown(s): bgneal@128: """ bgneal@128: A convenience function for one-off markdown jobs. bgneal@128: """ bgneal@124: md = Markdown() bgneal@124: return md.convert(s) bgneal@128: bgneal@128: bgneal@128: class SiteMarkup(object): bgneal@128: """ bgneal@128: This class provides site markup by combining markdown and bgneal@128: our own smiley markup. bgneal@792: bgneal@792: The relative_urls parameter controls whether the smileys are generated with bgneal@792: relative or absolute URLs. bgneal@792: bgneal@128: """ bgneal@792: def __init__(self, relative_urls=True): bgneal@128: self.md = Markdown() bgneal@792: self.smiley = SmilifyMarkdown(relative_urls=relative_urls) bgneal@128: bgneal@128: def convert(self, s): bgneal@211: return self.md.convert(self.smiley.convert(s)) bgneal@128: bgneal@128: bgneal@792: def site_markup(s, relative_urls=True): bgneal@128: """ bgneal@128: Convenience function for one-off site markup jobs. bgneal@128: """ bgneal@792: sm = SiteMarkup(relative_urls=relative_urls) bgneal@128: return sm.convert(s)