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@128: This is a thin wrapper around the Markdown class which deals with the bgneal@128: differences in Markdown versions on the production and development server. bgneal@128: This code was inspired by the code in bgneal@128: django/contrib/markup/templatetags/markup.py. bgneal@128: Currently, we only have to worry about Markdown 1.6b and 2.0. bgneal@128: """ bgneal@124: def __init__(self, safe_mode='escape'): bgneal@124: # Unicode support only in markdown v1.7 or above. Version_info bgneal@124: # exists only in markdown v1.6.2rc-2 or above. bgneal@124: self.unicode_support = getattr(_markdown, "version_info", None) >= (1, 7) bgneal@124: self.md = _markdown.Markdown(safe_mode=safe_mode) bgneal@124: bgneal@124: def convert(self, s): bgneal@124: if self.unicode_support: bgneal@124: return self.md.convert(force_unicode(s)) bgneal@124: else: bgneal@124: return force_unicode(self.md.convert(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@128: """ bgneal@128: def __init__(self): bgneal@128: self.md = Markdown() bgneal@211: self.smiley = SmilifyMarkdown() bgneal@128: bgneal@128: def convert(self, s): bgneal@211: return self.md.convert(self.smiley.convert(s)) bgneal@128: bgneal@128: bgneal@128: def site_markup(s): bgneal@128: """ bgneal@128: Convenience function for one-off site markup jobs. bgneal@128: """ bgneal@128: sm = SiteMarkup() bgneal@128: return sm.convert(s)