diff --git a/rest_framework/compat.py b/rest_framework/compat.py index b4b67ba17..3b341a656 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -244,6 +244,7 @@ try: md = markdown.Markdown( extensions=extensions, extension_configs=extension_configs ) + md_filter_add_syntax_highlight(md) return md.convert(text) except ImportError: apply_markdown = None @@ -274,6 +275,12 @@ except ImportError: return None if markdown is not None and pygments is not None: + # starting from this blogpost and modified to support current markdown extensions API + # https://zerokspot.com/weblog/2008/06/18/syntax-highlighting-in-markdown-with-pygments/ + + from markdown.preprocessors import Preprocessor + import re + class CodeBlockPreprocessor(Preprocessor): pattern = re.compile( r'^\s*@@ (.+?) @@\s*(.+?)^\s*@@', re.M|re.S) diff --git a/tests/test_description.py b/tests/test_description.py index 4df14ac55..a97550ed8 100644 --- a/tests/test_description.py +++ b/tests/test_description.py @@ -24,11 +24,36 @@ another header indented -# hash style header #""" +# hash style header # + +@@ json @@ +[{ + "alpha": 1, + "beta: "this is a string" +}] +@@""" # If markdown is installed we also test it's working # (and that our wrapped forces '=' to h2 and '-' to h3) +MARKED_DOWN_HILITE = """ +
[{
"alpha": 1,
\
+ "beta: "this\
+ is a \
+string"
}]
@@ json @@ +[{ + "alpha": 1, + "beta: "this is a string" +}] +@@
""" + # We support markdown < 2.1 and markdown >= 2.1 MARKED_DOWN_lt_21 = """code block
indented
-code block
indented
-