@@ -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 :
@@ -54,123 +56,53 @@ def doctree(source, config=None, return_warnings=False, entrypoint="jupyter_sphi
54
56
shutil .rmtree (tree )
55
57
56
58
57
- def test_basic (doctree ):
59
+ def test_continue_linenos_conf_option (doctree ):
60
+ # Test no linenumbering without linenos config or lineno-start directive
58
61
source = """
59
62
.. jupyter-execute::
60
63
61
64
2 + 2
62
- """
63
- tree = doctree (source )
64
- cell , = tree .traverse (JupyterCellNode )
65
- assert cell .attributes ["code_below" ] is False
66
- assert cell .attributes ["hide_code" ] is False
67
- assert cell .attributes ["hide_output" ] is False
68
- assert cell .attributes ["linenos" ] is False
69
- assert cell .children [0 ].rawsource .strip () == "2 + 2"
70
- assert cell .children [1 ].rawsource .strip () == "4"
71
-
72
-
73
- def test_basic_old_entrypoint (doctree ):
74
- source = """
75
- .. jupyter-execute::
76
65
77
- 2 + 2
78
66
"""
79
- tree = doctree (source , entrypoint = "jupyter_sphinx.execute" )
80
- cell , = tree .traverse (JupyterCellNode )
81
- assert cell .attributes ["code_below" ] is False
82
- assert cell .attributes ["hide_code" ] is False
83
- assert cell .attributes ["hide_output" ] is False
84
- assert cell .attributes ["linenos" ] is False
85
- assert cell .children [0 ].rawsource .strip () == "2 + 2"
86
- assert cell .children [1 ].rawsource .strip () == "4"
87
67
88
-
89
- def test_hide_output (doctree ):
90
- source = """
91
- .. jupyter-execute::
92
- :hide-output:
93
-
94
- 2 + 2
95
- """
96
- tree = doctree (source )
97
- cell , = tree .traverse (JupyterCellNode )
98
- assert cell .attributes ["hide_output" ] is True
99
- assert len (cell .children ) == 1
68
+ tree = doctree (source , config = "jupyter_sphinx_continue_linenos = True" )
69
+ (cell ,) = tree .traverse (JupyterCellNode )
70
+ assert "linenos" not in cell .children [0 ].attributes
100
71
assert cell .children [0 ].rawsource .strip () == "2 + 2"
72
+ assert cell .children [1 ].rawsource .strip () == "4"
101
73
102
-
103
- def test_hide_code (doctree ):
104
- source = """
105
- .. jupyter-execute::
106
- :hide-code:
107
-
108
- 2 + 2
109
- """
110
- tree = doctree (source )
111
- cell , = tree .traverse (JupyterCellNode )
112
- assert cell .attributes ["hide_code" ] is True
113
- assert len (cell .children ) == 1
114
- assert cell .children [0 ].rawsource .strip () == "4"
115
-
116
-
117
- def test_code_below (doctree ):
74
+ # Test continuous line numbeirng
118
75
source = """
119
76
.. jupyter-execute::
120
- :code-below:
121
77
122
78
2 + 2
123
- """
124
- tree = doctree (source )
125
- cell , = tree .traverse (JupyterCellNode )
126
- assert cell .attributes ["code_below" ] is True
127
- assert cell .children [0 ].rawsource .strip () == "4"
128
- assert cell .children [1 ].rawsource .strip () == "2 + 2"
129
79
130
-
131
- def test_linenos (doctree ):
132
- source = """
133
80
.. jupyter-execute::
134
- :linenos:
135
81
136
- 2 + 2
137
- """
138
- tree = doctree (source )
139
- cell , = tree .traverse (JupyterCellNode )
140
- assert cell .attributes ["linenos" ] is True
141
- assert len (cell .children ) == 2
142
- assert cell .children [0 ].rawsource .strip () == "2 + 2"
143
- assert cell .children [1 ].rawsource .strip () == "4"
144
- source = """
145
- .. jupyter-execute::
146
- :linenos:
147
- :code-below:
82
+ 3 + 3
148
83
149
- 2 + 2
150
84
"""
151
- tree = doctree (source )
152
- cell , = tree .traverse (JupyterCellNode )
153
- assert len (cell .children ) == 2
154
- assert cell .attributes ["linenos" ] is True
155
85
86
+ tree = doctree (
87
+ source ,
88
+ config = "jupyter_sphinx_linenos = True\n "
89
+ "jupyter_sphinx_continue_linenos = True" ,
90
+ )
156
91
157
- def test_linenos_conf_option (doctree ):
158
- source = """
159
- .. jupyter-execute::
160
-
161
- 2 + 2
162
- """
163
- tree = doctree (source , config = "jupyter_sphinx_linenos = True" )
164
- cell , = tree .traverse (JupyterCellNode )
165
- assert cell .children [0 ].attributes ["linenos" ]
166
- assert "highlight_args" not in cell .children [0 ].attributes
167
- assert cell .children [0 ].rawsource .strip () == "2 + 2"
168
- assert cell .children [1 ].rawsource .strip () == "4"
92
+ cell0 , cell1 = tree .traverse (JupyterCellNode )
93
+ assert cell0 .children [0 ].attributes ["linenos" ]
94
+ assert cell0 .children [0 ].rawsource .strip () == "2 + 2"
95
+ assert cell0 .children [1 ].rawsource .strip () == "4"
169
96
97
+ assert cell1 .children [0 ].attributes ["linenos" ]
98
+ assert cell1 .children [0 ].attributes ["highlight_args" ]["linenostart" ] == 2
99
+ assert cell1 .children [0 ].rawsource .strip () == "3 + 3"
100
+ assert cell1 .children [1 ].rawsource .strip () == "6"
170
101
171
- def test_continue_linenos_conf_option ( doctree ):
102
+ # Line number should continue after lineno-start option
172
103
source = """
173
104
.. jupyter-execute::
105
+ :lineno-start: 7
174
106
175
107
2 + 2
176
108
@@ -179,16 +111,18 @@ def test_continue_linenos_conf_option(doctree):
179
111
3 + 3
180
112
181
113
"""
182
- continue_linenos_config = "jupyter_sphinx_continue_linenos = True"
183
- tree = doctree (source , config = continue_linenos_config )
114
+ tree = doctree (
115
+ source ,
116
+ config = "jupyter_sphinx_linenos = True\n "
117
+ "jupyter_sphinx_continue_linenos = True" ,
118
+ )
184
119
cell0 , cell1 = tree .traverse (JupyterCellNode )
185
- assert cell0 .children [0 ].attributes ["linenos" ]
186
- assert cell0 .children [0 ].attributes ["highlight_args" ]["linenostart" ] == 1
120
+ assert cell0 .children [0 ].attributes ["highlight_args" ]["linenostart" ] == 7
187
121
assert cell0 .children [0 ].rawsource .strip () == "2 + 2"
188
122
assert cell0 .children [1 ].rawsource .strip () == "4"
189
123
190
124
assert cell1 .children [0 ].attributes ["linenos" ]
191
- assert cell1 .children [0 ].attributes ["highlight_args" ]["linenostart" ] == 2
125
+ assert cell1 .children [0 ].attributes ["highlight_args" ]["linenostart" ] == 8
192
126
assert cell1 .children [0 ].rawsource .strip () == "3 + 3"
193
127
assert cell1 .children [1 ].rawsource .strip () == "6"
194
128
@@ -272,7 +206,7 @@ def test_raises(doctree):
272
206
raise ValueError()
273
207
"""
274
208
tree = doctree (source )
275
- cell , = tree .traverse (JupyterCellNode )
209
+ ( cell ,) = tree .traverse (JupyterCellNode )
276
210
assert "ValueError" in cell .children [1 ].rawsource
277
211
278
212
source = """
@@ -282,7 +216,7 @@ def test_raises(doctree):
282
216
raise ValueError()
283
217
"""
284
218
tree = doctree (source )
285
- cell , = tree .traverse (JupyterCellNode )
219
+ ( cell ,) = tree .traverse (JupyterCellNode )
286
220
assert "ValueError" in cell .children [1 ].rawsource
287
221
288
222
@@ -306,8 +240,8 @@ def test_javascript(doctree):
306
240
Javascript('window.alert("Hello world!")')
307
241
"""
308
242
tree = doctree (source )
309
- node , = list (tree .traverse (raw ))
310
- text , = node .children
243
+ ( node ,) = list (tree .traverse (raw ))
244
+ ( text ,) = node .children
311
245
assert "world" in text
312
246
313
247
@@ -318,7 +252,7 @@ def test_stdout(doctree):
318
252
print('hello world')
319
253
"""
320
254
tree = doctree (source )
321
- cell , = tree .traverse (JupyterCellNode )
255
+ ( cell ,) = tree .traverse (JupyterCellNode )
322
256
assert len (cell .children ) == 2
323
257
assert cell .children [1 ].rawsource .strip () == "hello world"
324
258
@@ -333,7 +267,7 @@ def test_stderr(doctree):
333
267
334
268
tree , warnings = doctree (source , return_warnings = True )
335
269
assert "hello world" in warnings
336
- cell , = tree .traverse (JupyterCellNode )
270
+ ( cell ,) = tree .traverse (JupyterCellNode )
337
271
assert len (cell .children ) == 1 # no output
338
272
339
273
source = """
@@ -344,7 +278,7 @@ def test_stderr(doctree):
344
278
print('hello world', file=sys.stderr)
345
279
"""
346
280
tree = doctree (source )
347
- cell , = tree .traverse (JupyterCellNode )
281
+ ( cell ,) = tree .traverse (JupyterCellNode )
348
282
assert len (cell .children ) == 2
349
283
assert "stderr" in cell .children [1 ].attributes ["classes" ]
350
284
assert cell .children [1 ].astext ().strip () == "hello world"
@@ -361,7 +295,7 @@ def test_thebe_hide_output(doctree):
361
295
2 + 2
362
296
"""
363
297
tree = doctree (source , thebe_config )
364
- cell , = tree .traverse (JupyterCellNode )
298
+ ( cell ,) = tree .traverse (JupyterCellNode )
365
299
assert cell .attributes ["hide_output" ] is True
366
300
assert len (cell .children ) == 1
367
301
@@ -379,7 +313,7 @@ def test_thebe_hide_code(doctree):
379
313
2 + 2
380
314
"""
381
315
tree = doctree (source , thebe_config )
382
- cell , = tree .traverse (JupyterCellNode )
316
+ ( cell ,) = tree .traverse (JupyterCellNode )
383
317
assert cell .attributes ["hide_code" ] is True
384
318
assert len (cell .children ) == 2
385
319
@@ -403,7 +337,7 @@ def test_thebe_code_below(doctree):
403
337
2 + 2
404
338
"""
405
339
tree = doctree (source , thebe_config )
406
- cell , = tree .traverse (JupyterCellNode )
340
+ ( cell ,) = tree .traverse (JupyterCellNode )
407
341
assert cell .attributes ["code_below" ] is True
408
342
409
343
output = cell .children [0 ]
@@ -461,5 +395,5 @@ def test_latex(doctree):
461
395
462
396
for start , end in delimiter_pairs :
463
397
tree = doctree (source .format (start , end ))
464
- cell , = tree .traverse (JupyterCellNode )
398
+ ( cell ,) = tree .traverse (JupyterCellNode )
465
399
assert cell .children [1 ].astext () == r"\int"
0 commit comments