# Path: uqbar/strings.py
# def ansi_escape(string):
#     return _ansi_escape_pattern.sub("", string)
# 
# def normalize(string: str) -> str:
#     """
#     Normalizes whitespace.
# 
#     Strips leading and trailing blank lines, dedents, and removes trailing
#     whitespace from the result.
#     """
#     string = string.replace("\t", "    ")
#     lines = string.split("\n")
#     while lines and (not lines[0] or lines[0].isspace()):
#         lines.pop(0)
#     while lines and (not lines[-1] or lines[-1].isspace()):
#         lines.pop()
#     for i, line in enumerate(lines):
#         lines[i] = line.rstrip()
#     string = "\n".join(lines)
#     string = textwrap.dedent(string)
#     return string


import pathlib
import shutil
import sys
import pytest
import sphinx.errors
from uqbar.strings import ansi_escape, normalize
    sys.path.remove(str(path))


@pytest.fixture
def rm_dirs(app):
    path = pathlib.Path(app.doctreedir)
    shutil.rmtree(path)
    yield


@pytest.mark.sphinx(
    "html", testroot="uqbar-sphinx-book", confoverrides={"uqbar_book_use_cache": True}
)
def test_sphinx_book_html_cached(app, status, warning, rm_dirs):
    app.build()
    assert not warning.getvalue().strip()
    image_path = pathlib.Path(app.outdir) / "_images"
    dot_paths = [path for path in image_path.iterdir() if path.suffix == ".dot"]
    dot_sources = set(normalize(path.read_text()) for path in dot_paths)
    assert dot_sources == set(
        [
            normalize(
                """
                digraph G {
                    node_0;
                    node_1;
                    node_0 -> node_1;
                }
                """
            ),

# Path: uqbar/strings.py
# def ansi_escape(string):
#     return _ansi_escape_pattern.sub("", string)
# 
# def normalize(string: str) -> str:
#     """
#     Normalizes whitespace.
# 
#     Strips leading and trailing blank lines, dedents, and removes trailing
#     whitespace from the result.
#     """
#     string = string.replace("\t", "    ")
#     lines = string.split("\n")
#     while lines and (not lines[0] or lines[0].isspace()):
#         lines.pop(0)
#     while lines and (not lines[-1] or lines[-1].isspace()):
#         lines.pop()
#     for i, line in enumerate(lines):
#         lines[i] = line.rstrip()
#     string = "\n".join(lines)
#     string = textwrap.dedent(string)
#     return string


# Path: tests/test_sphinx_book.py
import pathlib
import shutil
import sys
import pytest
import sphinx.errors
from uqbar.strings import ansi_escape, normalize

    sys.path.remove(str(path))


@pytest.fixture
def rm_dirs(app):
    path = pathlib.Path(app.doctreedir)
    shutil.rmtree(path)
    yield


@pytest.mark.sphinx(
    "html", testroot="uqbar-sphinx-book", confoverrides={"uqbar_book_use_cache": True}
)
def test_sphinx_book_html_cached(app, status, warning, rm_dirs):
    app.build()
    assert not warning.getvalue().strip()
    image_path = pathlib.Path(app.outdir) / "_images"
    dot_paths = [path for path in image_path.iterdir() if path.suffix == ".dot"]
    dot_sources = set(normalize(path.read_text()) for path in dot_paths)
    assert dot_sources == set(
        [
            normalize(
                """
                digraph G {
                    node_0;
                    node_1;
                    node_0 -> node_1;
                }
                """
            ),

            normalize(