Return the text header corresponding to a notebook, and remove the
    first cell of the notebook if it contained the header

def metadata_and_cell_to_header(notebook, metadata, text_format, ext):
    """
    Return the text header corresponding to a notebook, and remove the
    first cell of the notebook if it contained the header
    """

    header = []

    lines_to_next_cell = None
    if notebook.cells:
        cell = notebook.cells[0]
        if cell.cell_type == 'raw':
            lines = cell.source.strip('\n\t ').splitlines()
            if len(lines) >= 2 \
                    and _HEADER_RE.match(lines[0]) \
                    and _HEADER_RE.match(lines[-1]):
                header = lines[1:-1]
                lines_to_next_cell = cell.metadata.get('lines_to_next_cell')
                notebook.cells = notebook.cells[1:]

    metadata = insert_jupytext_info_and_filter_metadata(metadata, ext, text_format)

    if metadata:
        header.extend(yaml.safe_dump({'jupyter': metadata}, default_flow_style=False).splitlines())

    if header:
        header = ['---'] + header + ['---']

    return comment_lines(header, text_format.header_prefix), lines_to_next_cell