From 21faa5d187b2cccf2a48315d183c2863ed2cdc50 Mon Sep 17 00:00:00 2001 From: Sylvain Gugger Date: Tue, 7 May 2019 13:47:30 +0000 Subject: [PATCH] Fix #2045 --- fastai/gen_doc/nbdoc.py | 10 +++++++--- fastai/gen_doc/nbtest.py | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/fastai/gen_doc/nbdoc.py b/fastai/gen_doc/nbdoc.py index 5daf55bb34..7161d2a138 100644 --- a/fastai/gen_doc/nbdoc.py +++ b/fastai/gen_doc/nbdoc.py @@ -1,6 +1,6 @@ "`gen_doc.nbdoc` generates notebook documentation from module functions and links to correct places" -import inspect,importlib,enum,os,re +import inspect,importlib,enum,os,re,nbconvert from IPython.core.display import display, Markdown, HTML from nbconvert import HTMLExporter from IPython.core import page @@ -12,7 +12,7 @@ from .nbtest import get_pytest_html from ..utils.ipython import IS_IN_COLAB -__all__ = ['get_fn_link', 'link_docstring', 'show_doc', 'get_ft_names', +__all__ = ['get_fn_link', 'link_docstring', 'show_doc', 'get_ft_names', 'md2thml' 'get_exports', 'show_video', 'show_video_from_youtube', 'import_mod', 'get_source_link', 'is_enum', 'jekyll_note', 'jekyll_warn', 'jekyll_important', 'doc'] @@ -123,6 +123,10 @@ def show_doc(elt, doc_string:bool=True, full_name:str=None, arg_comments:dict=No if markdown: display(Markdown(doc)) else: return doc +def md2html(md): + if nbconvert.__version__ < '5.5.0': return HTMLExporter().markdown2html(md) + else: return HTMLExporter().markdown2html(defaultdict(lambda: defaultdict(dict)), md) + def doc(elt): "Show `show_doc` info in preview window along with link to full docs." global use_relative_links @@ -131,7 +135,7 @@ def doc(elt): md = show_doc(elt, markdown=False) if is_fastai_class(elt): md += f'\n\nShow in docs' - output = HTMLExporter().markdown2html(md) + output = md2html(md) use_relative_links = True if IS_IN_COLAB: get_ipython().run_cell_magic(u'html', u'', output) else: diff --git a/fastai/gen_doc/nbtest.py b/fastai/gen_doc/nbtest.py index 178a4d624d..f4ee035581 100644 --- a/fastai/gen_doc/nbtest.py +++ b/fastai/gen_doc/nbtest.py @@ -25,7 +25,7 @@ def show_test(elt)->str: def doctest(elt): "Inline notebook popup for `show_test`" md = build_tests_markdown(elt) - output = HTMLExporter().markdown2html(md) + output = nbdoc.md2html(md) try: page.page({'text/html': output}) except: display(Markdown(md)) @@ -54,7 +54,7 @@ def tests2md(tests, type_label:str): def get_pytest_html(elt, anchor_id:str)->Tuple[str,str]: md = build_tests_markdown(elt) - html = HTMLExporter().markdown2html(md).replace('\n','') # nbconverter fails to parse markdown if it has both html and '\n' + html = nbdoc.md2html(md).replace('\n','') # nbconverter fails to parse markdown if it has both html and '\n' anchor_id = anchor_id.replace('.', '-') + '-pytest' link, body = get_pytest_card(html, anchor_id) return link, body