def parse_input_output()

in src/doc_builder/convert_to_notebook.py [0:0]


def parse_input_output(code_lines):
    """
    Parse a code sample written in doctest syntax to extract input code and expected output.
    """
    current_lines = []
    in_input = True
    cells = []

    for idx, line in enumerate(code_lines):
        if is_empty_line(line):
            current_lines.append(line)
        elif not in_input and line.startswith(">>> "):
            in_input = True
            cells[-1] = (cells[-1][0], "\n".join(current_lines).strip())
            current_lines = [line[4:]]
        elif in_input and line[:4] not in [">>> ", "... "]:
            in_input = False
            cells.append(("\n".join(current_lines).strip(), None))
            current_lines = [line]
        else:
            if line.startswith(">>> ") or line.startswith("... "):
                current_lines.append(line[4:])
            else:
                current_lines.append(line)

    if in_input:
        cells.append(("\n".join(current_lines).strip(), None))
    else:
        cells[-1] = (cells[-1][0], "\n".join(current_lines).strip())

    if len(cells) == 1 and len(cells[0][0]) == 0:
        return [(cells[0][1], None)]

    return cells