Mercurial > public > sg101
annotate core/markup.py @ 848:32ebe22f0cad
For issue #79 update to Markdown 2.5.1.
This commit follows new Markdown conventions for extensions.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 28 Oct 2014 19:33:14 -0500 |
parents | 7429c98c8ece |
children | ff645a692791 |
rev | line source |
---|---|
bgneal@124 | 1 """ |
bgneal@128 | 2 Markup related utitlities useful for the entire project. |
bgneal@848 | 3 |
bgneal@124 | 4 """ |
bgneal@124 | 5 import markdown as _markdown |
bgneal@124 | 6 from django.utils.encoding import force_unicode |
bgneal@124 | 7 |
bgneal@211 | 8 from smiley import SmilifyMarkdown |
bgneal@848 | 9 from core.mdexts.urlize import UrlizeExtension |
bgneal@848 | 10 from core.mdexts.deleted import DelExtension |
bgneal@124 | 11 |
bgneal@124 | 12 class Markdown(object): |
bgneal@128 | 13 """ |
bgneal@792 | 14 This is a thin wrapper around the Markdown class. |
bgneal@792 | 15 |
bgneal@128 | 16 """ |
bgneal@124 | 17 def __init__(self, safe_mode='escape'): |
bgneal@353 | 18 self.md = _markdown.Markdown(safe_mode=safe_mode, |
bgneal@848 | 19 extensions=[ |
bgneal@848 | 20 UrlizeExtension(), |
bgneal@848 | 21 'markdown.extensions.nl2br', |
bgneal@848 | 22 DelExtension(), |
bgneal@848 | 23 ]) |
bgneal@124 | 24 |
bgneal@124 | 25 def convert(self, s): |
bgneal@686 | 26 return self.md.convert(force_unicode(s)) |
bgneal@124 | 27 |
bgneal@124 | 28 |
bgneal@124 | 29 def markdown(s): |
bgneal@128 | 30 """ |
bgneal@128 | 31 A convenience function for one-off markdown jobs. |
bgneal@128 | 32 """ |
bgneal@124 | 33 md = Markdown() |
bgneal@124 | 34 return md.convert(s) |
bgneal@128 | 35 |
bgneal@128 | 36 |
bgneal@128 | 37 class SiteMarkup(object): |
bgneal@128 | 38 """ |
bgneal@128 | 39 This class provides site markup by combining markdown and |
bgneal@128 | 40 our own smiley markup. |
bgneal@792 | 41 |
bgneal@792 | 42 The relative_urls parameter controls whether the smileys are generated with |
bgneal@792 | 43 relative or absolute URLs. |
bgneal@792 | 44 |
bgneal@128 | 45 """ |
bgneal@792 | 46 def __init__(self, relative_urls=True): |
bgneal@128 | 47 self.md = Markdown() |
bgneal@792 | 48 self.smiley = SmilifyMarkdown(relative_urls=relative_urls) |
bgneal@128 | 49 |
bgneal@128 | 50 def convert(self, s): |
bgneal@211 | 51 return self.md.convert(self.smiley.convert(s)) |
bgneal@128 | 52 |
bgneal@128 | 53 |
bgneal@792 | 54 def site_markup(s, relative_urls=True): |
bgneal@128 | 55 """ |
bgneal@128 | 56 Convenience function for one-off site markup jobs. |
bgneal@128 | 57 """ |
bgneal@792 | 58 sm = SiteMarkup(relative_urls=relative_urls) |
bgneal@128 | 59 return sm.convert(s) |