forked from aaren/notedown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
480 lines (353 loc) · 11.1 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
from __future__ import absolute_import
from __future__ import print_function
import os
import tempfile
import nose.tools as nt
import nbformat
import notedown
simple_backtick = """
```
code1
space_indent
more code
```
text1
``
```
code2
tab_indent
~~~
```
text2"""
simple_tilde = """
~~~
code1
space_indent
more code
~~~
text1
``
~~~~
code2
tab_indent
~~~
~~~~
text2"""
simple_indented = """
code1
space_indent
more code
text1
``
code2
tab_indent
~~~
text2"""
simple_code_cells = ['code1\n space_indent\n\n\nmore code',
'code2\n tab_indent\n~~~']
# note: ipython markdown cells do not end with a newline unless
# explicitly present.
simple_markdown_cells = ['text1\n``', 'text2']
alt_lang = """
This is how you write a code block in another language:
```bash
echo "This is bash ${BASH_VERSION}!"
```
"""
alt_lang_code = '%%bash\necho "This is bash ${BASH_VERSION}!"'
sample_markdown = u"""### Create IPython Notebooks from markdown
This is a simple tool to convert markdown with code into an IPython
Notebook.
Usage:
```
notedown input.md > output.ipynb
```
It is really simple and separates your markdown into code and not
code. Code goes into code cells, not-code goes into markdown cells.
Installation:
pip install notedown
"""
# Generate the sample notebook from the markdown using
#
# import notedown
# reader = notedown.MarkdownReader()
# sample_notebook = reader.reads(sample_markdown)
# print nbformat.writes(sample_notebook)
#
# which is defined in create_json_notebook() below
sample_notebook = r"""{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create IPython Notebooks from markdown\n",
"\n",
"This is a simple tool to convert markdown with code into an IPython\n",
"Notebook.\n",
"\n",
"Usage:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"notedown input.md > output.ipynb"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is really simple and separates your markdown into code and not\n",
"code. Code goes into code cells, not-code goes into markdown cells.\n",
"\n",
"Installation:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pip install notedown"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 0
}"""
roundtrip_markdown = u"""## A roundtrip test
Here is a code cell:
```python
a = 1
```
and here is another one:
```python
b = 2
```
"""
attribute_markdown = u"""Attribute test
```lang
code1
```
```{.attr}
code2
```
``` {.attr}
code3
```
"""
ref_attributes = ['lang', r'{.attr}', r'{.attr}']
def create_json_notebook(markdown):
reader = notedown.MarkdownReader()
notebook = reader.reads(markdown)
json_notebook = nbformat.writes(notebook)
return json_notebook
def test_notedown():
"""Integration test the whole thing."""
from difflib import ndiff
notebook = create_json_notebook(sample_markdown)
diff = ndiff(sample_notebook.splitlines(1), notebook.splitlines(1))
print('\n'.join(diff))
nt.assert_multi_line_equal(create_json_notebook(sample_markdown),
sample_notebook)
def parse_cells(text, regex=None):
reader = notedown.MarkdownReader(code_regex=regex)
return reader.parse_blocks(text)
def separate_code_cells(cells):
codetype = notedown.MarkdownReader.code
code_cells = [c['content'] for c in cells if c['type'] == codetype]
return code_cells
def separate_markdown_cells(cells):
markdowntype = notedown.MarkdownReader.markdown
markdown_cells = [c['content'] for c in cells if c['type'] == markdowntype]
return markdown_cells
def test_parse_gfm():
"""Test with GFM code blocks."""
all_cells = parse_cells(simple_backtick, 'fenced')
code_cells = separate_code_cells(all_cells)
markdown_cells = separate_markdown_cells(all_cells)
print("out: ", code_cells)
print("ref: ", simple_code_cells)
print("out: ", markdown_cells)
print("ref: ", simple_markdown_cells)
assert(code_cells == simple_code_cells)
assert(markdown_cells == simple_markdown_cells)
def test_parse_tilde():
"""Test with ~~~ delimited code blocks."""
all_cells = parse_cells(simple_tilde, 'fenced')
code_cells = separate_code_cells(all_cells)
markdown_cells = separate_markdown_cells(all_cells)
assert(code_cells == simple_code_cells)
assert(markdown_cells == simple_markdown_cells)
def test_parse_indented():
"""Test with indented code blocks."""
all_cells = parse_cells(simple_indented, 'indented')
code_cells = separate_code_cells(all_cells)
markdown_cells = separate_markdown_cells(all_cells)
print("out: ", code_cells)
print("ref: ", simple_code_cells)
print("out: ", markdown_cells)
print("ref: ", simple_markdown_cells)
assert(code_cells == simple_code_cells)
assert(markdown_cells == simple_markdown_cells)
def test_alt_lang():
"""Specifying a language that isn't python should generate
code blocks using %%language magic."""
reader = notedown.MarkdownReader(code_regex='fenced')
all_blocks = reader.parse_blocks(alt_lang)
code_blocks = [b for b in all_blocks if b['type'] == reader.code]
magic_block = code_blocks[0]
reader.process_code_block(magic_block)
assert(magic_block['content'] == alt_lang_code)
def test_format_agnostic():
"""Test whether we can process markdown with either fenced or
indented blocks."""
fenced_cells = parse_cells(simple_backtick, None)
indented_cells = parse_cells(simple_indented, None)
fenced_code_cells = separate_code_cells(fenced_cells)
indented_code_cells = separate_code_cells(indented_cells)
fenced_markdown_cells = separate_markdown_cells(fenced_cells)
indented_markdown_cells = separate_markdown_cells(indented_cells)
assert(fenced_code_cells == indented_code_cells)
assert(fenced_markdown_cells == indented_markdown_cells)
def test_attributes():
"""Are code block attributes correctly parsed?"""
cells = parse_cells(attribute_markdown)
attributes = [cell['attributes'] for cell in cells if cell['type'] == 'code']
for attr, ref in zip(attributes, ref_attributes):
assert attr == ref
def test_pre_process_text():
"""test the stripping of blank lines"""
block = {}
ref = "\t \n\n \t\n\ntext \t \n\n\n"
block['content'] = ref
notedown.MarkdownReader.pre_process_text_block(block)
expected = "text"
print("---")
print("in: ")
print(ref)
print("---")
print("out: ")
print(block['content'])
print("---")
print("expected: ")
print(expected)
print("---")
assert(block['content'] == expected)
def test_roundtrip():
"""Run nbconvert using our custom markdown template to recover
original markdown from a notebook.
"""
# create a notebook from the markdown
mr = notedown.MarkdownReader()
roundtrip_notebook = mr.to_notebook(roundtrip_markdown)
# write the notebook into json
notebook_json = nbformat.writes(roundtrip_notebook)
# write the json back into notebook
notebook = nbformat.reads(notebook_json, as_version=4)
# convert notebook to markdown
mw = notedown.MarkdownWriter(template_file='notedown/templates/markdown.tpl', strip_outputs=True)
markdown = mw.writes(notebook)
nt.assert_multi_line_equal(roundtrip_markdown, markdown)
def test_template_load_absolute():
"""Load a template from an absolute path.
IPython 3 requires a relative path in a child directory.
"""
template_abspath = os.path.abspath('notedown/templates/markdown.tpl')
writer = notedown.MarkdownWriter(template_file=template_abspath)
import jinja2
assert(isinstance(writer.exporter.template, jinja2.Template))
def test_template_load_nonchild():
"""Load a template from a non-child directory.
IPython 3 requires a relative path in a child directory.
"""
temp = tempfile.NamedTemporaryFile(delete=False, mode='w+t')
template_path = 'notedown/templates/markdown.tpl'
with open(template_path, 'rt') as source:
temp.write(source.read())
temp.close()
writer = notedown.MarkdownWriter(template_file=temp.name)
import jinja2
assert(isinstance(writer.exporter.template, jinja2.Template))
os.remove(temp.name)
def test_markdown_markdown():
mr = notedown.MarkdownReader()
mw = notedown.MarkdownWriter(notedown.markdown_template)
nb = mr.reads(roundtrip_markdown)
markdown = mw.writes(nb)
nt.assert_multi_line_equal(markdown, roundtrip_markdown)
def test_R():
"""Check that the R notebook generated from Rmd looks the same
as the reference (without output cells).
"""
knitr = notedown.Knitr()
with open('r-examples/r-example.Rmd') as rmd:
knitted_markdown_file = knitr.knit(rmd)
reader = notedown.MarkdownReader(precode=r"%load_ext rpy2.ipython",
magic=True)
notebook = reader.read(knitted_markdown_file)
with open('r-examples/r-example.ipynb') as f:
reference_notebook = nbformat.read(f, as_version=4)
notedown.main.strip(notebook)
notedown.main.strip(reference_notebook)
writer = nbformat
nbjson = writer.writes(notebook)
reference_nbjson = writer.writes(reference_notebook)
nt.assert_multi_line_equal(nbjson, reference_nbjson)
def test_match_fenced():
mr = notedown.MarkdownReader(match='fenced')
nb = mr.to_notebook(sample_markdown)
assert(nb.cells[1]['cell_type'] == 'code')
assert(nb.cells[3]['cell_type'] == 'markdown')
def test_match_arbitrary():
mr = notedown.MarkdownReader(match='attr')
nb = mr.to_notebook(attribute_markdown)
assert(nb.cells[0]['cell_type'] == 'markdown')
assert(nb.cells[2]['cell_type'] == 'code')
assert(nb.cells[3]['cell_type'] == 'code')
class TestCommandLine(object):
@property
def default_args(self):
parser = notedown.main.command_line_parser()
return parser.parse_args()
def run(self, args):
notedown.main.main(args)
def test_basic(self):
args = self.default_args
args.input_file = 'example.md'
self.run(args)
def test_reverse(self):
args = self.default_args
args.input_file = 'example.ipynb'
self.run(args)
def test_markdown_to_notebook(self):
args = self.default_args
args.input_file = 'example.md'
args.informat = 'markdown'
args.outformat = 'notebook'
self.run(args)
def test_markdown_to_markdown(self):
args = self.default_args
args.input_file = 'example.md'
args.informat = 'markdown'
args.outformat = 'markdown'
self.run(args)
def test_notebook_to_markdown(self):
args = self.default_args
args.input_file = 'example.ipynb'
args.informat = 'notebook'
args.outformat = 'markdown'
self.run(args)
def test_notebook_to_notebook(self):
args = self.default_args
args.input_file = 'example.ipynb'
args.informat = 'notebook'
args.outformat = 'notebook'
self.run(args)