Skip to content

Commit 03eb760

Browse files
0.14 (#15)
1 parent 1c46b7c commit 03eb760

File tree

9 files changed

+238
-149
lines changed

9 files changed

+238
-149
lines changed

doc/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
# -- Project information -----------------------------------------------------
2626
project = 'sphinx-exec-code'
27-
copyright = '2021, spacemanspiff2007'
27+
copyright = '2024, spacemanspiff2007'
2828
author = 'spacemanspiff2007'
2929

3030
# The full version, including alpha/beta/rc tags

doc/description.rst

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,21 @@ Generated view
2323
Options
2424
------------------------------
2525
It's possible to further configure both the code block and the output block with the following options:
26+
`See sphinx docs <https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-code>`_
27+
for a detailed description
2628

27-
28-
hide_code/hide_output:
29+
hide/hide_output:
2930
Will hide the corresponding block
31+
name/name_output
32+
Define implicit target name that can be referenced by using ref
3033
caption/caption_output
3134
Will add a caption above the block
3235
linenos/linenos_output
3336
Will add line numbers
37+
lineno-start/lineno-start_output
38+
Set the first line number of the block. Linenos is also automatically activated
39+
emphasize-lines/emphasize-lines_output
40+
Emphasize particular lines of the block
3441
language/language_output:
3542
| Will add syntax highlighting for the specified language
3643
| The default for the code block is python, the default for the output block is plain text
@@ -56,7 +63,7 @@ Generated view
5663
:hide_output:
5764
:caption: This is an important caption
5865

59-
print('Easy!')
66+
print('Easy!')
6067

6168
----
6269

@@ -171,3 +178,54 @@ Generated view
171178
----
172179

173180
With the combination of ``skip`` and ``hide`` it's possible to "simulate" every code.
181+
182+
183+
Further Examples
184+
------------------------------
185+
186+
This is an example with captions, highlights and name.
187+
188+
189+
.. code-block:: python
190+
191+
.. exec_code::
192+
:lineno-start: 5
193+
:emphasize-lines: 1, 4
194+
:caption: This is an important caption
195+
:caption_output: This is an important output caption
196+
:name: my_example_1
197+
:name_output: my_output_1
198+
199+
print('My')
200+
# This is a comment
201+
202+
print('Output!')
203+
204+
Generated view
205+
206+
----
207+
208+
.. exec_code::
209+
:lineno-start: 5
210+
:emphasize-lines: 1, 4
211+
:caption: This is an important caption
212+
:caption_output: This is an important output caption
213+
:name: my_example_1
214+
:name_output: my_output_1
215+
216+
print('My')
217+
# This is a comment
218+
219+
print('Output!')
220+
221+
----
222+
223+
Create a link using to the blocks by using the name:
224+
225+
.. code-block:: text
226+
227+
See :ref:`this code snippet <my_example_1>` for an example
228+
See :ref:`this code snippet <my_output_1>` for an example output
229+
230+
See :ref:`this code snippet <my_example_1>` for an example
231+
See :ref:`this code snippet <my_output_1>` for an example output

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ This code will be executed
3636
```
3737

3838
# Changelog
39+
#### 0.14 (2024-11-15)
40+
- Add support for all options from code block
41+
- Reworked how blocks and options are processed
42+
43+
#### 0.13 (2024-10-15)
44+
- Add support for python 3.13
45+
3946
#### 0.12 (2024-01-09)
4047
- Error when providing invalid options
4148

src/sphinx_exec_code/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.13'
1+
__version__ = '0.14'

src/sphinx_exec_code/code_format.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from typing import Iterable, List, Tuple
1+
from textwrap import dedent
2+
from typing import List, Tuple
3+
4+
from docutils.statemachine import StringList
25

36

47
class VisibilityMarkerError(Exception):
@@ -61,7 +64,7 @@ def get_lines(self) -> List[str]:
6164
return code_lines
6265

6366

64-
def get_show_exec_code(code_lines: Iterable[str]) -> Tuple[str, str]:
67+
def get_show_exec_code(code_lines: StringList) -> Tuple[str, str]:
6568
shown = CodeMarker('hide')
6669
executed = CodeMarker('skip')
6770

@@ -77,13 +80,7 @@ def get_show_exec_code(code_lines: Iterable[str]) -> Tuple[str, str]:
7780

7881
shown_lines = shown.get_lines()
7982

80-
# check if the shown code block is indented as a whole -> strip
81-
leading_spaces = [len(line) - len(line.lstrip()) for line in shown_lines]
82-
if strip_spaces := min(leading_spaces, default=0):
83-
for i, line in enumerate(shown_lines):
84-
shown_lines[i] = line[strip_spaces:]
85-
8683
shown_code = '\n'.join(shown_lines)
8784
executed_code = '\n'.join(executed.get_lines())
8885

89-
return shown_code, executed_code.strip()
86+
return dedent(shown_code), dedent(executed_code.strip())

src/sphinx_exec_code/sphinx_exec.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import traceback
22
from pathlib import Path
3-
from typing import List
43

5-
from docutils import nodes
4+
from docutils.statemachine import StringList
5+
from sphinx.directives.code import CodeBlock
66
from sphinx.errors import ExtensionError
77
from sphinx.util.docutils import SphinxDirective
88

@@ -13,24 +13,6 @@
1313
from sphinx_exec_code.sphinx_spec import SphinxSpecBase, build_spec, get_specs
1414

1515

16-
def create_literal_block(objs: list, code: str, spec: SphinxSpecBase) -> None:
17-
if spec.hide or not code:
18-
return None
19-
20-
# generate header if specified
21-
if spec.caption:
22-
objs.append(nodes.caption(text=spec.caption))
23-
24-
# generate code block
25-
block = nodes.literal_block(code, code)
26-
objs.append(block)
27-
28-
# set linenos
29-
block['linenos'] = spec.linenos
30-
block['language'] = spec.language
31-
return None
32-
33-
3416
class ExecCode(SphinxDirective):
3517
""" Sphinx class for execute_code directive
3618
"""
@@ -57,7 +39,7 @@ def run(self) -> list:
5739
msg = f'Error while running {name}!'
5840
raise ExtensionError(msg, orig_exc=e) from None
5941

60-
def _get_code_line(self, line_no: int, content: List[str]) -> int:
42+
def _get_code_line(self, line_no: int, content: StringList) -> int:
6143
"""Get the first line number of the code"""
6244
if not content:
6345
return line_no
@@ -99,7 +81,7 @@ def _run(self) -> list:
9981
raise ExtensionError(msg, orig_exc=e) from None
10082

10183
# Show the code from the user
102-
create_literal_block(output, code_show, spec=code_spec)
84+
self.create_literal_block(output, code_show, code_spec, line)
10385

10486
try:
10587
code_results = execute_code(code_exec, file, line)
@@ -115,5 +97,22 @@ def _run(self) -> list:
11597
raise ExtensionError(msg) from None
11698

11799
# Show the output from the code execution
118-
create_literal_block(output, code_results, spec=output_spec)
100+
self.create_literal_block(output, code_results, output_spec, line)
119101
return output
102+
103+
def create_literal_block(self, objs: list, code: str, spec: SphinxSpecBase, line: int) -> None:
104+
if spec.hide or not code:
105+
return None
106+
107+
c = CodeBlock(
108+
'code-block', [spec.language], spec.spec,
109+
StringList(code.splitlines()),
110+
line,
111+
# I'm not sure what these two do
112+
self.content_offset, '',
113+
# Let's hope these are just for producing error messages and not for anything else
114+
self.state, self.state_machine
115+
)
116+
117+
objs.extend(c.run())
118+
return None

0 commit comments

Comments
 (0)