def doctest(doc: str) -> str:
    """Wrap doctest as markdown Python code."""
    keep = False
    docs = []
    lines = doc.splitlines()
    for i, line in enumerate(lines):
        signed = line.startswith(">>> ")
        if signed:
            if not keep:
                docs.append("```python")
                keep = True
        elif keep:
            docs.append("```")
            keep = False
        docs.append(line)
        if signed and i == len(lines) - 1:
            docs.append("```")
            keep = False
    return '\n'.join(docs)