Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions myst_nb/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from nbformat import NotebookNode

_RGX_CARRIAGERETURN = re.compile(r".*\r(?=[^\n])")
_RGX_BACKSPACE = re.compile(r"[^\n]\b")
_RGX_BACKSPACE = re.compile(r"[^\n\x08]\x08")


def coalesce_streams(outputs: list[NotebookNode]) -> list[NotebookNode]:
Expand Down Expand Up @@ -38,9 +38,7 @@ def coalesce_streams(outputs: list[NotebookNode]) -> list[NotebookNode]:

# process \r and \b characters
for output in streams.values():
old = output["text"]
while len(output["text"]) < len(old):
old = output["text"]
while _RGX_BACKSPACE.search(output["text"]):
# Cancel out anything-but-newline followed by backspace
output["text"] = _RGX_BACKSPACE.sub("", output["text"])
# Replace all carriage returns not followed by newline
Expand Down
13 changes: 13 additions & 0 deletions tests/test_render_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import pytest

from myst_nb.core.render import EntryPointError, load_renderer
from myst_nb.core.utils import coalesce_streams
from nbformat import from_dict
from sphinx.util.fileutil import copy_asset_file


Expand Down Expand Up @@ -116,6 +118,17 @@ def test_merge_streams_parallel(sphinx_run, file_regression):
file_regression.check(doctree.pformat(), extension=".xml", encoding="utf-8")


def test_merge_streams_backspace():
"""Test that merging streams cancels out characters erased by backspaces."""
outputs = [
from_dict(
{"output_type": "stream", "name": "stdout", "text": "50%\b\b\b100%\n"}
),
from_dict({"output_type": "stream", "name": "stderr", "text": "a\bb word\n"}),
]
assert [o["text"] for o in coalesce_streams(outputs)] == ["100%\n", "b word\n"]


@pytest.mark.sphinx_params(
"metadata_image.ipynb",
conf={"nb_execution_mode": "off", "nb_cell_metadata_key": "myst"},
Expand Down