mirror of
git://github.com/kovidgoyal/calibre.git
synced 2026-04-28 09:32:58 +02:00
Allow loading builtin markdown extensions without relying on pkg_resources
This commit is contained in:
parent
0242b3d1c1
commit
c4a5afcf24
2 changed files with 21 additions and 3 deletions
|
|
@ -103,10 +103,28 @@ def convert_basic(txt, title='', epub_split_size_kb=0):
|
|||
|
||||
|
||||
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 @@ def test_terminal(self):
|
|||
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…
Reference in a new issue