fix clode block generation
added fenced code blocks to default extension list
This commit is contained in:
parent
400e523c4c
commit
b2b6af24d6
@ -113,7 +113,7 @@ class Config:
|
|||||||
"direct-reply = 3",
|
"direct-reply = 3",
|
||||||
"reply-to-self = false",
|
"reply-to-self = false",
|
||||||
"[markup]",
|
"[markup]",
|
||||||
"options = strikethrough, autolink",
|
"options = strikethrough, autolink, fenced_code",
|
||||||
"allowed-elements = ",
|
"allowed-elements = ",
|
||||||
"allowed-attributes = "
|
"allowed-attributes = "
|
||||||
]
|
]
|
||||||
|
@ -4,6 +4,7 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
import textwrap
|
||||||
|
|
||||||
from isso.core import Config
|
from isso.core import Config
|
||||||
from isso.utils import html
|
from isso.utils import html
|
||||||
@ -30,6 +31,37 @@ class TestHTML(unittest.TestCase):
|
|||||||
for (input, expected) in examples:
|
for (input, expected) in examples:
|
||||||
self.assertEqual(convert(input), expected)
|
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")
|
@unittest.skipIf(html.html5lib_version == "0.95", "backport")
|
||||||
def test_sanitizer(self):
|
def test_sanitizer(self):
|
||||||
sanitizer = html.Sanitizer(elements=[], attributes=[])
|
sanitizer = html.Sanitizer(elements=[], attributes=[])
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
# -*- encoding: utf-8 -*-
|
# -*- encoding: utf-8 -*-
|
||||||
|
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
import operator
|
import operator
|
||||||
|
|
||||||
@ -54,16 +56,30 @@ def Markdown(extensions=("strikethrough", "superscript", "autolink")):
|
|||||||
|
|
||||||
flags = reduce(operator.xor, map(
|
flags = reduce(operator.xor, map(
|
||||||
lambda ext: getattr(misaka, 'EXT_' + ext.upper()), extensions), 0)
|
lambda ext: getattr(misaka, 'EXT_' + ext.upper()), extensions), 0)
|
||||||
|
md = misaka.Markdown(Unofficial(), extensions=flags)
|
||||||
|
|
||||||
def inner(text):
|
def inner(text):
|
||||||
rv = misaka.html(text, extensions=flags).rstrip("\n")
|
rv = md.render(text).rstrip("\n")
|
||||||
if not rv.endswith("<p>") and not rv.endswith("</p>"):
|
if rv.startswith("<p>") or rv.endswith("</p>"):
|
||||||
return "<p>" + rv + "</p>"
|
return rv
|
||||||
return rv
|
return "<p>" + rv + "</p>"
|
||||||
|
|
||||||
return inner
|
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):
|
class Markup(object):
|
||||||
|
|
||||||
def __init__(self, conf):
|
def __init__(self, conf):
|
||||||
|
Loading…
Reference in New Issue
Block a user