Mercurial > public > sg101
annotate core/markup.py @ 1201:fe10aea76cbd tip
Add 2023 MP3 compilation links
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 24 Mar 2024 14:50:23 -0500 |
parents | 98d2388b6bb2 |
children |
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@925 | 8 from smiley.utils import SmilifyMarkdown |
bgneal@848 | 9 from core.mdexts.urlize import UrlizeExtension |
bgneal@848 | 10 from core.mdexts.deleted import DelExtension |
bgneal@849 | 11 from core.html import clean_html |
bgneal@849 | 12 |
bgneal@124 | 13 |
bgneal@124 | 14 class Markdown(object): |
bgneal@128 | 15 """ |
bgneal@792 | 16 This is a thin wrapper around the Markdown class. |
bgneal@792 | 17 |
bgneal@128 | 18 """ |
bgneal@849 | 19 def __init__(self): |
bgneal@849 | 20 self.md = _markdown.Markdown(extensions=[ |
bgneal@848 | 21 UrlizeExtension(), |
bgneal@848 | 22 'markdown.extensions.nl2br', |
bgneal@848 | 23 DelExtension(), |
bgneal@848 | 24 ]) |
bgneal@124 | 25 |
bgneal@124 | 26 def convert(self, s): |
bgneal@849 | 27 return clean_html(self.md.convert(force_unicode(s))) |
bgneal@124 | 28 |
bgneal@124 | 29 |
bgneal@124 | 30 def markdown(s): |
bgneal@128 | 31 """ |
bgneal@128 | 32 A convenience function for one-off markdown jobs. |
bgneal@128 | 33 """ |
bgneal@124 | 34 md = Markdown() |
bgneal@124 | 35 return md.convert(s) |
bgneal@128 | 36 |
bgneal@128 | 37 |
bgneal@128 | 38 class SiteMarkup(object): |
bgneal@128 | 39 """ |
bgneal@128 | 40 This class provides site markup by combining markdown and |
bgneal@128 | 41 our own smiley markup. |
bgneal@792 | 42 |
bgneal@792 | 43 The relative_urls parameter controls whether the smileys are generated with |
bgneal@792 | 44 relative or absolute URLs. |
bgneal@792 | 45 |
bgneal@128 | 46 """ |
bgneal@792 | 47 def __init__(self, relative_urls=True): |
bgneal@128 | 48 self.md = Markdown() |
bgneal@792 | 49 self.smiley = SmilifyMarkdown(relative_urls=relative_urls) |
bgneal@128 | 50 |
bgneal@128 | 51 def convert(self, s): |
bgneal@211 | 52 return self.md.convert(self.smiley.convert(s)) |
bgneal@128 | 53 |
bgneal@128 | 54 |
bgneal@792 | 55 def site_markup(s, relative_urls=True): |
bgneal@128 | 56 """ |
bgneal@128 | 57 Convenience function for one-off site markup jobs. |
bgneal@128 | 58 """ |
bgneal@792 | 59 sm = SiteMarkup(relative_urls=relative_urls) |
bgneal@128 | 60 return sm.convert(s) |