def test_render(line, title, input, expected): dct = yaml.safe_load(input) dct.setdefault("metadata", {}) ntbk = nbformat.from_dict(dct) md, env, tokens = nb_to_tokens(ntbk, MdParserConfig(), "default") document = make_document() with mock_sphinx_env(document=document): tokens_to_docutils(md, env, tokens, document) output = document.pformat().rstrip() if output != expected.rstrip(): print(output) assert output == expected.rstrip()
def test_myst_mock_sphinx_env(): conf = {"extensions": ["sphinxcontrib.bibtex"]} requested_extensions = set(conf.get("extensions")) with mock_sphinx_env( conf=conf, srcdir=".", with_builder=True, document=make_document() ) as app, pytest.xfail( reason="See https://github.com/executablebooks/MyST-Parser/issues/327" ): app_exts = sorted(app.extensions) assert ( requested_extensions.intersection(app_exts) == requested_extensions ), f"requested {requested_extensions}, got {app_exts}"
def test_parse(test_name, text, caplog, data_regression): with mock_sphinx_env(srcdir="root", with_builder=True) as app: # type: Sphinx app.add_post_transform(MystReferenceResolver) app.add_source_suffix(".md", "markdown") document = parse(app, text, docname="index") app.env.apply_post_transforms(document, "index") data_regression.check( { "doc": document.pformat(), "logs": caplog.record_tuples }, basename=test_name)
def test_reporting(line, title, input, expected): dct = yaml.safe_load(input) dct.setdefault("metadata", {}) ntbk = nbformat.from_dict(dct) md, env, tokens = nb_to_tokens(ntbk, MdParserConfig(), "default") document = make_document("source/path") messages = [] def observer(msg_node): if msg_node["level"] > 1: messages.append(msg_node.astext()) document.reporter.attach_observer(observer) with mock_sphinx_env(document=document): tokens_to_docutils(md, env, tokens, document) assert "\n".join(messages).rstrip() == expected.rstrip()
def to_docutils( text: str, options=None, env=None, disable_syntax: List[str] = (), math_delimiters: str = "dollars", renderer="sphinx", document=None, in_sphinx_env: bool = False, conf=None, srcdir=None, ): """Render text to the docutils AST :param text: the text to render :param options: options to update the parser with :param env: The sandbox environment for the parse (will contain e.g. reference definitions) :param disable_syntax: list of syntax element names to disable :param document: the docutils root node to use (otherwise a new one will be created) :param in_sphinx_env: initialise a minimal sphinx environment (useful for testing) :param conf: the sphinx conf.py as a dictionary :param srcdir: to parse to the mock sphinc env :returns: docutils document """ from myst_parser.docutils_renderer import make_document md = default_parser( renderer=renderer, disable_syntax=disable_syntax, math_delimiters=math_delimiters, ) if options: md.options.update(options) md.options["document"] = document or make_document() if in_sphinx_env: from myst_parser.sphinx_renderer import mock_sphinx_env with mock_sphinx_env(conf=conf, srcdir=srcdir, document=md.options["document"]): return md.render(text, env) else: return md.render(text, env)
def test_parse(test_name, text, should_warn, file_regression): with mock_sphinx_env( conf={"extensions": ["myst_parser"]}, srcdir="root", with_builder=True, raise_on_warning=True, ) as app: # type: Sphinx app.env.myst_config = MdParserConfig() document = parse(app, text, docname="index") if should_warn: with pytest.raises(SphinxWarning): app.env.apply_post_transforms(document, "index") else: app.env.apply_post_transforms(document, "index") content = document.pformat() # windows fix content = content.replace("root" + os.sep + "index.md", "root/index.md") file_regression.check(content, basename=test_name, extension=".xml")
def to_docutils( text: str, parser_config: Optional[MdParserConfig] = None, options=None, env=None, document=None, in_sphinx_env: bool = False, conf=None, srcdir=None, ): """Render text to the docutils AST :param text: the text to render :param options: options to update the parser with :param env: The sandbox environment for the parse (will contain e.g. reference definitions) :param document: the docutils root node to use (otherwise a new one will be created) :param in_sphinx_env: initialise a minimal sphinx environment (useful for testing) :param conf: the sphinx conf.py as a dictionary :param srcdir: to parse to the mock sphinc env :returns: docutils document """ from myst_parser.docutils_renderer import make_document md = default_parser(parser_config or MdParserConfig()) if options: md.options.update(options) md.options["document"] = document or make_document() if in_sphinx_env: from myst_parser.sphinx_renderer import mock_sphinx_env with mock_sphinx_env(conf=conf, srcdir=srcdir, document=md.options["document"]): return md.render(text, env) else: return md.render(text, env)
def test_minimal_sphinx(): with mock_sphinx_env(conf={"author": "bob geldof"}, with_builder=True) as app: assert app.config["author"] == "bob geldof"