annotate gpp/core/markup.py @ 399:24f1230f3ee3

Fixing #193; reduce news query counts by grabbing tags and comment counts in bulk. Increased news items per page from 5 to 10.
author Brian Neal <bgneal@gmail.com>
date Sat, 26 Mar 2011 03:08:05 +0000
parents f54bf3b3bece
children
rev   line source
bgneal@124 1 """
bgneal@128 2 Markup related utitlities useful for the entire project.
bgneal@124 3 """
bgneal@124 4 import markdown as _markdown
bgneal@124 5 from django.utils.encoding import force_unicode
bgneal@124 6
bgneal@211 7 from smiley import SmilifyMarkdown
bgneal@124 8
bgneal@124 9 class Markdown(object):
bgneal@128 10 """
bgneal@128 11 This is a thin wrapper around the Markdown class which deals with the
bgneal@128 12 differences in Markdown versions on the production and development server.
bgneal@352 13 This code was inspired by the code in
bgneal@128 14 django/contrib/markup/templatetags/markup.py.
bgneal@128 15 Currently, we only have to worry about Markdown 1.6b and 2.0.
bgneal@128 16 """
bgneal@124 17 def __init__(self, safe_mode='escape'):
bgneal@124 18 # Unicode support only in markdown v1.7 or above. Version_info
bgneal@124 19 # exists only in markdown v1.6.2rc-2 or above.
bgneal@124 20 self.unicode_support = getattr(_markdown, "version_info", None) >= (1, 7)
bgneal@353 21 self.md = _markdown.Markdown(safe_mode=safe_mode,
bgneal@356 22 extensions=['urlize', 'nl2br', 'del'])
bgneal@124 23
bgneal@124 24 def convert(self, s):
bgneal@124 25 if self.unicode_support:
bgneal@124 26 return self.md.convert(force_unicode(s))
bgneal@124 27 else:
bgneal@124 28 return force_unicode(self.md.convert(s))
bgneal@124 29
bgneal@124 30
bgneal@124 31 def markdown(s):
bgneal@128 32 """
bgneal@128 33 A convenience function for one-off markdown jobs.
bgneal@128 34 """
bgneal@124 35 md = Markdown()
bgneal@124 36 return md.convert(s)
bgneal@128 37
bgneal@128 38
bgneal@128 39 class SiteMarkup(object):
bgneal@128 40 """
bgneal@128 41 This class provides site markup by combining markdown and
bgneal@128 42 our own smiley markup.
bgneal@128 43 """
bgneal@128 44 def __init__(self):
bgneal@128 45 self.md = Markdown()
bgneal@211 46 self.smiley = SmilifyMarkdown()
bgneal@128 47
bgneal@128 48 def convert(self, s):
bgneal@211 49 return self.md.convert(self.smiley.convert(s))
bgneal@128 50
bgneal@128 51
bgneal@128 52 def site_markup(s):
bgneal@128 53 """
bgneal@128 54 Convenience function for one-off site markup jobs.
bgneal@128 55 """
bgneal@128 56 sm = SiteMarkup()
bgneal@128 57 return sm.convert(s)