def writes(self, nb, metadata=None, **kwargs):
        """Return the text representation of the notebook"""
        if self.fmt.get('format_name') == 'pandoc':
            metadata = insert_jupytext_info_and_filter_metadata(metadata, self.ext, self.implementation)

            cells = []
            for cell in nb.cells:
                cell_metadata = filter_metadata(copy(cell.metadata),
                                                self.fmt.get('cell_metadata_filter'),
                                                _IGNORE_CELL_METADATA)
                if cell.cell_type == 'code':
                    cells.append(new_code_cell(source=cell.source, metadata=cell_metadata))
                else:
                    cells.append(NotebookNode(source=cell.source, metadata=cell_metadata, cell_type=cell.cell_type))

            return notebook_to_md(new_notebook(metadata=metadata, cells=cells))

        # Copy the notebook, in order to be sure we do not modify the original notebook
        nb = new_notebook(cells=nb.cells, metadata=deepcopy(metadata or nb.metadata))
        metadata = nb.metadata
        default_language = default_language_from_metadata_and_ext(metadata, self.implementation.extension)
        self.update_fmt_with_notebook_options(nb.metadata)

        if 'main_language' in metadata.get('jupytext', {}):
            del metadata['jupytext']['main_language']

        header = encoding_and_executable(nb, metadata, self.ext)
        header_content, header_lines_to_next_cell = metadata_and_cell_to_header(nb, metadata,
                                                                                self.implementation, self.ext)
        header.extend(header_content)

        cell_exporters = []
        looking_for_first_markdown_cell = (self.implementation.format_name and
                                           self.implementation.format_name.startswith('sphinx'))
        split_at_heading = self.fmt.get('split_at_heading', False)

        for cell in nb.cells:
            if looking_for_first_markdown_cell and cell.cell_type == 'markdown':
                cell.metadata.setdefault('cell_marker', '"""')
                looking_for_first_markdown_cell = False

            cell_exporters.append(self.implementation.cell_exporter_class(cell, default_language, self.fmt))

        texts = [cell.cell_to_text() for cell in cell_exporters]
        lines = []

        # concatenate cells in reverse order to determine how many blank lines (pep8)
        for i, cell in reversed(list(enumerate(cell_exporters))):
            text = cell.remove_eoc_marker(texts[i], lines)

            if i == 0 and self.implementation.format_name and \
                    self.implementation.format_name.startswith('sphinx') and \
                    (text in [['%matplotlib inline'], ['# %matplotlib inline']]):
                continue

            lines_to_next_cell = cell.lines_to_next_cell
            if lines_to_next_cell is None:
                lines_to_next_cell = pep8_lines_between_cells(text, lines, self.implementation.extension)

            text.extend([''] * lines_to_next_cell)

            # two blank lines between markdown cells in Rmd when those do not have explicit region markers
            if self.ext in ['.Rmd', '.md'] and not cell.is_code():
                if (i + 1 < len(cell_exporters) and not cell_exporters[i + 1].is_code() and
                        not texts[i][0].startswith('<!-- #region') and
                        not texts[i + 1][0].startswith('<!-- #region') and
                        (not split_at_heading or not (texts[i + 1] and texts[i + 1][0].startswith('#')))):
                    text.append('')

            # "" between two consecutive code cells in sphinx
            if self.implementation.format_name.startswith('sphinx') and cell.is_code():
                if i + 1 < len(cell_exporters) and cell_exporters[i + 1].is_code():
                    text.append('""')

            if i + 1 < len(cell_exporters):
                lines = cell_exporters[i + 1].simplify_soc_marker(lines, text)
            lines = text + lines

        if header_lines_to_next_cell is None:
            header_lines_to_next_cell = pep8_lines_between_cells(header_content, lines, self.implementation.extension)

        header.extend([''] * header_lines_to_next_cell)

        if cell_exporters:
            lines = cell_exporters[0].simplify_soc_marker(lines, header)

        return '\n'.join(header + lines)
def writes ( self , nb , metadata = None , ** kwargs ) : if self . fmt . get ( 'format_name' ) == 'pandoc' : metadata = insert_jupytext_info_and_filter_metadata ( metadata , self . ext , self . implementation ) cells = [ ] for cell in nb . cells : cell_metadata = filter_metadata ( copy ( cell . metadata ) , self . fmt . get ( 'cell_metadata_filter' ) , _IGNORE_CELL_METADATA ) if cell . cell_type == 'code' : cells . append ( new_code_cell ( source = cell . source , metadata = cell_metadata ) ) else : cells . append ( NotebookNode ( source = cell . source , metadata = cell_metadata , cell_type = cell . cell_type ) ) return notebook_to_md ( new_notebook ( metadata = metadata , cells = cells ) ) nb = new_notebook ( cells = nb . cells , metadata = deepcopy ( metadata or nb . metadata ) ) metadata = nb . metadata default_language = default_language_from_metadata_and_ext ( metadata , self . implementation . extension ) self . update_fmt_with_notebook_options ( nb . metadata ) if 'main_language' in metadata . get ( 'jupytext' , { } ) : del metadata [ 'jupytext' ] [ 'main_language' ] header = encoding_and_executable ( nb , metadata , self . ext ) header_content , header_lines_to_next_cell = metadata_and_cell_to_header ( nb , metadata , self . implementation , self . ext ) header . extend ( header_content ) cell_exporters = [ ] looking_for_first_markdown_cell = ( self . implementation . format_name and self . implementation . format_name . startswith ( 'sphinx' ) ) split_at_heading = self . fmt . get ( 'split_at_heading' , False ) for cell in nb . cells : if looking_for_first_markdown_cell and cell . cell_type == 'markdown' : cell . metadata . setdefault ( 'cell_marker' , '"""' ) looking_for_first_markdown_cell = False cell_exporters . append ( self . implementation . cell_exporter_class ( cell , default_language , self . fmt ) ) texts = [ cell . cell_to_text ( ) for cell in cell_exporters ] lines = [ ] for i , cell in reversed ( list ( enumerate ( cell_exporters ) ) ) : text = cell . remove_eoc_marker ( texts [ i ] , lines ) if i == 0 and self . implementation . format_name and self . implementation . format_name . startswith ( 'sphinx' ) and ( text in [ [ '%matplotlib inline' ] , [ '# %matplotlib inline' ] ] ) : continue lines_to_next_cell = cell . lines_to_next_cell if lines_to_next_cell is None : lines_to_next_cell = pep8_lines_between_cells ( text , lines , self . implementation . extension ) text . extend ( [ '' ] * lines_to_next_cell ) if self . ext in [ '.Rmd' , '.md' ] and not cell . is_code ( ) : if ( i + 1 < len ( cell_exporters ) and not cell_exporters [ i + 1 ] . is_code ( ) and not texts [ i ] [ 0 ] . startswith ( '<!-- #region' ) and not texts [ i + 1 ] [ 0 ] . startswith ( '<!-- #region' ) and ( not split_at_heading or not ( texts [ i + 1 ] and texts [ i + 1 ] [ 0 ] . startswith ( '#' ) ) ) ) : text . append ( '' ) if self . implementation . format_name . startswith ( 'sphinx' ) and cell . is_code ( ) : if i + 1 < len ( cell_exporters ) and cell_exporters [ i + 1 ] . is_code ( ) : text . append ( '""' ) if i + 1 < len ( cell_exporters ) : lines = cell_exporters [ i + 1 ] . simplify_soc_marker ( lines , text ) lines = text + lines if header_lines_to_next_cell is None : header_lines_to_next_cell = pep8_lines_between_cells ( header_content , lines , self . implementation . extension ) header . extend ( [ '' ] * header_lines_to_next_cell ) if cell_exporters : lines = cell_exporters [ 0 ] . simplify_soc_marker ( lines , header ) return '\n' . join ( header + lines )
Return the text representation of the notebook