Skip to content
Browse files

Ansi parser for curling a post using /ansi url

  • Loading branch information...
Depado committed Jul 6, 2015
1 parent 4a50717 commit 4ea590a319af9442c9ea604816c7a18b0c19c8f2
Showing with 111 additions and 4 deletions.
  1. +4 −1 app/models/
  2. +26 −1 app/modules/blog/
  3. +1 −1 app/utils/
  4. +80 −1 app/utils/
@@ -8,7 +8,7 @@
from flask_login import current_user

from app import app, db
from app.utils import markdown_renderer
from app.utils import markdown_renderer, ansi_renderer

class Post(db.Model):
@@ -64,3 +64,6 @@ def content_as_html(self):
return content
return markdown_renderer.render(self.content)

def content_as_ansi(self):
return ansi_renderer.render(self.content)
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

from flask import render_template, redirect, url_for
from flask import render_template, redirect, url_for, make_response, abort
from flask_login import current_user
from sqlalchemy import desc

@@ -56,6 +56,31 @@ def get(user_slug, post_slug):
return render_template("blog/blog_page_404.html")

def get_raw(user_slug, post_slug):
post = Post.query.filter_by(title_slug=post_slug).first()
if post is not None:
resp = make_response(post.content)
resp.mimetype = 'text/plain'
return resp

def get_ansi(user_slug, post_slug):
post = Post.query.filter_by(title_slug=post_slug).first()
if post is not None:
resp = make_response(post.content_as_ansi())
resp.mimetype = 'text/plain'
resp.charset = 'utf-8'
return resp

def all_posts(user_slug):
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

from .markdown import HighlighterRenderer, markdown_renderer
from .markdown import HighlighterRenderer, markdown_renderer, ansi_renderer
@@ -1,12 +1,91 @@
# -*- coding: utf-8 -*-

import misaka
from misaka import HtmlRenderer, SmartyPants
from misaka import HtmlRenderer, SmartyPants, BaseRenderer
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters.html import HtmlFormatter
from pygments.formatters.terminal256 import Terminal256Formatter

class AnsiRenderer(BaseRenderer):

def block_quote(self, quote):
return "{}\n\n".format(quote)

def block_html(self, raw_html):
return "{}\n\n".format(raw_html)

def header(self, text, level):
border = '-'
if level == 1:
border = '='

return "\n\x1b[31;1m{border}\n| {text} |\n{border}\n\x1b[0m\n".format(border=border*(len(text)+4), text=text)

def hrule(self):
return "----------\n\n"

def list(self, contents, is_ordered):
return "{}\n".format(contents)

def list_item(self, text, is_ordered):
return "- {}\n".format(text)

def paragraph(self, text):
return "{}\n\n".format(text)

def autolink(self, link, is_email):
return link

def codespan(self, code):
return code

def double_emphasis(self, text):
return text

def emphasis(self, text):
return text

def image(self, link, title, alt_text):
return link

def linebreak(self):
return "\n"

def link(self, link, title, content):
return link

def raw_html(self, raw_html):
return raw_html

def triple_emphasis(self, text):
return text

def strikethrough(self, text):
return text

def superscript(self, text):
return text

def block_code(self, text, lang):
if not lang:
lang = 'text'
lexer = get_lexer_by_name(lang, stripall=True)
lexer = get_lexer_by_name('text', stripall=True)

formatter = Terminal256Formatter()
return "{formatted}\n".format(
formatted=highlight(text, lexer, formatter),

ansi_renderer = misaka.Markdown(
extensions=misaka.EXT_FENCED_CODE | misaka.EXT_NO_INTRA_EMPHASIS

class HighlighterRenderer(HtmlRenderer, SmartyPants):

def block_code(self, text, lang):

0 comments on commit 4ea590a

Please sign in to comment.
You can’t perform that action at this time.