fix clode block generation

added fenced code blocks to default extension list
This commit is contained in:
Martin Zimmermann 2014-07-06 18:34:06 +02:00
parent 400e523c4c
commit b2b6af24d6
3 changed files with 53 additions and 5 deletions

View File

@ -113,7 +113,7 @@ class Config:
"direct-reply = 3",
"reply-to-self = false",
"[markup]",
"options = strikethrough, autolink",
"options = strikethrough, autolink, fenced_code",
"allowed-elements = ",
"allowed-attributes = "
]

View File

@ -4,6 +4,7 @@ try:
except ImportError:
import unittest
import textwrap
from isso.core import Config
from isso.utils import html
@ -30,6 +31,37 @@ class TestHTML(unittest.TestCase):
for (input, expected) in examples:
self.assertEqual(convert(input), expected)
def test_github_flavoured_markdown(self):
convert = html.Markdown(extensions=("fenced_code", ))
# without lang
_in = textwrap.dedent("""\
Hello, World
```
#!/usr/bin/env python
print("Hello, World")""")
_out = textwrap.dedent("""\
<p>Hello, World</p>
<pre><code>#!/usr/bin/env python
print("Hello, World")
</code></pre>""")
self.assertEqual(convert(_in), _out)
# w/ lang
_in = textwrap.dedent("""\
Hello, World
```python
#!/usr/bin/env python
print("Hello, World")""")
_out = textwrap.dedent("""\
<p>Hello, World</p>
<pre><code class="python">#!/usr/bin/env python
print("Hello, World")
</code></pre>""")
@unittest.skipIf(html.html5lib_version == "0.95", "backport")
def test_sanitizer(self):
sanitizer = html.Sanitizer(elements=[], attributes=[])

View File

@ -1,5 +1,7 @@
# -*- encoding: utf-8 -*-
from __future__ import unicode_literals
import pkg_resources
import operator
@ -54,16 +56,30 @@ def Markdown(extensions=("strikethrough", "superscript", "autolink")):
flags = reduce(operator.xor, map(
lambda ext: getattr(misaka, 'EXT_' + ext.upper()), extensions), 0)
md = misaka.Markdown(Unofficial(), extensions=flags)
def inner(text):
rv = misaka.html(text, extensions=flags).rstrip("\n")
if not rv.endswith("<p>") and not rv.endswith("</p>"):
return "<p>" + rv + "</p>"
return rv
rv = md.render(text).rstrip("\n")
if rv.startswith("<p>") or rv.endswith("</p>"):
return rv
return "<p>" + rv + "</p>"
return inner
class Unofficial(misaka.HtmlRenderer):
"""A few modifications to process "common" Markdown.
For instance, fenced code blocks (~~~ or ```) are just wrapped in <code>
which does not preserve line breaks. If a language is given, it is added
to <code class="$lang">, compatible with Highlight.js.
"""
def block_code(self, text, lang):
lang = ' class="{0}"'.format(lang) if lang else ''
return "<pre><code{1}>{0}</code></pre>\n".format(text, lang)
class Markup(object):
def __init__(self, conf):