From efa334de39c027ccccaefe8771d0822462126f7a Mon Sep 17 00:00:00 2001 From: posativ Date: Sat, 20 Oct 2012 17:55:29 +0200 Subject: [PATCH] markdown support (using misaka) --- isso/comment.py | 8 ++++++++ isso/utils.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/isso/comment.py b/isso/comment.py index 820fa1b..fa0bbb7 100644 --- a/isso/comment.py +++ b/isso/comment.py @@ -21,6 +21,7 @@ def create(app, environ, request, path): except ValueError: return abort(400) + rv.text = utils.markdown(rv.text) response = Response(json.dumps(rv), 201, content_type='application/json') response.set_cookie('session', app.signer.dumps([path, rv.id]), max_age=app.MAX_AGE) return response @@ -31,6 +32,13 @@ def get(app, environ, request, path, id=None): rv = list(app.db.retrieve(path)) if id is None else app.db.get(path, id) if not rv: abort(404) + + if isinstance(rv, list): + for item in rv: + item.text = utils.markdown(item.text) + else: + rv.text = utils.markdown(rv.text) + return Response(json.dumps(rv), 200, content_type='application/json') diff --git a/isso/utils.py b/isso/utils.py index 34e523e..155b35e 100644 --- a/isso/utils.py +++ b/isso/utils.py @@ -8,6 +8,8 @@ import socket import httplib import urlparse import contextlib + +import misaka import werkzeug.routing from isso.models import Comment @@ -44,3 +46,9 @@ def determine(host): host = 'http://' + host rv = urlparse.urlparse(host) return (rv.netloc + ':443') if rv.scheme == 'https' else rv.netloc + + +def markdown(text): + return misaka.html(text, + extensions = misaka.EXT_STRIKETHROUGH | misaka.EXT_SUPERSCRIPT | misaka.EXT_AUTOLINK \ + | misaka.HTML_SKIP_HTML | misaka.HTML_SKIP_IMAGES | misaka.HTML_SAFELINK)