def parse_doc_into_cells()

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


def parse_doc_into_cells(content):
    """
    Split a documentation content into cells.
    """
    cells = []
    doc_config = get_doc_config()
    if doc_config is not None and hasattr(doc_config, "notebook_first_cells"):
        for cell in doc_config.notebook_first_cells:
            if cell["type"] == "markdown":
                cells.append(markdown_cell(cell["content"].strip()))
            elif cell["type"] == "code":
                cells.append(code_cell(cell["content"].strip()))

    current_lines = []
    cell_type = "markdown"
    # We keep track of whether we are in a general code block (not necessarily in a code cell) as a line with a comment
    # would be detected as a header.
    in_code = False

    for line in content.split("\n"):
        # Look if we've got a special line.
        special_line = None
        if _re_header.search(line) is not None and not in_code:
            special_line = "header"
        elif _re_python_code.search(line) is not None:
            special_line = "begin_code"
        elif line.rstrip() == "```" and cell_type == "code":
            special_line = "end_code"
        elif line.startswith("```"):
            special_line = "other_code"
        elif _re_youtube.search(line) is not None:
            special_line = "youtube"

        # Some of those special lines mean we have to process the current cell.
        process_current_cell = False
        if cell_type == "markdown":
            process_current_cell = special_line in ["header", "begin_code", "youtube"]
        elif cell_type == "code":
            process_current_cell = special_line == "end_code"

        # Add the current cell to the list
        if process_current_cell:
            if cell_type == "markdown":
                content = "\n".join(current_lines).strip()
                if len(content) > 0:
                    cells.append(markdown_cell(content))
            elif cell_type == "code" and len(current_lines) > 0:
                for code, output in parse_input_output(current_lines):
                    cells.append(code_cell(code, output=output))
            current_lines = []

        if special_line == "header":
            # Header go on their separate Markdown cell, as it plays nicely with the collapsible headers extension.
            cells.append(markdown_cell(line))
        elif special_line == "begin_code":
            cell_type = "code"
            in_code = True
        elif special_line == "end_code":
            cell_type = "markdown"
            in_code = False
        elif special_line == "other_code":
            current_lines.append(line)
            in_code = not in_code
        elif special_line == "youtube":
            # YouTube cells are their own separate cell for proper showing
            youtube_id = _re_youtube.search(line).groups()[0]
            cells.append(youtube_cell(youtube_id))
        else:
            current_lines.append(line)

    # Now that we're done, we just have to process the remainder.
    if cell_type == "markdown":
        content = "\n".join(current_lines).strip()
        if len(content) > 0:
            cells.append(markdown_cell(content))

    return cells