Removing cells with the flag # 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.
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")
])
Old way of putting [WARNING]
, [NOTE]
or [TIP]
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
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
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)
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
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
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.
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
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
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
Replace "` `" by ``
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
test_eq(add_title_level(markdown_cell('# title')), markdown_cell('## title'))
Remove digits from numbered lists and format labeled lists
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
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")))
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))
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'))
test_eq(wrap_references(markdown_cell("There is a reference <<ref>> here.")),
markdown_cell("There is a reference xxrefrefxxeref here."))
Catch sidebars: sidebars are delimited by header cells like ### Sidebar title
then ### End sidebar
_re_sidebar_title.search('### Sidebar: Tenacity in deep learning').groups()
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```"))
Raw cells just need to have a new line added at the beginning
Replace special tokens by their values
nbconvert will flag the code cells with [ipython3]
, we replace this by [python]
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)
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.
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\]]
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.
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
test_eq(fix_quotes("``double quotes''"), '"double quotes"')
Put back << >> around refs
test_eq(fix_references("There is a reference xxrefrefxxeref here."), "There is a reference <<ref>> here.")
Format tables
Just as a personal preference, replace all blocks of three new lines or more by \n\n
test_eq(remove_lines('a\n\n\n\n\n\nb'), 'a\n\n\nb')
All together
test_eq(output_num('output_31_0.png'), 31)
test_eq(output_num('output_12_0.svg'), 12)
dest = Path('test')
convert_nb('test/_test.ipynb', dest)
dest = Path('..')/'convert_book'
# copy_images(Path('book'), dest)
from nbdev.export import *
notebook2script()