Skip to content

Commit

Permalink
Support ANSI colors code blocks
Browse files Browse the repository at this point in the history
Fixes #63
Fixes #61
  • Loading branch information
hroncok authored and encukou committed Mar 20, 2017
1 parent fc8b6a8 commit c6edab4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
16 changes: 15 additions & 1 deletion naucse/markdown_util.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from textwrap import dedent
import re

from ansi2html import Ansi2HTMLConverter
import mistune
from jinja2 import Markup
import pygments
import pygments.lexers
import pygments.formatters.html


ansi_convertor = Ansi2HTMLConverter(inline=True)

pygments_formatter = pygments.formatters.html.HtmlFormatter(
cssclass='codehilite'
)
Expand Down Expand Up @@ -53,7 +57,14 @@ def parse_deflist(self, m):
})


def ansi_convert(code):
replaced = code.replace('\u241b', '\x1b')
return ansi_convertor.convert(replaced, full=False)


class Renderer(mistune.Renderer):
code_tmpl = '<div class="codehilite"><pre><code>{}</code></pre></div>'

def admonition(self, name, content):
return '<div class="admonition {}">{}</div>'.format(name, content)

Expand All @@ -62,7 +73,10 @@ def block_code(self, code, lang):
lang = lang.strip()
if not lang or lang == 'plain':
escaped = mistune.escape(code)
return '<div class="codehilite"><pre><code>{}</code></pre></div>'.format(escaped)
return self.code_tmpl.format(escaped)
if lang == 'ansi':
converted = ansi_convert(code)
return self.code_tmpl.format(converted)
lexer = pygments.lexers.get_lexer_by_name(lang)
return pygments.highlight(code, lexer, pygments_formatter).strip()

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PyYAML
ansi2html
flask
elsa>=0.1.2
frozen-flask
Expand Down
32 changes: 32 additions & 0 deletions test_naucse/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ def test_markdown_definition_list_advanced():
assert convert_markdown(src).strip() == expected


def test_markdown_ansi_colors():
src = dedent("""
```ansi
On branch ansicolors
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
␛[32mmodified: naucse/markdown_util.py␛[m
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
␛[31mmodified: test_naucse/test_markdown.py␛[m
```
""")
expected = dedent("""
<div class="codehilite"><pre><code>On branch ansicolors
Changes to be committed:
(use "git reset HEAD &lt;file&gt;..." to unstage)
<span style="color: #00aa00">modified: naucse/markdown_util.py</span>
Changes not staged for commit:
(use "git add &lt;file&gt;..." to update what will be committed)
(use "git checkout -- &lt;file&gt;..." to discard changes in working directory)
<span style="color: #aa0000">modified: test_naucse/test_markdown.py</span></code></pre></div>
""").strip()
assert convert_markdown(src) == expected


def test_markdown_keeps_nbsp():
text = 'Some text\N{NO-BREAK SPACE}more text'
assert convert_markdown(text).strip() == '<p>{}</p>'.format(text)

0 comments on commit c6edab4

Please sign in to comment.