def create_publisher(app: "Sphinx", filetype: str) -> Publisher: reader = SphinxStandaloneReader() reader.setup(app) parser = app.registry.create_source_parser(app, filetype) if parser.__class__.__name__ == 'CommonMarkParser' and parser.settings_spec == ( ): # a workaround for recommonmark # If recommonmark.AutoStrictify is enabled, the parser invokes reST parser # internally. But recommonmark-0.4.0 does not provide settings_spec for reST # parser. As a workaround, this copies settings_spec for RSTParser to the # CommonMarkParser. from docutils.parsers.rst import Parser as RSTParser parser.settings_spec = RSTParser.settings_spec pub = Publisher(reader=reader, parser=parser, writer=SphinxDummyWriter(), source_class=SphinxFileInput, destination=NullOutput()) # Propagate exceptions by default when used programmatically: defaults = {"traceback": True, **app.env.settings} # Set default settings if docutils.__version_info__[:2] >= (0, 19): pub.get_settings(**defaults) # type: ignore[arg-type] else: pub.settings = pub.setup_option_parser( **defaults).get_default_values() # type: ignore return pub
def test_syntax_extensions(file_params): """The description is parsed as a docutils commandline""" pub = Publisher(parser=Parser()) option_parser = pub.setup_option_parser() try: settings = option_parser.parse_args( shlex.split(file_params.description)).__dict__ except Exception as err: raise AssertionError( f"Failed to parse commandline: {file_params.description}\n{err}") report_stream = StringIO() settings["warning_stream"] = report_stream doctree = publish_doctree( file_params.content, parser=Parser(), settings_overrides=settings, ) file_params.assert_expected(doctree.pformat(), rstrip_lines=True)