@@ -24,7 +24,9 @@ def doctree():
24
24
apps = []
25
25
syspath = sys .path [:]
26
26
27
- def doctree (source , config = None , return_warnings = False , entrypoint = "jupyter_sphinx" ):
27
+ def doctree (
28
+ source , config = None , return_warnings = False , entrypoint = "jupyter_sphinx"
29
+ ):
28
30
src_dir = tempfile .mkdtemp ()
29
31
source_trees .append (src_dir )
30
32
with open (os .path .join (src_dir , "conf.py" ), "w" ) as f :
@@ -61,7 +63,7 @@ def test_basic(doctree):
61
63
2 + 2
62
64
"""
63
65
tree = doctree (source )
64
- cell , = tree .traverse (JupyterCellNode )
66
+ ( cell ,) = tree .traverse (JupyterCellNode )
65
67
assert cell .attributes ["code_below" ] is False
66
68
assert cell .attributes ["hide_code" ] is False
67
69
assert cell .attributes ["hide_output" ] is False
@@ -77,7 +79,7 @@ def test_basic_old_entrypoint(doctree):
77
79
2 + 2
78
80
"""
79
81
tree = doctree (source , entrypoint = "jupyter_sphinx.execute" )
80
- cell , = tree .traverse (JupyterCellNode )
82
+ ( cell ,) = tree .traverse (JupyterCellNode )
81
83
assert cell .attributes ["code_below" ] is False
82
84
assert cell .attributes ["hide_code" ] is False
83
85
assert cell .attributes ["hide_output" ] is False
@@ -94,7 +96,7 @@ def test_hide_output(doctree):
94
96
2 + 2
95
97
"""
96
98
tree = doctree (source )
97
- cell , = tree .traverse (JupyterCellNode )
99
+ ( cell ,) = tree .traverse (JupyterCellNode )
98
100
assert cell .attributes ["hide_output" ] is True
99
101
assert len (cell .children ) == 1
100
102
assert cell .children [0 ].rawsource .strip () == "2 + 2"
@@ -108,7 +110,7 @@ def test_hide_code(doctree):
108
110
2 + 2
109
111
"""
110
112
tree = doctree (source )
111
- cell , = tree .traverse (JupyterCellNode )
113
+ ( cell ,) = tree .traverse (JupyterCellNode )
112
114
assert cell .attributes ["hide_code" ] is True
113
115
assert len (cell .children ) == 1
114
116
assert cell .children [0 ].rawsource .strip () == "4"
@@ -122,7 +124,7 @@ def test_code_below(doctree):
122
124
2 + 2
123
125
"""
124
126
tree = doctree (source )
125
- cell , = tree .traverse (JupyterCellNode )
127
+ ( cell ,) = tree .traverse (JupyterCellNode )
126
128
assert cell .attributes ["code_below" ] is True
127
129
assert cell .children [0 ].rawsource .strip () == "4"
128
130
assert cell .children [1 ].rawsource .strip () == "2 + 2"
@@ -136,7 +138,7 @@ def test_linenos(doctree):
136
138
2 + 2
137
139
"""
138
140
tree = doctree (source )
139
- cell , = tree .traverse (JupyterCellNode )
141
+ ( cell ,) = tree .traverse (JupyterCellNode )
140
142
assert cell .attributes ["linenos" ] is True
141
143
assert len (cell .children ) == 2
142
144
assert cell .children [0 ].rawsource .strip () == "2 + 2"
@@ -149,7 +151,7 @@ def test_linenos(doctree):
149
151
2 + 2
150
152
"""
151
153
tree = doctree (source )
152
- cell , = tree .traverse (JupyterCellNode )
154
+ ( cell ,) = tree .traverse (JupyterCellNode )
153
155
assert len (cell .children ) == 2
154
156
assert cell .attributes ["linenos" ] is True
155
157
@@ -161,14 +163,29 @@ def test_linenos_conf_option(doctree):
161
163
2 + 2
162
164
"""
163
165
tree = doctree (source , config = "jupyter_sphinx_linenos = True" )
164
- cell , = tree .traverse (JupyterCellNode )
166
+ ( cell ,) = tree .traverse (JupyterCellNode )
165
167
assert cell .children [0 ].attributes ["linenos" ]
166
168
assert "highlight_args" not in cell .children [0 ].attributes
167
169
assert cell .children [0 ].rawsource .strip () == "2 + 2"
168
170
assert cell .children [1 ].rawsource .strip () == "4"
169
171
170
172
171
173
def test_continue_linenos_conf_option (doctree ):
174
+ # Test no linenumbering without linenos config or lineno-start directive
175
+ source = """
176
+ .. jupyter-execute::
177
+
178
+ 2 + 2
179
+
180
+ """
181
+
182
+ tree = doctree (source , config = "jupyter_sphinx_continue_linenos = True" )
183
+ (cell ,) = tree .traverse (JupyterCellNode )
184
+ assert "linenos" not in cell .children [0 ].attributes
185
+ assert cell .children [0 ].rawsource .strip () == "2 + 2"
186
+ assert cell .children [1 ].rawsource .strip () == "4"
187
+
188
+ # Test continuous line numbering
172
189
source = """
173
190
.. jupyter-execute::
174
191
@@ -179,11 +196,15 @@ def test_continue_linenos_conf_option(doctree):
179
196
3 + 3
180
197
181
198
"""
182
- continue_linenos_config = "jupyter_sphinx_continue_linenos = True"
183
- tree = doctree (source , config = continue_linenos_config )
199
+
200
+ tree = doctree (
201
+ source ,
202
+ config = "jupyter_sphinx_linenos = True\n "
203
+ "jupyter_sphinx_continue_linenos = True" ,
204
+ )
205
+
184
206
cell0 , cell1 = tree .traverse (JupyterCellNode )
185
207
assert cell0 .children [0 ].attributes ["linenos" ]
186
- assert cell0 .children [0 ].attributes ["highlight_args" ]["linenostart" ] == 1
187
208
assert cell0 .children [0 ].rawsource .strip () == "2 + 2"
188
209
assert cell0 .children [1 ].rawsource .strip () == "4"
189
210
@@ -192,6 +213,34 @@ def test_continue_linenos_conf_option(doctree):
192
213
assert cell1 .children [0 ].rawsource .strip () == "3 + 3"
193
214
assert cell1 .children [1 ].rawsource .strip () == "6"
194
215
216
+ # Line number should continue after lineno-start option
217
+
218
+ source = """
219
+ .. jupyter-execute::
220
+ :lineno-start: 7
221
+
222
+ 2 + 2
223
+
224
+ .. jupyter-execute::
225
+
226
+ 3 + 3
227
+
228
+ """
229
+ tree = doctree (
230
+ source ,
231
+ config = "jupyter_sphinx_linenos = True\n "
232
+ "jupyter_sphinx_continue_linenos = True" ,
233
+ )
234
+ cell0 , cell1 = tree .traverse (JupyterCellNode )
235
+ assert cell0 .children [0 ].attributes ["highlight_args" ]["linenostart" ] == 7
236
+ assert cell0 .children [0 ].rawsource .strip () == "2 + 2"
237
+ assert cell0 .children [1 ].rawsource .strip () == "4"
238
+
239
+ assert cell1 .children [0 ].attributes ["linenos" ]
240
+ assert cell1 .children [0 ].attributes ["highlight_args" ]["linenostart" ] == 8
241
+ assert cell1 .children [0 ].rawsource .strip () == "3 + 3"
242
+ assert cell1 .children [1 ].rawsource .strip () == "6"
243
+
195
244
196
245
def test_emphasize_lines (doctree ):
197
246
source = """
@@ -272,7 +321,7 @@ def test_raises(doctree):
272
321
raise ValueError()
273
322
"""
274
323
tree = doctree (source )
275
- cell , = tree .traverse (JupyterCellNode )
324
+ ( cell ,) = tree .traverse (JupyterCellNode )
276
325
assert "ValueError" in cell .children [1 ].rawsource
277
326
278
327
source = """
@@ -282,7 +331,7 @@ def test_raises(doctree):
282
331
raise ValueError()
283
332
"""
284
333
tree = doctree (source )
285
- cell , = tree .traverse (JupyterCellNode )
334
+ ( cell ,) = tree .traverse (JupyterCellNode )
286
335
assert "ValueError" in cell .children [1 ].rawsource
287
336
288
337
@@ -306,8 +355,8 @@ def test_javascript(doctree):
306
355
Javascript('window.alert("Hello world!")')
307
356
"""
308
357
tree = doctree (source )
309
- node , = list (tree .traverse (raw ))
310
- text , = node .children
358
+ ( node ,) = list (tree .traverse (raw ))
359
+ ( text ,) = node .children
311
360
assert "world" in text
312
361
313
362
@@ -318,7 +367,7 @@ def test_stdout(doctree):
318
367
print('hello world')
319
368
"""
320
369
tree = doctree (source )
321
- cell , = tree .traverse (JupyterCellNode )
370
+ ( cell ,) = tree .traverse (JupyterCellNode )
322
371
assert len (cell .children ) == 2
323
372
assert cell .children [1 ].rawsource .strip () == "hello world"
324
373
@@ -333,7 +382,7 @@ def test_stderr(doctree):
333
382
334
383
tree , warnings = doctree (source , return_warnings = True )
335
384
assert "hello world" in warnings
336
- cell , = tree .traverse (JupyterCellNode )
385
+ ( cell ,) = tree .traverse (JupyterCellNode )
337
386
assert len (cell .children ) == 1 # no output
338
387
339
388
source = """
@@ -344,7 +393,7 @@ def test_stderr(doctree):
344
393
print('hello world', file=sys.stderr)
345
394
"""
346
395
tree = doctree (source )
347
- cell , = tree .traverse (JupyterCellNode )
396
+ ( cell ,) = tree .traverse (JupyterCellNode )
348
397
assert len (cell .children ) == 2
349
398
assert "stderr" in cell .children [1 ].attributes ["classes" ]
350
399
assert cell .children [1 ].astext ().strip () == "hello world"
@@ -361,7 +410,7 @@ def test_thebe_hide_output(doctree):
361
410
2 + 2
362
411
"""
363
412
tree = doctree (source , thebe_config )
364
- cell , = tree .traverse (JupyterCellNode )
413
+ ( cell ,) = tree .traverse (JupyterCellNode )
365
414
assert cell .attributes ["hide_output" ] is True
366
415
assert len (cell .children ) == 1
367
416
@@ -379,7 +428,7 @@ def test_thebe_hide_code(doctree):
379
428
2 + 2
380
429
"""
381
430
tree = doctree (source , thebe_config )
382
- cell , = tree .traverse (JupyterCellNode )
431
+ ( cell ,) = tree .traverse (JupyterCellNode )
383
432
assert cell .attributes ["hide_code" ] is True
384
433
assert len (cell .children ) == 2
385
434
@@ -403,7 +452,7 @@ def test_thebe_code_below(doctree):
403
452
2 + 2
404
453
"""
405
454
tree = doctree (source , thebe_config )
406
- cell , = tree .traverse (JupyterCellNode )
455
+ ( cell ,) = tree .traverse (JupyterCellNode )
407
456
assert cell .attributes ["code_below" ] is True
408
457
409
458
output = cell .children [0 ]
@@ -461,5 +510,5 @@ def test_latex(doctree):
461
510
462
511
for start , end in delimiter_pairs :
463
512
tree = doctree (source .format (start , end ))
464
- cell , = tree .traverse (JupyterCellNode )
513
+ ( cell ,) = tree .traverse (JupyterCellNode )
465
514
assert cell .children [1 ].astext () == r"\int"
0 commit comments