def test_func_class_format(): with open("input.rst", "w") as fp: fp.write(INPUT) docutilsconfig = os.environ.get("DOCUTILSCONFIG") data = rst2json("input.rst", format=latex.Writer(), config_files=[]) assert data == OUTPUT_LATEX assert os.environ.get("DOCUTILSCONFIG") == docutilsconfig
def test_func_pathlib(): with open("input.rst", "w") as fp: fp.write(INPUT) docutilsconfig = os.environ.get("DOCUTILSCONFIG") data = rst2json(Path("input.rst"), format="html4", config_files=[]) assert data == OUTPUT assert os.environ.get("DOCUTILSCONFIG") == docutilsconfig
def main( out_dir, intro_name, section_fmt, rst_input, intro_template, section_template, format, config, ): if format.lower() not in ("html", "html4", "html5"): raise click.UsageError('--format must be "html", "html4", or "html5"') cfg_files = get_docutils_config_files() if config is not None: cfg_files.append(config) data = rst2json( rst_input, format=format, options={"split_section_level": 1}, config_files=cfg_files, ) contexts = prepare_contexts(data, intro_name, section_fmt) outdir = Path(out_dir) outdir.mkdir(parents=True, exist_ok=True) jenv = jinja2.Environment( trim_blocks=True, lstrip_blocks=True, loader=jinja2.FileSystemLoader(os.curdir, followlinks=True), ) intro_tmpl = jenv.get_template(intro_template) section_tmpl = jenv.get_template(section_template) for i, (filename, cntxt) in enumerate(contexts): tmpl = section_tmpl if i else intro_tmpl (outdir / filename).write_text(tmpl.render(cntxt))
def test_func_stringio(): expected = deepcopy(OUTPUT) expected["meta"]["source"] = "" expected["system_messages"][0]["source"] = "" docutilsconfig = os.environ.get("DOCUTILSCONFIG") data = rst2json(StringIO(INPUT), format="html4", config_files=[]) assert data == expected assert os.environ.get("DOCUTILSCONFIG") == docutilsconfig
def test_func_nonnull_envvar(monkeypatch): with open("input.rst", "w") as fp: fp.write(INPUT) with open("custom.conf", "w") as fp: print("[standalone reader]", file=fp) print("doctitle-xform = no", file=fp) monkeypatch.setenv("DOCUTILSCONFIG", "./custom.conf") data = rst2json("input.rst", config_files=[]) assert data == OUTPUT assert os.environ.get("DOCUTILSCONFIG") == "./custom.conf"
def test_func_standard_conf(): with open("input.rst", "w") as fp: fp.write(INPUT) with open("docutils.conf", "w") as fp: print("[standalone reader]", file=fp) print("doctitle-xform = no", file=fp) docutilsconfig = os.environ.get("DOCUTILSCONFIG") data = rst2json("input.rst", format="html4") assert data == OUTPUT_NO_DOC_TITLE assert os.environ.get("DOCUTILSCONFIG") == docutilsconfig
def test_func_options(): with open("input.rst", "w") as fp: fp.write(INPUT) docutilsconfig = os.environ.get("DOCUTILSCONFIG") data = rst2json( "input.rst", format="html4", options={"doctitle_xform": False}, config_files=[], ) assert data == OUTPUT_NO_DOC_TITLE assert os.environ.get("DOCUTILSCONFIG") == docutilsconfig
def test_func_custom_conf_ignore_standard_conf(): with open("input.rst", "w") as fp: fp.write(INPUT) with open("custom.conf", "w") as fp: print("[standalone reader]", file=fp) print("doctitle-xform = no", file=fp) with open("docutils.conf", "w") as fp: print("[html writers]", file=fp) print("initial_header_level = 2", file=fp) docutilsconfig = os.environ.get("DOCUTILSCONFIG") data = rst2json("input.rst", format="html4", config_files=["custom.conf"]) assert data == OUTPUT_NO_DOC_TITLE assert os.environ.get("DOCUTILSCONFIG") == docutilsconfig
def test_func_destination_path_adjacent(tmp_path): input_path = tmp_path / "input.rst" css_path = tmp_path / "foo.css" input_path.write_text(":math:`a^2 + b^2 = c^2`\n") docutilsconfig = os.environ.get("DOCUTILSCONFIG") data = rst2json( input_path, format="html4", config_files=[], options={ "embed_stylesheet": False, "math_output": f"html {css_path}" }, destination_path=tmp_path / "quux", ) assert 'href="foo.css"' in data["html"]["math_requires"] assert os.environ.get("DOCUTILSCONFIG") == docutilsconfig
def test_func_options_vs_config(): with open("input.rst", "w") as fp: fp.write(INPUT) with open("custom.conf", "w") as fp: print("[standalone reader]", file=fp) print("doctitle-xform = no", file=fp) print("[html writers]", file=fp) print("initial_header_level = 2", file=fp) docutilsconfig = os.environ.get("DOCUTILSCONFIG") data = rst2json( "input.rst", format="html4", options={"initial_header_level": 3}, config_files=["custom.conf"], ) assert "<h2>A Document</h2>" in data["content"]["body"] assert os.environ.get("DOCUTILSCONFIG") == docutilsconfig
def test_func_destination_path_unrelated(tmp_path): input_path = tmp_path / "input.rst" css_path = tmp_path / "foo.css" dest_parts = list((tmp_path / "quux").parts) dest_parts[1] += "-glarch" input_path.write_text(":math:`a^2 + b^2 = c^2`\n") docutilsconfig = os.environ.get("DOCUTILSCONFIG") data = rst2json( input_path, format="html4", config_files=[], options={ "embed_stylesheet": False, "math_output": f"html {css_path}" }, destination_path=Path(*dest_parts), ) assert f'href="{css_path}"' in data["html"]["math_requires"] assert os.environ.get("DOCUTILSCONFIG") == docutilsconfig