annotate core/markup.py @ 943:cf9918328c64

Haystack tweaks for Django 1.7.7. I had to upgrade to Haystack 2.3.1 to get it to work with Django 1.7.7. I also had to update the Xapian backend. But I ran into problems. On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms are greater than 245 chars (or something) when indexing. So I created a custom field that would simply omit terms greater than 64 chars and used this field everywhere I previously used a CharField. Secondly, the custom search form was broken now. Something changed in the Xapian backend and exact searches stopped working. Fortunately the auto_query (which I was using originally and broke during an upgrade) started working again. So I cut the search form back over to doing an auto_query. I kept the form the same (3 fields) because I didn't want to change the form and I think it's better that way.
author Brian Neal <bgneal@gmail.com>
date Wed, 13 May 2015 20:25:07 -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)