2012-10-15 22:03:50 +02:00
|
|
|
import markdown
|
2012-10-22 05:06:28 +02:00
|
|
|
import logging
|
|
|
|
import traceback
|
2012-10-25 21:38:47 +02:00
|
|
|
import re
|
2012-10-15 22:03:50 +02:00
|
|
|
|
2012-10-20 05:34:14 +02:00
|
|
|
from zephyr.lib.avatar import gravatar_hash
|
2012-11-19 18:31:03 +01:00
|
|
|
from zephyr.lib.bugdown import codehilite, fenced_code
|
2012-10-17 04:42:19 +02:00
|
|
|
|
|
|
|
class Gravatar(markdown.inlinepatterns.Pattern):
|
|
|
|
def handleMatch(self, match):
|
|
|
|
img = markdown.util.etree.Element('img')
|
|
|
|
img.set('class', 'message_body_gravatar img-rounded')
|
|
|
|
img.set('src', 'https://secure.gravatar.com/avatar/%s?d=identicon&s=30'
|
2012-10-22 02:15:44 +02:00
|
|
|
% (gravatar_hash(match.group('email')),))
|
2012-10-17 04:42:19 +02:00
|
|
|
return img
|
|
|
|
|
2012-10-22 02:32:18 +02:00
|
|
|
class AutoLink(markdown.inlinepatterns.Pattern):
|
|
|
|
def handleMatch(self, match):
|
|
|
|
url = match.group('url')
|
|
|
|
a = markdown.util.etree.Element('a')
|
|
|
|
a.set('href', url)
|
|
|
|
a.text = url
|
|
|
|
return a
|
|
|
|
|
2012-11-02 18:25:37 +01:00
|
|
|
class UListProcessor(markdown.blockprocessors.OListProcessor):
|
|
|
|
""" Process unordered list blocks.
|
|
|
|
|
|
|
|
Based on markdown.blockprocessors.UListProcessor, but does not accept
|
|
|
|
'+' as a bullet character."""
|
|
|
|
|
|
|
|
TAG = 'ul'
|
|
|
|
RE = re.compile(r'^[ ]{0,3}[*-][ ]+(.*)')
|
|
|
|
|
2012-10-16 17:35:58 +02:00
|
|
|
class Bugdown(markdown.Extension):
|
|
|
|
def extendMarkdown(self, md, md_globals):
|
2012-10-22 02:40:58 +02:00
|
|
|
for k in ('image_link', 'image_reference', 'automail', 'autolink'):
|
2012-10-22 02:35:36 +02:00
|
|
|
del md.inlinePatterns[k]
|
|
|
|
|
2012-11-02 18:25:37 +01:00
|
|
|
for k in ('hashheader', 'setextheader', 'olist', 'ulist'):
|
2012-10-22 02:35:36 +02:00
|
|
|
del md.parser.blockprocessors[k]
|
2012-10-16 17:35:58 +02:00
|
|
|
|
2012-11-02 18:25:37 +01:00
|
|
|
md.parser.blockprocessors.add('ulist', UListProcessor(md.parser), '>hr')
|
|
|
|
|
2012-10-22 02:15:44 +02:00
|
|
|
md.inlinePatterns.add('gravatar', Gravatar(r'!gravatar\((?P<email>[^)]*)\)'), '_begin')
|
2012-10-17 04:42:19 +02:00
|
|
|
|
2012-10-22 02:32:18 +02:00
|
|
|
# A link starts after whitespace and continues to the next whitespace.
|
2012-10-22 23:30:05 +02:00
|
|
|
link_regex = r'\b(?P<url>https?://[^\s[\](){}<>]+)'
|
2012-10-22 23:47:49 +02:00
|
|
|
md.inlinePatterns.add('autolink', AutoLink(link_regex), '>link')
|
2012-10-22 02:32:18 +02:00
|
|
|
|
2012-10-15 22:03:50 +02:00
|
|
|
# We need to re-initialize the markdown engine every 30 messages
|
|
|
|
# due to some sort of performance leak in the markdown library.
|
|
|
|
MAX_MD_ENGINE_USES = 30
|
|
|
|
|
|
|
|
_md_engine = None
|
|
|
|
_use_count = 0
|
|
|
|
|
2012-10-25 21:38:47 +02:00
|
|
|
# We want to log Markdown parser failures, but shouldn't log the actual input
|
|
|
|
# message for privacy reasons. The compromise is to replace all alphanumeric
|
|
|
|
# characters with 'x'.
|
|
|
|
#
|
|
|
|
# We also use repr() to improve reproducibility, and to escape terminal control
|
|
|
|
# codes, which can do surprisingly nasty things.
|
|
|
|
_privacy_re = re.compile(r'\w', flags=re.UNICODE)
|
|
|
|
def _sanitize_for_log(md):
|
|
|
|
return repr(_privacy_re.sub('x', md))
|
|
|
|
|
2012-10-15 22:03:50 +02:00
|
|
|
def _linkify(match):
|
|
|
|
url = match.group('url')
|
|
|
|
return ' [%s](%s) ' % (url, url)
|
|
|
|
|
|
|
|
def convert(md):
|
|
|
|
"""Convert Markdown to HTML, with Humbug-specific settings and hacks."""
|
|
|
|
global _md_engine, _use_count
|
|
|
|
|
|
|
|
if _md_engine is None:
|
|
|
|
_md_engine = markdown.Markdown(
|
|
|
|
safe_mode = 'escape',
|
2012-10-24 22:02:15 +02:00
|
|
|
output_format = 'html',
|
2012-11-19 18:31:03 +01:00
|
|
|
extensions = ['nl2br',
|
2012-11-19 17:34:37 +01:00
|
|
|
codehilite.makeExtension(configs=[
|
|
|
|
('force_linenos', False),
|
|
|
|
('guess_lang', False)]),
|
2012-11-19 18:31:03 +01:00
|
|
|
fenced_code.makeExtension(),
|
2012-10-20 05:34:14 +02:00
|
|
|
Bugdown()])
|
2012-10-15 22:03:50 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
html = _md_engine.convert(md)
|
|
|
|
except:
|
|
|
|
# FIXME: Do something more reasonable here!
|
|
|
|
html = '<p>[Humbug note: Sorry, we could not understand the formatting of your message]</p>'
|
2012-10-25 21:38:47 +02:00
|
|
|
logging.getLogger('').error('Exception in Markdown parser: %sInput (sanitized) was: %s'
|
|
|
|
% (traceback.format_exc(), _sanitize_for_log(md)))
|
2012-10-15 22:03:50 +02:00
|
|
|
|
|
|
|
_use_count += 1
|
|
|
|
if _use_count >= MAX_MD_ENGINE_USES:
|
|
|
|
_md_engine = None
|
|
|
|
_use_count = 0
|
|
|
|
|
|
|
|
return html
|