From b2b6af24d69193e0a1e18e32267c8d5b1873cab6 Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Sun, 6 Jul 2014 18:34:06 +0200 Subject: [PATCH] fix clode block generation added fenced code blocks to default extension list --- isso/core.py | 2 +- isso/tests/test_html.py | 32 ++++++++++++++++++++++++++++++++ isso/utils/html.py | 24 ++++++++++++++++++++---- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/isso/core.py b/isso/core.py index 4e30f49..3cdadf5 100644 --- a/isso/core.py +++ b/isso/core.py @@ -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 = " ] diff --git a/isso/tests/test_html.py b/isso/tests/test_html.py index f03d4c2..65d6056 100644 --- a/isso/tests/test_html.py +++ b/isso/tests/test_html.py @@ -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("""\ +

Hello, World

+
#!/usr/bin/env python
+            print("Hello, World")
+            
""") + + self.assertEqual(convert(_in), _out) + + # w/ lang + _in = textwrap.dedent("""\ + Hello, World + + ```python + #!/usr/bin/env python + print("Hello, World")""") + _out = textwrap.dedent("""\ +

Hello, World

+
#!/usr/bin/env python
+            print("Hello, World")
+            
""") + @unittest.skipIf(html.html5lib_version == "0.95", "backport") def test_sanitizer(self): sanitizer = html.Sanitizer(elements=[], attributes=[]) diff --git a/isso/utils/html.py b/isso/utils/html.py index 212875d..4c8013a 100644 --- a/isso/utils/html.py +++ b/isso/utils/html.py @@ -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("

") and not rv.endswith("

"): - return "

" + rv + "

" - return rv + rv = md.render(text).rstrip("\n") + if rv.startswith("

") or rv.endswith("

"): + return rv + return "

" + rv + "

" return inner +class Unofficial(misaka.HtmlRenderer): + """A few modifications to process "common" Markdown. + + For instance, fenced code blocks (~~~ or ```) are just wrapped in + which does not preserve line breaks. If a language is given, it is added + to , compatible with Highlight.js. + """ + + def block_code(self, text, lang): + lang = ' class="{0}"'.format(lang) if lang else '' + return "
{0}
\n".format(text, lang) + + class Markup(object): def __init__(self, conf):