API for the fastdoc convertor

markdown_cell[source]

markdown_cell(md)

code_cell[source]

code_cell(code, metadata=None, outputs=None)

Preprocessing

Preprocessing on the list of all cells

Removing cells with the flag # hide

remove_hidden_cells[source]

remove_hidden_cells(cells)

Remove cells marked with #hide

cells = [code_cell('# hide'), code_cell('lalala'), markdown_cell('lalala\n# hide')]
test_eq(remove_hidden_cells(cells), [code_cell('lalala')])

Isolating the bits in triple quotes annotated with asciidoc in code cells without outputs so that they are not interpreted by the converter, with adding ##clear## so that the post-processing removes the [python] flag.

isolate_adoc_blocks[source]

isolate_adoc_blocks(cells)

test = """This is some text
```asciidoc
This should be isolated
```
Some other text
```asciidoc
This should also be isolated
```
end"""
test_eq(isolate_adoc_blocks([markdown_cell(test)]), [
    markdown_cell("This is some text"),
    code_cell("##clear##This should be isolated"),
    markdown_cell("Some other text"),
    code_cell("##clear##This should also be isolated"),
    markdown_cell("end")
])

Preprocessing individual code cells

Old way of putting [WARNING], [NOTE] or [TIP]

replace_old_jekylls[source]

replace_old_jekylls(cell)

test_eq(replace_old_jekylls(code_cell('jekyll_warn("""Try to convert me!""")')), 
        code_cell('##clear##[WARNING]\n====\nTry to convert me!\n===='))

Hide input of cells with hide_input=True in metadata (extension hide input) or a flag #hide_input. Put ##remove## instead of the code that will be removed during post-processing

hide_input[source]

hide_input(cell)

test_eq(hide_input(code_cell('some code', metadata={'hide_input': True}, outputs=[1])), 
        code_cell('##remove##', metadata={'hide_input': True}, outputs=[1]))
test_eq(hide_input(code_cell('# hide_input\nsome code', outputs=[1])), 
        code_cell('##remove##', outputs=[1]))

Hide outputs of cells with collapsed=True in their metadata or a flag #hide_output

hide_output[source]

hide_output(cell)

test_eq(hide_output(code_cell('some code', metadata={'collapsed': True}, outputs=[1])), 
        code_cell('some code', metadata={'collapsed': True}))
test_eq(hide_output(code_cell('# hide_output\nsome code', outputs=[1])), 
        code_cell('some code'))

Replace outputs as text_html by text_plain (otherwise they are not kept)

extract_html[source]

extract_html(cell)

test_eq(extract_html(code_cell('some code', outputs=[{'data': {'text/html': 'some_html'}}])),
        code_cell('some code', outputs=[{'data': {'text/plain': 'some_html'}}]))

Deal with errors by putting them in plain text

split_max_len[source]

split_max_len(text, l)

deal_error[source]

deal_error(cell)

test_eq(deal_error(code_cell('some code', outputs=[{'output_type': 'error', 'ename': 'Error name', 'evalue': 'This is an error.'}])), 
        code_cell('some code', outputs = [
            {'data': {'text/plain': ' Error name: This is an error.'},
             'execution_count': None,
             'metadata': {},
             'output_type': 'execute_result'}
        ]))

Remove interrupted progress bars from the outputs

remove_interrupted_pbars[source]

remove_interrupted_pbars(cell)

test_eq(remove_interrupted_pbars(
    code_cell("some code", outputs = [{'a': 1}, {'data': {'text/plain': 'progress-bar-interrupted'}}, {'b': 2}])),
        code_cell("some code", outputs = [{'a': 1}, {'b': 2}]))

Get metadata for outputs.

get_cell_meta[source]

get_cell_meta(cell)

test_eq(get_cell_meta(code_cell("#id 123\n#caption This is a bear\nsome code")), 
        code_cell("some code", metadata = {'id': '123', 'caption': 'This is a bear'}))

Deal with table captions and refs

caption_tables[source]

caption_tables(cell)

cell = code_cell("some code", 
    metadata={'id': '123', 'caption': 'a caption'},
    outputs=[{'data': {'text/plain': '<table border="1">\nTable code'}}])
cell2 = code_cell("some code", 
    metadata={'id': '123', 'caption': 'a caption'},
    outputs=[{'data': {'text/plain': '<table id="123" border="1">\n  <caption>a caption</caption>\nTable code'}}])
test_eq(caption_tables(cell), cell2)
cell = code_cell("#hide_input\n#id 123\n#caption a caption", 
    metadata={},
    outputs=[{'data': {'text/plain': '<table border="1">\nTable code'}, 'output_type':''}])

Wrap text in outputs

wrap_text_outputs[source]

wrap_text_outputs(cell)

cell = code_cell("some code", 
    metadata={},
    outputs=[{'data': {'text/plain': 'This is a long output'*5}, 'output_type':''},
             {'text': 'This is a long output'*5}])
wrapped = 'This is a long outputThis is a long outputThis is a long outputThis is a long\n > outputThis is a long output'
test_eq(wrap_text_outputs(cell), code_cell("some code", 
    metadata={},
    outputs=[{'data': {'text/plain': wrapped}, 'output_type':''},
             {'text': wrapped}]))

Test code length

check_code_len[source]

check_code_len(cell)

Preprocessing individual markdown cells

Replace "` `" by ``

deal_quotes[source]

deal_quotes(cell)

test_eq(deal_quotes(markdown_cell('"`code`"')), markdown_cell('`code`'))
test_eq(deal_quotes(markdown_cell('a"b"c')), markdown_cell('a"b"c'))
test_eq(deal_quotes(markdown_cell("a'b'c")), markdown_cell('axxsinglequotebxxsinglequotec'))

Add one title level to every Markdown cell

add_title_level[source]

add_title_level(cell)

test_eq(add_title_level(markdown_cell('# title')), markdown_cell('## title'))

Remove digits from numbered lists and format labeled lists

deal_with_lists[source]

deal_with_lists(cell)

test_eq(deal_with_lists(markdown_cell("  1. Item\n  2. Item")),
        markdown_cell("  . Itemxxnewl\n  . Itemxxnewl"))
test_eq(deal_with_lists(markdown_cell("- lbl1:: item1\n- lbl2:: item2")),
        markdown_cell("lbl1::xxnewlsitem1xxnewl\nlbl2::xxnewlsitem2xxnewl"))

Catch block quotes and put them in asciidoc blocks

replace_jekylls[source]

replace_jekylls(cell)

test_eq(replace_jekylls(markdown_cell("text\n> : This is a block quote")),
    markdown_cell("text\n```asciidoc\n____\nThis is a block quote\n____\n```\n"))
test_eq(replace_jekylls(markdown_cell("text\n> jargon: term: Some new term")),
    markdown_cell('text\n```asciidoc\n.Jargon: term\n[NOTE]\n====\nSome new term\n====\n```\n'))
test_warns(lambda: replace_jekylls(markdown_cell("text\n> This is a block quote")))

interpret_sidebar[source]

interpret_sidebar(cell)

test = """#sidebar My intervention

This will be changed to a sidebar when converted in Asciidoc.

It can have several lines, contrary to a block quote."""
interpret_sidebar(markdown_cell(test))
{'cell_type': 'markdown',
 'source': '```asciidoc\n.My intervention\n****\n\nThis will be changed to a sidebar when converted in Asciidoc.\n\nIt can have several lines, contrary to a block quote.\n****\n```\n',
 'metadata': {}}

process_images[source]

process_images(cell)

txt = 'text\n<img alt="Alternative text" width="700" caption="This is an image" src="puppy.jpg" id="123"/>\nother text'
test_eq(process_images(markdown_cell(txt)), 
        markdown_cell('text\n```asciidoc\n[[123]]\n.This is an image\nimage::puppy.jpg["Alternative text", 420]\n```\nother text'))

wrap_references[source]

wrap_references(cell)

test_eq(wrap_references(markdown_cell("There is a reference <<ref>> here.")),
        markdown_cell("There is a reference xxrefrefxxeref here."))

extract_attachments[source]

extract_attachments(cell, dest)

Catch sidebars: sidebars are delimited by header cells like ### Sidebar title then ### End sidebar

_re_sidebar_title.search('### Sidebar: Tenacity in deep learning').groups()
('Tenacity in deep learning',)

sidebar_headers(cell)

test_eq(sidebar_headers(markdown_cell("### Sidebar: My intervention")),
        markdown_cell("```asciidoc\n.My intervention\n****\n```"))
test_eq(sidebar_headers(markdown_cell("### End sidebar")), markdown_cell("```asciidoc\n****\n```"))

All preprocessing together

Raw cells just need to have a new line added at the beginning

add_new_line[source]

add_new_line(cell)

treat_notebook[source]

treat_notebook(nb, dest)

Post-processing

Replace special tokens by their values

rep_spec_tok[source]

rep_spec_tok(adoc, metadata=None)

nbconvert will flag the code cells with [ipython3], we replace this by [python]

ipython2python[source]

ipython2python(adoc, metadata=None)

test_eq(ipython2python("[source, ipython3]\n----\nsome code\n----\n"), "[source, python]\n----\nsome code\n----\n")

Remove empty cells or cells flagged for removal (because of hide_input)

remove_cells[source]

remove_cells(adoc, metadata=None)

test_eq(remove_cells("lalala\n[source, python]\n----\n\n----\n"), "lalala")
test_eq(remove_cells("lalala\n[source, python]\n----\n##remove##\n----\n"), "lalala")

Clear code cells from the code flag when there is a ##clear## tag.

clear_cells[source]

clear_cells(adoc, metadata=None)

test_eq(clear_cells(
    "lalala\n[source, python]\n----\n##clear##pure adoc\n----\nfoo\nbla\n[source, python]\n----\n##clear##pure adoc again\n----\nbli"),
        "lalala\npure adoc\nfoo\nbla\npure adoc again\nbli")

Format LaTeX equations properly: they arrive either as latexmath:[$equation$] or latexmath:[\[equation\]]

format_latex[source]

format_latex(adoc, metadata=None)

test_eq(format_latex(r"latexmath:[$equation$]"), r"latexmath:[\(equation\)]")
test_eq(format_latex(r"latexmath:[\[equation\]]"), 
        "\n[latexmath]\n++++\n\\begin{equation}\nequation\n\\end{equation}\n++++\n")

Format image outputs and make sure they point to the right folder.

format_outputs[source]

format_outputs(adoc, metadata=None)

test_eq(format_outputs('----\n![svg](output.svg)\n----', {'folder':'path', 'output.svg': {'alt': 'alt'}}),
        'image::path/output.svg["alt"]')
test_eq(format_outputs('----\n![svg](output.svg)\n----', {'folder':'path', 'output.svg': {'alt': 'alt', 'width': 100}}),
        'image::path/output.svg["alt", 100]')
test_eq(format_outputs('----\n![png](output1.png)\n----'),
        'image::./output1.png[""]')

Deal with quotes

fix_quotes[source]

fix_quotes(adoc, metadata=None)

test_eq(fix_quotes("``double quotes''"), '"double quotes"')

Put back << >> around refs

fix_references[source]

fix_references(adoc, metadata=None)

test_eq(fix_references("There is a reference xxrefrefxxeref here."), "There is a reference <<ref>> here.")

Format tables

format_tables[source]

format_tables(adoc, metadata=None)

Just as a personal preference, replace all blocks of three new lines or more by \n\n

remove_lines[source]

remove_lines(text, metadata=None)

test_eq(remove_lines('a\n\n\n\n\n\nb'), 'a\n\n\nb')

All together

post_process[source]

post_process(adoc, metadata=None)

Exporting

add_metadata[source]

add_metadata(nb)

Stripping removes metadata used in the conversion.

output_num[source]

output_num(n)

test_eq(output_num('output_31_0.png'), 31)
test_eq(output_num('output_12_0.svg'), 12)

get_output_width[source]

get_output_width(name, raw, folder)

convert_nb[source]

convert_nb(fname, dest_path='.', folder=None)

Convert a notebook fname to html file in dest_path.

dest = Path('test')
convert_nb('test/_test.ipynb', dest)
Converting test/_test.ipynb
 

copy_images[source]

copy_images(path, dest_path)

dest = Path('..')/'convert_book'
# copy_images(Path('book'), dest)

fastdoc_convert_all[source]

fastdoc_convert_all(path:"Path to notebooks"='book', dest_path:"Path to generated asciidoc files"='../convert_book')

 
from nbdev.export import *
notebook2script()
Converted 00_asciidoc.ipynb.
Converted 01_clean.ipynb.
Converted index.ipynb.