diff --git a/readme.md b/readme.md index 63b1083..ea4c3ce 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/src/sphinx_exec_code/__version__.py b/src/sphinx_exec_code/__version__.py index e46aee1..969399e 100644 --- a/src/sphinx_exec_code/__version__.py +++ b/src/sphinx_exec_code/__version__.py @@ -1 +1 @@ -__version__ = '0.9' +__version__ = '0.10' diff --git a/src/sphinx_exec_code/code_format.py b/src/sphinx_exec_code/code_format.py index dad23c3..94dcd22 100644 --- a/src/sphinx_exec_code/code_format.py +++ b/src/sphinx_exec_code/code_format.py @@ -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() diff --git a/tests/test_code_format.py b/tests/test_code_format.py index ebe024f..a690f69 100644 --- a/tests/test_code_format.py +++ b/tests/test_code_format.py @@ -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 == ''