Skip to content

Commit 6526245

Browse files
0.10 (#8)
1 parent 7a064c4 commit 6526245

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

readme.md

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

3838
# Changelog
39+
#### 0.10 (2023-02-13)
40+
- Fixed a bug when no code was shown/executed
41+
3942
#### 0.9 (2023-02-08)
4043
- If the whole shown code block is indented the indention is removed
4144

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.9'
1+
__version__ = '0.10'

src/sphinx_exec_code/code_format.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,39 @@ def add_line(self, line: str):
5151

5252
self.lines.append(line)
5353

54+
def get_lines(self) -> List[str]:
55+
# remove leading and tailing empty lines of the code
56+
code_lines = self.lines
57+
while code_lines and not code_lines[0].strip():
58+
code_lines.pop(0)
59+
while code_lines and not code_lines[-1].strip():
60+
code_lines.pop(-1)
61+
return code_lines
62+
5463

5564
def get_show_exec_code(code_lines: Iterable[str]) -> Tuple[str, str]:
56-
hide = CodeMarker('hide')
57-
skip = CodeMarker('skip')
65+
shown = CodeMarker('hide')
66+
executed = CodeMarker('skip')
5867

5968
for org_line in code_lines:
6069
line = org_line.replace(' ', '').replace('-', '').lower()
6170

62-
if hide.is_marker(line):
63-
continue
64-
if skip.is_marker(line):
71+
if shown.is_marker(line) or executed.is_marker(line):
6572
continue
6673

6774
add_line = org_line.rstrip()
68-
hide.add_line(add_line)
69-
skip.add_line(add_line)
75+
shown.add_line(add_line)
76+
executed.add_line(add_line)
7077

71-
# remove leading and tailing empty lines of the shown code
72-
shown_lines = hide.lines
73-
while shown_lines and not shown_lines[0].strip():
74-
shown_lines.pop(0)
75-
while shown_lines and not shown_lines[-1].strip():
76-
shown_lines.pop(-1)
78+
shown_lines = shown.get_lines()
7779

7880
# check if the shown code block is indented as a whole -> strip
7981
leading_spaces = [len(line) - len(line.lstrip()) for line in shown_lines]
80-
if strip_spaces := min(leading_spaces):
82+
if strip_spaces := min(leading_spaces, default=0):
8183
for i, line in enumerate(shown_lines):
8284
shown_lines[i] = line[strip_spaces:]
8385

8486
shown_code = '\n'.join(shown_lines)
85-
executed_code = '\n'.join(skip.lines)
87+
executed_code = '\n'.join(executed.get_lines())
8688

8789
return shown_code, executed_code.strip()

tests/test_code_format.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,23 @@ def test_code_indent():
3737
assert show == "print('asdf')\n" \
3838
" print('1234')\n" \
3939
" # comment"
40+
41+
42+
def test_code_split_empty():
43+
show, run = get_show_exec_code([''])
44+
assert show == ''
45+
assert run == ''
46+
47+
48+
def test_code_no_show():
49+
code = '# - hide: start -\nprint("l1")\nprint("l2")'
50+
show, run = get_show_exec_code(code.splitlines())
51+
assert show == ''
52+
assert run == 'print("l1")\nprint("l2")'
53+
54+
55+
def test_code_no_exec():
56+
code = '# - skip: start -\nprint(1 / 0)\nprint(2 / 0)'
57+
show, run = get_show_exec_code(code.splitlines())
58+
assert show == 'print(1 / 0)\nprint(2 / 0)'
59+
assert run == ''

0 commit comments

Comments
 (0)