From 98859afd4106c1b8ebc5c06f176126bff12084aa Mon Sep 17 00:00:00 2001 From: Priyam Singh Date: Wed, 23 Aug 2017 18:52:59 +0530 Subject: [PATCH] Fixes #311 Add support for markdown constructs not in commonmark --- modules/scripts/markdown.py | 21 +++++++++++++++++++++ modules/templates/conf.py_t | 4 ++-- 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 modules/scripts/markdown.py diff --git a/modules/scripts/markdown.py b/modules/scripts/markdown.py new file mode 100644 index 00000000..84f4fa95 --- /dev/null +++ b/modules/scripts/markdown.py @@ -0,0 +1,21 @@ +import re +from recommonmark.parser import CommonMarkParser +from md2rst import md2rst + + +MARKDOWN_PLUS_REGEX = re.compile('(.*?)', re.DOTALL) +EVAL_RST_TEMPLATE = "```eval_rst\n{content}\n```" + + +def preprocess_markdown(inputstring): + def callback(match_object): + text = match_object.group(1) + return EVAL_RST_TEMPLATE.format(content=md2rst(text)) + + return re.sub(MARKDOWN_PLUS_REGEX, callback, inputstring) + + +class MarkdownParser(CommonMarkParser): + def parse(self, inputstring, document): + content = preprocess_markdown(inputstring) + CommonMarkParser.parse(self, content, document) diff --git a/modules/templates/conf.py_t b/modules/templates/conf.py_t index 0a00b882..06a0d1dc 100644 --- a/modules/templates/conf.py_t +++ b/modules/templates/conf.py_t @@ -38,7 +38,6 @@ import os import sys import mock import pkg_resources -from recommonmark.parser import CommonMarkParser from recommonmark.transform import AutoStructify # Adding scripts and extensions directory to sys.path @@ -46,6 +45,7 @@ sys.path.insert(0, os.path.abspath('scripts')) sys.path.insert(0, os.path.join(os.path.abspath('scripts'), 'extensions')) from config.serializer import deserialize +from markdown import MarkdownParser {% if autoapi_python == true %} for (dirpath, dirnames, filenames) in os.walk('{{ root_dir }}'): @@ -107,7 +107,7 @@ templates_path = ['{{ dot }}templates'] # The parser(s) of source files source_parsers = { - '.md': CommonMarkParser, + '.md': MarkdownParser, } # The suffix(es) of source filenames.