Skip to content

Commit 2dd873f

Browse files
authored
Merge pull request #83 from seanpue/continue_linenos_setting
Continue linenos through setting
2 parents 9398627 + 38649ff commit 2dd873f

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

doc/source/index.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ produces:
193193
print('B')
194194
print('C')
195195

196-
197196
Controlling exceptions
198197
----------------------
199198

@@ -347,3 +346,11 @@ jupyter_execute_kwargs
347346

348347
Keyword arguments to pass to ``nbconvert.preprocessors.execute.executenb``, which controls how
349348
code cells are executed. The default is ``dict(timeout=-1, allow_errors=True)``.
349+
350+
jupyter_sphinx_linenos
351+
352+
Whether to show line numbering in all ``jupyter-execute`` sources.
353+
354+
jupyter_sphinx_continue_linenos
355+
356+
Whether to show continuous line numbering in all ``jupyter-execute`` sources.

jupyter_sphinx/execute.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class JupyterCell(Directive):
135135
code-below : bool
136136
If provided, the code will be shown below the cell output.
137137
linenos : bool
138-
If provided, the code will be shown with line numbers.
138+
If provided, the code will be shown with line numbering.
139139
raises : comma separated list of exception types
140140
If provided, a comma-separated list of exception type names that
141141
the cell may raise. If one of the listed execption types is raised
@@ -332,7 +332,8 @@ def apply(self):
332332
default_kernel = self.config.jupyter_execute_default_kernel
333333
default_names = default_notebook_names(docname)
334334
thebe_config = self.config.jupyter_sphinx_thebelab_config
335-
335+
linenos_config = self.config.jupyter_sphinx_linenos
336+
continue_linenos = self.config.jupyter_sphinx_continue_linenos
336337
# Check if we have anything to execute.
337338
if not doctree.traverse(JupyterCellNode):
338339
return
@@ -399,11 +400,19 @@ def apply(self):
399400
source = node.children[0]
400401
source.attributes['language'] = lexer
401402

402-
# Add line numbers
403+
# Add line numbers to code cells if jupyter_sphinx_linenos or
404+
# jupyter_sphinx_continue_linenos are set in the configuration,
405+
# or the linenos directive is set.
406+
# Update current line numbers from cell if jupyter_sphinx_continue_linenos
407+
# is set.
408+
linenostart = 1
403409
for node in nodes:
404-
if node["linenos"]:
405-
source = node.children[0]
410+
source = node.children[0]
411+
if linenos_config or continue_linenos or node["linenos"]:
406412
source["linenos"] = True
413+
if continue_linenos:
414+
source["highlight_args"] = {'linenostart': linenostart}
415+
linenostart += source.rawsource.count("\n") + 1
407416

408417

409418
# Add code cell CSS class
@@ -820,6 +829,10 @@ def setup(app):
820829

821830
app.add_config_value('jupyter_sphinx_thebelab_url', THEBELAB_URL_DEFAULT, 'html')
822831

832+
# linenos config
833+
app.add_config_value('jupyter_sphinx_linenos', False, 'env')
834+
app.add_config_value('jupyter_sphinx_continue_linenos', False, 'env')
835+
823836
# Used for nodes that do not need to be rendered
824837
def skip(self, node):
825838
raise docutils.nodes.SkipNode

tests/test_execute.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,45 @@ def test_linenos(doctree):
141141
assert cell.attributes['linenos'] is True
142142

143143

144+
def test_linenos_conf_option(doctree):
145+
source = '''
146+
.. jupyter-execute::
147+
148+
2 + 2
149+
'''
150+
tree = doctree(source, config="jupyter_sphinx_linenos = True")
151+
cell, = tree.traverse(JupyterCellNode)
152+
assert cell.children[0].attributes['linenos']
153+
assert 'highlight_args' not in cell.children[0].attributes
154+
assert cell.children[0].rawsource.strip() == "2 + 2"
155+
assert cell.children[1].rawsource.strip() == "4"
156+
157+
158+
def test_continue_linenos_conf_option(doctree):
159+
source = '''
160+
.. jupyter-execute::
161+
162+
2 + 2
163+
164+
.. jupyter-execute::
165+
166+
3 + 3
167+
168+
'''
169+
continue_linenos_config = "jupyter_sphinx_continue_linenos = True"
170+
tree = doctree(source, config=continue_linenos_config)
171+
cell0, cell1 = tree.traverse(JupyterCellNode)
172+
assert cell0.children[0].attributes['linenos']
173+
assert cell0.children[0].attributes['highlight_args']['linenostart'] == 1
174+
assert cell0.children[0].rawsource.strip() == "2 + 2"
175+
assert cell0.children[1].rawsource.strip() == "4"
176+
177+
assert cell1.children[0].attributes['linenos']
178+
assert cell1.children[0].attributes['highlight_args']['linenostart'] == 2
179+
assert cell1.children[0].rawsource.strip() == "3 + 3"
180+
assert cell1.children[1].rawsource.strip() == "6"
181+
182+
144183
def test_execution_environment_carries_over(doctree):
145184
source = '''
146185
.. jupyter-execute::

0 commit comments

Comments
 (0)