Skip to content

Commit 0661045

Browse files
authored
Merge pull request #154 from akhmerov/issue-142
store the line number configuration in the literal block
2 parents 96c16bf + f1e515b commit 0661045

File tree

4 files changed

+46
-37
lines changed

4 files changed

+46
-37
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/css/jupyter-sphinx.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ div.jupyter_container {
6262
padding: 0;
6363
margin: 0;
6464
}
65+
66+
/* Prevent alabaster breaking highlight alignment */
67+
div.jupyter_container .hll {
68+
padding: 0;
69+
margin: 0;
70+
}
71+
6572
/* overrides for sphinx_rtd_theme */
6673
.rst-content .jupyter_container div[class^='highlight'],
6774
.document .jupyter_container div[class^='highlight'],

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: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ def test_basic(doctree, buildername):
8484
tree = doctree(source, buildername=buildername)
8585
(cell,) = tree.traverse(JupyterCellNode)
8686
(cellinput, celloutput) = cell.children
87-
assert cell.attributes["code_below"] is False
88-
assert cell.attributes["hide_code"] is False
89-
assert cell.attributes["hide_output"] is False
90-
assert cell.attributes["linenos"] is False
87+
assert not cell.attributes["code_below"]
88+
assert not cell.attributes["hide_code"]
89+
assert not cell.attributes["hide_output"]
90+
assert not cellinput.children[0]["linenos"]
9191
assert cellinput.children[0].rawsource.strip() == "2 + 2"
9292
assert celloutput.children[0].rawsource.strip() == "4"
9393

@@ -101,10 +101,10 @@ def test_basic_old_entrypoint(doctree):
101101
tree = doctree(source, entrypoint="jupyter_sphinx.execute")
102102
(cell,) = tree.traverse(JupyterCellNode)
103103
(cellinput, celloutput) = cell.children
104-
assert cell.attributes["code_below"] is False
105-
assert cell.attributes["hide_code"] is False
106-
assert cell.attributes["hide_output"] is False
107-
assert cell.attributes["linenos"] is False
104+
assert not cell.attributes["code_below"]
105+
assert not cell.attributes["hide_code"]
106+
assert not cell.attributes["hide_output"]
107+
assert not cellinput.children[0]["linenos"]
108108
assert cellinput.children[0].rawsource.strip() == "2 + 2"
109109
assert celloutput.children[0].rawsource.strip() == "4"
110110

@@ -119,7 +119,7 @@ def test_hide_output(doctree):
119119
tree = doctree(source)
120120
(cell,) = tree.traverse(JupyterCellNode)
121121
(cellinput, celloutput) = cell.children
122-
assert cell.attributes["hide_output"] is True
122+
assert cell.attributes["hide_output"]
123123
assert len(celloutput.children) == 0
124124
assert cellinput.children[0].rawsource.strip() == "2 + 2"
125125

@@ -134,7 +134,7 @@ def test_hide_code(doctree):
134134
tree = doctree(source)
135135
(cell,) = tree.traverse(JupyterCellNode)
136136
(celloutput,) = cell.children
137-
assert cell.attributes["hide_code"] is True
137+
assert cell.attributes["hide_code"]
138138
assert len(cell.children) == 1
139139
assert celloutput.children[0].rawsource.strip() == "4"
140140

@@ -149,7 +149,7 @@ def test_code_below(doctree):
149149
tree = doctree(source)
150150
(cell,) = tree.traverse(JupyterCellNode)
151151
(celloutput, cellinput) = cell.children
152-
assert cell.attributes["code_below"] is True
152+
assert cell.attributes["code_below"]
153153
assert cellinput.children[0].rawsource.strip() == "2 + 2"
154154
assert celloutput.children[0].rawsource.strip() == "4"
155155

@@ -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"]
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"]
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

@@ -455,7 +454,7 @@ def test_thebe_hide_output(doctree):
455454
tree = doctree(source, thebe_config)
456455
(cell,) = tree.traverse(JupyterCellNode)
457456
(cellinput, celloutput) = cell.children
458-
assert cell.attributes["hide_output"] is True
457+
assert cell.attributes["hide_output"]
459458
assert len(celloutput.children) == 0
460459

461460
source = cellinput.children[0]
@@ -474,12 +473,12 @@ def test_thebe_hide_code(doctree):
474473
tree = doctree(source, thebe_config)
475474
(cell,) = tree.traverse(JupyterCellNode)
476475
(cellinput, celloutput) = cell.children
477-
assert cell.attributes["hide_code"] is True
476+
assert cell.attributes["hide_code"]
478477
assert len(cell.children) == 2
479478

480479
source = cellinput.children[0]
481480
assert type(source) == ThebeSourceNode
482-
assert source.attributes["hide_code"] is True
481+
assert source.attributes["hide_code"]
483482
assert len(source.children) == 1
484483
assert source.children[0].rawsource.strip() == "2 + 2"
485484

@@ -499,7 +498,7 @@ def test_thebe_code_below(doctree):
499498
tree = doctree(source, thebe_config)
500499
(cell,) = tree.traverse(JupyterCellNode)
501500
(cellinput, celloutput) = cell.children
502-
assert cell.attributes["code_below"] is True
501+
assert cell.attributes["code_below"]
503502

504503
output = cellinput.children[0]
505504
assert type(output) is ThebeOutputNode
@@ -510,7 +509,7 @@ def test_thebe_code_below(doctree):
510509
assert type(source) is ThebeSourceNode
511510
assert len(source.children) == 1
512511
assert source.children[0].rawsource.strip() == "2 + 2"
513-
assert source.attributes["code_below"] is True
512+
assert source.attributes["code_below"]
514513

515514

516515
def test_thebe_button_auto(doctree):

0 commit comments

Comments
 (0)