Beispiel #1
0
 def convert_chunk(self, chunk):
     """Convert the chunk to HTML and return the HTML output."""
     doctree = self.create_subdocument(chunk)
     writer = self.writer_class()
     writer.set_external_ids(self.collect_external_ids(chunk))
     writer.write(doctree, io.NullOutput())
     writer.assemble_parts()
     return self.assemble_html_output(chunk, writer.parts)
class PublishDoctreeTestCase(DocutilsTestSupport.StandardTestCase,
                             docutils.SettingsSpec):

    settings_default_overrides = {
        '_disable_config': True,
        'warning_stream': io.NullOutput()
    }

    def test_publish_doctree(self):
        # Test `publish_doctree` and `publish_from_doctree`.

        # Produce the document tree.
        doctree = core.publish_doctree(source=test_document,
                                       reader_name='standalone',
                                       parser_name='restructuredtext',
                                       settings_spec=self,
                                       settings_overrides={
                                           'expose_internals':
                                           ['refnames', 'do_not_expose'],
                                           'report_level':
                                           5
                                       })
        self.assertTrue(isinstance(doctree, nodes.document))

        # Confirm that transforms have been applied (in this case, the
        # DocTitle transform):
        self.assertTrue(isinstance(doctree[0], nodes.title))
        self.assertTrue(isinstance(doctree[1], nodes.paragraph))
        # Confirm that the Messages transform has not yet been applied:
        self.assertEqual(len(doctree), 2)

        # The `do_not_expose` attribute may not show up in the
        # pseudoxml output because the expose_internals transform may
        # not be applied twice.
        doctree.do_not_expose = 'test'
        # Write out the document:
        output = core.publish_from_doctree(doctree,
                                           writer_name='pseudoxml',
                                           settings_spec=self,
                                           settings_overrides={
                                               'expose_internals':
                                               ['refnames', 'do_not_expose'],
                                               'report_level':
                                               1
                                           })
        self.assertEqual(output, exposed_pseudoxml_output)

        # Test publishing parts using document as the source.
        parts = core.publish_parts(reader_name='doctree',
                                   source_class=io.DocTreeInput,
                                   source=doctree,
                                   source_path='test',
                                   writer_name='html',
                                   settings_spec=self)
        self.assertTrue(isinstance(parts, dict))

    def test_publish_pickle(self):
        # Test publishing a document tree with pickling and unpickling.

        # Produce the document tree.
        doctree = core.publish_doctree(source=test_document,
                                       reader_name='standalone',
                                       parser_name='restructuredtext',
                                       settings_spec=self)
        self.assertTrue(isinstance(doctree, nodes.document))

        # Pickle the document.  Note: if this fails, some unpickleable
        # reference has been added somewhere within the document tree.
        # If so, you need to fix that.
        #
        # Note: Please do not remove this test, this is an important
        # requirement, applications will be built on the assumption
        # that we can pickle the document.

        # Remove the reporter and the transformer before pickling.
        doctree.reporter = None
        doctree.transformer = None

        doctree_pickled = pickle.dumps(doctree)
        self.assertTrue(isinstance(doctree_pickled, bytes))
        del doctree

        # Unpickle the document.
        doctree_zombie = pickle.loads(doctree_pickled)
        self.assertTrue(isinstance(doctree_zombie, nodes.document))

        # Write out the document:
        output = core.publish_from_doctree(doctree_zombie,
                                           writer_name='pseudoxml',
                                           settings_spec=self)
        self.assertEqual(output, pseudoxml_output)