def render(self, filename_root=None, file=None):
        """Render the document repeatedly until the output no longer changes due
        to cross-references that need some iterations to converge."""
        self.error = False
        filename_root = Path(filename_root) if filename_root else None
        if filename_root and file is None:
            extension = self.backend_document.extension
            filename = filename_root.with_suffix(extension)
            file = filename.open('wb')
        elif file and filename_root is None:
            filename = getattr(file, 'name', None)
        else:
            raise ValueError("You need to specify either 'filename_root' or "
                             "'file'.")

        def has_converged(part_page_counts):
            """Return `True` if the last rendering iteration converged to a
            stable result.

            Practically, this tests whether the total number of pages and page
            references to document elements have changed since the previous
            rendering iteration."""
            nonlocal prev_number_of_pages, prev_page_references
            return (part_page_counts == prev_number_of_pages and
                    self.page_references == prev_page_references)

        fake_container = FakeContainer(self)
        try:
            self.document_tree.build_document(fake_container)
            (prev_number_of_pages,
             prev_page_references) = self._load_cache(filename_root)

            self.part_page_counts = prev_number_of_pages
            self.prepare(fake_container)
            self.page_elements.clear()
            self.page_references = prev_page_references.copy()
            self.part_page_counts = self._render_pages()
            while not has_converged(self.part_page_counts):
                prev_number_of_pages = self.part_page_counts
                prev_page_references = self.page_references.copy()
                print('Not yet converged, rendering again...')
                del self.backend_document
                self.backend_document = self.backend.Document(self.CREATOR)
                self.part_page_counts = self._render_pages()
            self.create_outlines()
            if filename:
                self._save_cache(filename_root, self.part_page_counts,
                                 self.page_references)
                self.style_log.write_log(filename_root)
                print('Writing output: {}'.format(filename))
            self.backend_document.write(file)
        finally:
            if filename_root:
                file.close()
        return not self.error
def render ( self , filename_root = None , file = None ) : self . error = False filename_root = Path ( filename_root ) if filename_root else None if filename_root and file is None : extension = self . backend_document . extension filename = filename_root . with_suffix ( extension ) file = filename . open ( 'wb' ) elif file and filename_root is None : filename = getattr ( file , 'name' , None ) else : raise ValueError ( "You need to specify either 'filename_root' or " "'file'." ) def has_converged ( part_page_counts ) : nonlocal prev_number_of_pages , prev_page_references return ( part_page_counts == prev_number_of_pages and self . page_references == prev_page_references ) fake_container = FakeContainer ( self ) try : self . document_tree . build_document ( fake_container ) ( prev_number_of_pages , prev_page_references ) = self . _load_cache ( filename_root ) self . part_page_counts = prev_number_of_pages self . prepare ( fake_container ) self . page_elements . clear ( ) self . page_references = prev_page_references . copy ( ) self . part_page_counts = self . _render_pages ( ) while not has_converged ( self . part_page_counts ) : prev_number_of_pages = self . part_page_counts prev_page_references = self . page_references . copy ( ) print ( 'Not yet converged, rendering again...' ) del self . backend_document self . backend_document = self . backend . Document ( self . CREATOR ) self . part_page_counts = self . _render_pages ( ) self . create_outlines ( ) if filename : self . _save_cache ( filename_root , self . part_page_counts , self . page_references ) self . style_log . write_log ( filename_root ) print ( 'Writing output: {}' . format ( filename ) ) self . backend_document . write ( file ) finally : if filename_root : file . close ( ) return not self . error
Render the document repeatedly until the output no longer changes due
        to cross-references that need some iterations to converge.