From c4a5afcf24769ec9adde99bc80f1ed33c9ee5e78 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 10 Feb 2019 19:14:26 +0530 Subject: [PATCH] Allow loading builtin markdown extensions without relying on pkg_resources --- src/calibre/ebooks/txt/processor.py | 22 ++++++++++++++++++++-- src/calibre/test_build.py | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/txt/processor.py b/src/calibre/ebooks/txt/processor.py index b2a6292748..7a729f859e 100644 --- a/src/calibre/ebooks/txt/processor.py +++ b/src/calibre/ebooks/txt/processor.py @@ -103,10 +103,28 @@ DEFAULT_MD_EXTENSIONS = ('footnotes', 'tables', 'toc') def create_markdown_object(extensions): + # Need to load markdown extensions without relying on pkg_resources + import importlib from calibre.ebooks.markdown import Markdown + from markdown import Extension + + class NotBrainDeadMarkdown(Markdown): + def build_extension(self, ext_name, configs): + if '.' in ext_name or ':' in ext_name: + return Markdown.build_extension(self, ext_name, configs) + ext_name = 'markdown.extensions.' + ext_name + module = importlib.import_module(ext_name) + if hasattr(module, 'makeExtension'): + return module.makeExtension(**configs) + for name, x in vars(module).items(): + if type(x) is type and issubclass(x, Extension) and x is not Extension: + return x(**configs) + raise ImportError('No extension class in {}'.format(ext_name)) + from calibre.ebooks.conversion.plugins.txt_input import MD_EXTENSIONS - extensions = [x.lower() for x in extensions if x.lower() in MD_EXTENSIONS] - md = Markdown(extensions=extensions) + extensions = [x.lower() for x in extensions] + extensions = [x for x in extensions if x in MD_EXTENSIONS] + md = NotBrainDeadMarkdown(extensions=extensions) return md diff --git a/src/calibre/test_build.py b/src/calibre/test_build.py index 64470fa025..f450e91744 100644 --- a/src/calibre/test_build.py +++ b/src/calibre/test_build.py @@ -271,7 +271,7 @@ class BuildTest(unittest.TestCase): def test_markdown(self): from calibre.ebooks.txt.processor import create_markdown_object from calibre.ebooks.conversion.plugins.txt_input import MD_EXTENSIONS - create_markdown_object(MD_EXTENSIONS) + create_markdown_object(sorted(MD_EXTENSIONS)) from calibre.library.comments import sanitize_comments_html sanitize_comments_html(b'''xxx''')