~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/browser/stringformatter.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-11-25 12:34:41 UTC
  • mfrom: (14339.3.11 391780-markdown)
  • Revision ID: launchpad@pqm.canonical.com-20111125123441-5ltnp6fjn34q0kmu
[r=rvb][bug=391780][incr] Markdown markup in project and user home
        pages

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import re
23
23
from lxml import html
24
24
from xml.sax.saxutils import unescape as xml_unescape
 
25
import markdown
25
26
 
26
27
from zope.component import getUtility
27
28
from zope.interface import implements
39
40
    re_email_address,
40
41
    obfuscate_email,
41
42
    )
 
43
from lp.services.features import (
 
44
    getFeatureFlag,
 
45
    )
42
46
 
43
47
 
44
48
def escape(text, quote=True):
556
560
        0*(?P<bugnum>\d+)
557
561
      ) |
558
562
      (?P<faq>
559
 
        \bfaq(?:[\s=-]|<br\s*/>)*(?:\#|item|number?|num\.?|no\.?)?(?:[\s=-]|<br\s*/>)*
 
563
        \bfaq(?:[\s=-]|<br\s*/>)*(?:\#|item|number?|num\.?|no\.?)?
 
564
        (?:[\s=-]|<br\s*/>)*
560
565
        0*(?P<faqnum>\d+)
561
566
      ) |
562
567
      (?P<oops>
982
987
        url = root_url + self._stringtoformat
983
988
        return '<a href="%s">%s</a>' % (url, self._stringtoformat)
984
989
 
 
990
    def markdown(self):
 
991
        if getFeatureFlag('markdown.enabled'):
 
992
            return format_markdown(self._stringtoformat)
 
993
        else:
 
994
            return self.text_to_html()
 
995
 
985
996
    def traverse(self, name, furtherPath):
986
997
        if name == 'nl_to_br':
987
998
            return self.nl_to_br()
991
1002
            return self.lower()
992
1003
        elif name == 'break-long-words':
993
1004
            return self.break_long_words()
 
1005
        elif name == 'markdown':
 
1006
            return self.markdown()
994
1007
        elif name == 'text-to-html':
995
1008
            return self.text_to_html()
996
1009
        elif name == 'text-to-html-with-target':
1033
1046
            return self.oops_id()
1034
1047
        else:
1035
1048
            raise TraversalError(name)
 
1049
 
 
1050
 
 
1051
def format_markdown(text):
 
1052
    """Return html form of marked-up text."""
 
1053
    # This returns whole paragraphs (in p tags), similarly to text_to_html.
 
1054
    md = markdown.Markdown(
 
1055
        safe_mode='escape',
 
1056
        extensions=[
 
1057
            'tables',
 
1058
            'nl2br',
 
1059
            ])
 
1060
    return md.convert(text)  # How easy was that?