Skip to content

Commit 693db1b

Browse files
committed
added linenos flag, updated docs, and basic tests
1 parent 135e954 commit 693db1b

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.tox/
12
*.py[cod]
23

34
# Packages

doc/source/index.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,24 @@ produces:
175175

176176
print('this code is below the output')
177177

178+
You may also add *line numbers* to the source code with ``:linenos:``::
179+
180+
.. jupyter-execute::
181+
:linenos:
182+
183+
print('A')
184+
print('B')
185+
print('C')
186+
187+
produces:
188+
189+
.. jupyter-execute::
190+
:linenos:
191+
192+
print('A')
193+
print('B')
194+
print('C')
195+
178196

179197
Controlling exceptions
180198
----------------------

jupyter_sphinx/execute.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ class JupyterCell(Directive):
153153
'hide-code': directives.flag,
154154
'hide-output': directives.flag,
155155
'code-below': directives.flag,
156+
'linenos': directives.flag,
156157
'raises': csv_option,
157158
'stderr': directives.flag,
158159
}
@@ -187,6 +188,7 @@ def run(self):
187188
hide_code=('hide-code' in self.options),
188189
hide_output=('hide-output' in self.options),
189190
code_below=('code-below' in self.options),
191+
linenos=('linenos' in self.options),
190192
raises=self.options.get('raises'),
191193
stderr=('stderr' in self.options),
192194
)]
@@ -392,6 +394,12 @@ def apply(self):
392394
source = node.children[0]
393395
source.attributes['language'] = lexer
394396

397+
# Add line numbers
398+
for node in nodes:
399+
if node["linenos"]:
400+
source = node.children[0]
401+
source["linenos"] = True
402+
395403
# Write certain cell outputs (e.g. images) to separate files, and
396404
# modify the metadata of the associated cells in 'notebook' to
397405
# include the path to the output file.

tests/test_execute.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def test_basic(doctree):
6868
assert cell.attributes['code_below'] is False
6969
assert cell.attributes['hide_code'] is False
7070
assert cell.attributes['hide_output'] is False
71+
assert cell.attributes['linenos'] is False
7172
assert cell.children[0].rawsource.strip() == "2 + 2"
7273
assert cell.children[1].rawsource.strip() == "4"
7374

@@ -114,6 +115,32 @@ def test_code_below(doctree):
114115
assert cell.children[1].rawsource.strip() == "2 + 2"
115116

116117

118+
def test_linenos(doctree):
119+
source = '''
120+
.. jupyter-execute::
121+
:linenos:
122+
123+
2 + 2
124+
'''
125+
tree = doctree(source)
126+
cell, = tree.traverse(JupyterCellNode)
127+
assert cell.attributes['linenos'] is True
128+
assert len(cell.children) == 2
129+
assert cell.children[0].rawsource.strip() == "2 + 2"
130+
assert cell.children[1].rawsource.strip() == "4"
131+
source = '''
132+
.. jupyter-execute::
133+
:linenos:
134+
:code-below:
135+
136+
2 + 2
137+
'''
138+
tree = doctree(source)
139+
cell, = tree.traverse(JupyterCellNode)
140+
assert len(cell.children) == 2
141+
assert cell.attributes['linenos'] is True
142+
143+
117144
def test_execution_environment_carries_over(doctree):
118145
source = '''
119146
.. jupyter-execute::

0 commit comments

Comments
 (0)