Skip to content

Commit 62ed21f

Browse files
committed
store the line number configuration in the literal block
Closes #142
1 parent 96c16bf commit 62ed21f

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

jupyter_sphinx/ast.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ def run(self):
136136
hide_code=("hide-code" in self.options),
137137
hide_output=("hide-output" in self.options),
138138
code_below=("code-below" in self.options),
139-
linenos=("linenos" in self.options),
140-
linenostart=(self.options.get("lineno-start")),
141139
emphasize_lines=hl_lines,
142140
raises=self.options.get("raises"),
143141
stderr=("stderr" in self.options),
@@ -146,7 +144,11 @@ def run(self):
146144

147145
# Add the input section of the cell, we'll add output at execution time
148146
cell_input = CellInputNode(classes=["cell_input"])
149-
cell_input += docutils.nodes.literal_block(text="\n".join(content))
147+
cell_input += docutils.nodes.literal_block(
148+
text="\n".join(content),
149+
linenos=("linenos" in self.options),
150+
linenostart=(self.options.get("lineno-start")),
151+
)
150152
cell_node += cell_input
151153
return [cell_node]
152154

jupyter_sphinx/execute.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,18 @@ def apply(self):
182182
linenostart = 1
183183

184184
for node in nodes:
185-
source = node.children[0]
185+
# The literal_block node with the source
186+
source = node.children[0].children[0]
186187
nlines = source.rawsource.count("\n") + 1
187188
show_numbering = (
188-
linenos_config or node["linenos"] or node["linenostart"]
189+
linenos_config or source["linenos"] or source["linenostart"]
189190
)
190191

191192
if show_numbering:
192193
source["linenos"] = True
193-
if node["linenostart"]:
194-
linenostart = node["linenostart"]
195-
if node["linenostart"] or continue_linenos:
194+
if source["linenostart"]:
195+
linenostart = source["linenostart"]
196+
if source["linenostart"] or continue_linenos:
196197
source["highlight_args"] = {"linenostart": linenostart}
197198
else:
198199
linenostart = 1

tests/test_execute.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_basic(doctree, buildername):
8787
assert cell.attributes["code_below"] is False
8888
assert cell.attributes["hide_code"] is False
8989
assert cell.attributes["hide_output"] is False
90-
assert cell.attributes["linenos"] is False
90+
assert cellinput.children[0]["linenos"] is False
9191
assert cellinput.children[0].rawsource.strip() == "2 + 2"
9292
assert celloutput.children[0].rawsource.strip() == "4"
9393

@@ -104,7 +104,7 @@ def test_basic_old_entrypoint(doctree):
104104
assert cell.attributes["code_below"] is False
105105
assert cell.attributes["hide_code"] is False
106106
assert cell.attributes["hide_output"] is False
107-
assert cell.attributes["linenos"] is False
107+
assert cellinput.children[0]["linenos"] is False
108108
assert cellinput.children[0].rawsource.strip() == "2 + 2"
109109
assert celloutput.children[0].rawsource.strip() == "4"
110110

@@ -164,7 +164,7 @@ def test_linenos(doctree):
164164
tree = doctree(source)
165165
(cell,) = tree.traverse(JupyterCellNode)
166166
(cellinput, celloutput) = cell.children
167-
assert cell.attributes["linenos"] is True
167+
assert cellinput.children[0]["linenos"] is True
168168
assert len(cell.children) == 2
169169
assert cellinput.children[0].rawsource.strip() == "2 + 2"
170170
assert celloutput.children[0].rawsource.strip() == "4"
@@ -177,9 +177,8 @@ def test_linenos(doctree):
177177
"""
178178
tree = doctree(source)
179179
(cell,) = tree.traverse(JupyterCellNode)
180-
(cellinput, celloutput) = cell.children
181-
assert len(cell.children) == 2
182-
assert cell.attributes["linenos"] is True
180+
(celloutput, cellinput) = cell.children
181+
assert cellinput.children[0]["linenos"] is True
183182

184183

185184
def test_linenos_conf_option(doctree):
@@ -191,8 +190,8 @@ def test_linenos_conf_option(doctree):
191190
tree = doctree(source, config="jupyter_sphinx_linenos = True")
192191
(cell,) = tree.traverse(JupyterCellNode)
193192
(cellinput, celloutput) = cell.children
194-
assert cellinput.attributes["linenos"]
195-
assert "highlight_args" not in cellinput.attributes
193+
assert cellinput.children[0].attributes["linenos"]
194+
assert "highlight_args" not in cellinput.children[0].attributes
196195
assert cellinput.children[0].rawsource.strip() == "2 + 2"
197196
assert celloutput.children[0].rawsource.strip() == "4"
198197

@@ -209,7 +208,7 @@ def test_continue_linenos_conf_option(doctree):
209208
tree = doctree(source, config="jupyter_sphinx_continue_linenos = True")
210209
(cell,) = tree.traverse(JupyterCellNode)
211210
(cellinput, celloutput) = cell.children
212-
assert "linenos" not in cellinput.attributes
211+
assert not cellinput.children[0].attributes["linenos"]
213212
assert cellinput.children[0].rawsource.strip() == "2 + 2"
214213
assert celloutput.children[0].rawsource.strip() == "4"
215214

@@ -234,12 +233,12 @@ def test_continue_linenos_conf_option(doctree):
234233
cell0, cell1 = tree.traverse(JupyterCellNode)
235234
(cellinput0, celloutput0) = cell0.children
236235
(cellinput1, celloutput1) = cell1.children
237-
assert cellinput0.attributes["linenos"]
236+
assert cellinput0.children[0].attributes["linenos"]
238237
assert cellinput0.children[0].rawsource.strip() == "2 + 2"
239238
assert celloutput0.children[0].rawsource.strip() == "4"
240239

241-
assert cellinput1.attributes["linenos"]
242-
assert cellinput1.attributes["highlight_args"]["linenostart"] == 2
240+
assert cellinput1.children[0].attributes["linenos"]
241+
assert cellinput1.children[0].attributes["highlight_args"]["linenostart"] == 2
243242
assert cellinput1.children[0].rawsource.strip() == "3 + 3"
244243
assert celloutput1.children[0].rawsource.strip() == "6"
245244

@@ -264,12 +263,12 @@ def test_continue_linenos_conf_option(doctree):
264263
cell0, cell1 = tree.traverse(JupyterCellNode)
265264
(cellinput0, celloutput0) = cell0.children
266265
(cellinput1, celloutput1) = cell1.children
267-
assert cellinput0.attributes["highlight_args"]["linenostart"] == 7
266+
assert cellinput0.children[0].attributes["highlight_args"]["linenostart"] == 7
268267
assert cellinput0.children[0].rawsource.strip() == "2 + 2"
269268
assert celloutput0.children[0].rawsource.strip() == "4"
270269

271-
assert cellinput1.attributes["linenos"]
272-
assert cellinput1.attributes["highlight_args"]["linenostart"] == 8
270+
assert cellinput1.children[0].attributes["linenos"]
271+
assert cellinput1.children[0].attributes["highlight_args"]["linenostart"] == 8
273272
assert cellinput1.children[0].rawsource.strip() == "3 + 3"
274273
assert celloutput1.children[0].rawsource.strip() == "6"
275274

0 commit comments

Comments
 (0)