from pathlib import Path
from typing import List


def get_snippets(source, indent=4):
    """Get all code snippets from a given documentation source file."""
    if not source.endswith(".rst"):  # pragma: no cover
        source += ".rst"
    source_path = Path(__file__).parents[1] / "docs" / source
    lines = open(source_path).readlines()
    snippets: List[str] = []
    snippet: List[str] = []
    snippet_start = " " * indent
    for line in lines:
        if not line.rstrip() and snippet:
            snippet.append(line)
        elif line.startswith(snippet_start):
            snippet.append(line[indent:])
        else:
            if snippet:
                snippets.append("".join(snippet).rstrip() + "\n")
                snippet = []
    if snippet:
        snippets.append("".join(snippet).rstrip() + "\n")
    return snippets