bgneal@124: """ bgneal@128: Markup related utitlities useful for the entire project. bgneal@124: """ bgneal@124: import markdown as _markdown bgneal@124: from django.utils.encoding import force_unicode bgneal@124: bgneal@211: from smiley import SmilifyMarkdown 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@124: def __init__(self, safe_mode='escape'): bgneal@353: self.md = _markdown.Markdown(safe_mode=safe_mode, bgneal@356: extensions=['urlize', 'nl2br', 'del']) bgneal@124: bgneal@124: def convert(self, s): bgneal@686: return 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)