mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Allow loading builtin markdown extensions without relying on pkg_resources
This commit is contained in:
parent
0242b3d1c1
commit
c4a5afcf24
@ -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
|
||||
|
||||
|
||||
|
@ -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'''<script>moo</script>xxx<img src="http://moo.com/x.jpg">''')
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user