Skip to content

Commit

Permalink
0.10 (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Feb 13, 2023
1 parent 7a064c4 commit 6526245
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ This code will be executed
```

# Changelog
#### 0.10 (2023-02-13)
- Fixed a bug when no code was shown/executed

#### 0.9 (2023-02-08)
- If the whole shown code block is indented the indention is removed

Expand Down
2 changes: 1 addition & 1 deletion src/sphinx_exec_code/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.9'
__version__ = '0.10'
32 changes: 17 additions & 15 deletions src/sphinx_exec_code/code_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,39 @@ def add_line(self, line: str):

self.lines.append(line)

def get_lines(self) -> List[str]:
# remove leading and tailing empty lines of the code
code_lines = self.lines
while code_lines and not code_lines[0].strip():
code_lines.pop(0)
while code_lines and not code_lines[-1].strip():
code_lines.pop(-1)
return code_lines


def get_show_exec_code(code_lines: Iterable[str]) -> Tuple[str, str]:
hide = CodeMarker('hide')
skip = CodeMarker('skip')
shown = CodeMarker('hide')
executed = CodeMarker('skip')

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

if hide.is_marker(line):
continue
if skip.is_marker(line):
if shown.is_marker(line) or executed.is_marker(line):
continue

add_line = org_line.rstrip()
hide.add_line(add_line)
skip.add_line(add_line)
shown.add_line(add_line)
executed.add_line(add_line)

# remove leading and tailing empty lines of the shown code
shown_lines = hide.lines
while shown_lines and not shown_lines[0].strip():
shown_lines.pop(0)
while shown_lines and not shown_lines[-1].strip():
shown_lines.pop(-1)
shown_lines = shown.get_lines()

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

shown_code = '\n'.join(shown_lines)
executed_code = '\n'.join(skip.lines)
executed_code = '\n'.join(executed.get_lines())

return shown_code, executed_code.strip()
20 changes: 20 additions & 0 deletions tests/test_code_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,23 @@ def test_code_indent():
assert show == "print('asdf')\n" \
" print('1234')\n" \
" # comment"


def test_code_split_empty():
show, run = get_show_exec_code([''])
assert show == ''
assert run == ''


def test_code_no_show():
code = '# - hide: start -\nprint("l1")\nprint("l2")'
show, run = get_show_exec_code(code.splitlines())
assert show == ''
assert run == 'print("l1")\nprint("l2")'


def test_code_no_exec():
code = '# - skip: start -\nprint(1 / 0)\nprint(2 / 0)'
show, run = get_show_exec_code(code.splitlines())
assert show == 'print(1 / 0)\nprint(2 / 0)'
assert run == ''

0 comments on commit 6526245

Please sign in to comment.