def test_error(siteroot: SiteRoot) -> None: siteroot.write_text( siteroot.contents / "file1.rst", """ test ----------- :jinja:`{{page.load('file2.rst').html}}` """, ) siteroot.write_text(siteroot.contents / "file2.rst", ":jinja:`{{@@}}`") site = siteroot.load({}, {}) ok, err, deps, results, errors = site.build() assert ok == 0 assert err == len(errors) == 2 siteroot.write_text(siteroot.contents / "file2.rst", "") site = siteroot.load({}, {}) ok, err, deps, results, errors = site.build() assert ok == 2 assert err == len(errors) == 0
def test_build(siteroot: SiteRoot, debug: bool) -> None: siteroot.write_text( siteroot.path / "hooks.py", """ from miyadaiku.extend import * @pre_build def pre_build1(ctx): ctx.content.body = b"pre_build" return ctx @post_build def post_build1(ctx, outputinfos): org = outputinfos[0].filename.read_bytes() outputinfos[0].filename.write_bytes(org+b"post_build1") """, ) siteroot.write_text(siteroot.contents / "root1.txt", "content_root1") extend.load_hook(siteroot.path) site = siteroot.load({}, {}, debug=debug) site.build() assert len(extend.hooks_pre_build) == 1 assert len(extend.hooks_post_build) == 1 assert b"pre_buildpost_build1" == (siteroot.outputs / "root1.txt").read_bytes()
def test_indexbuilder(siteroot: SiteRoot) -> None: for i in range(21): siteroot.write_text(siteroot.contents / f"rstdir/{i}.rst", f"rst_{i}") siteroot.write_text( siteroot.contents / "rstdir/index.yml", """ type: index indexpage_max_articles: 10 directory: rstdir """, ) site = siteroot.load({}, {}) builders = builder.create_builders( site, site.files.get_content((("rstdir", ), "index.yml"))) indexbuilders = cast(List[builder.IndexBuilder], builders) assert indexbuilders[0].cur_page == 1 assert indexbuilders[0].num_pages == 2 assert len(indexbuilders[0].items) == 10 assert indexbuilders[1].cur_page == 2 assert indexbuilders[1].num_pages == 2 assert len(indexbuilders[1].items) == 11
def test_jinja_templ_err(siteroot: SiteRoot) -> None: mp_log.init_logging() siteroot.write_text( siteroot.templates / "page_article.html", """1 2 {{ 1/0}} 3 4 5 """, ) siteroot.write_text(siteroot.contents / "test.html", "test") site = siteroot.load({}, {}) jinjaenv = site.build_jinjaenv() ctx = context.JinjaOutput(site, jinjaenv, ((), "test.html")) try: context.eval_jinja_template(ctx, ctx.content, "page_article.html", {}) except exceptions.JinjaEvalError as e: assert e.errors[0][1] == 3 assert ">>> {{ 1/0}}" in e.errors[0][2] else: assert False
def test_hook(siteroot: SiteRoot) -> None: siteroot.write_text( siteroot.path / "hooks.py", """ from miyadaiku.extend import * @finished def fin(site): fin.called = 1 @finished def fin2(site): fin2.called = 1 @initialized def initialized(site): initialized.called = 1 """, ) extend.load_hook(siteroot.path) site = siteroot.load({}, {}) extend.run_finished(site) assert len(extend.hooks_finished) == 2 assert [1, 1] == [f.called for f in extend.hooks_finished] # type: ignore assert len(extend.hooks_initialized) == 1 assert extend.hooks_initialized[0].called == 1 # type: ignore
def test_index_group_filename(siteroot: SiteRoot) -> None: siteroot.write_text( siteroot.contents / "doc1.html", """ tags: tag1, tag2 {{ page.link_to("index.yml", group_value="tag1") }} """, ) for i in range(21): siteroot.write_text( siteroot.contents / f"htmldir/{i}.html", """ tags: tag1 """, ) siteroot.write_text( siteroot.contents / "index.yml", """ type: index groupby: tags indexpage_max_articles: 2 """, ) site = siteroot.load({}, {}) site.build() assert ('<a href="index_tags_tag1.html">index</a>' in (siteroot.outputs / "doc1.html").read_text()) assert len(list(siteroot.outputs.glob("index_tags_tag1*"))) == 11 assert len(list(siteroot.outputs.glob("index_tags_tag2*"))) == 1
def test_og(siteroot: SiteRoot) -> None: siteroot.write_text( siteroot.templates / "page_article.html", """ {{ macros.opengraph(page) }} """, ) siteroot.write_text(siteroot.contents / "img/img.png", "") siteroot.write_text( siteroot.contents / "index.rst", """ .. article:: :og_image: /img/img.png test article ------------------ body """, ) site = siteroot.load({}, {}) site.build() ret = (siteroot.outputs / "index.html").read_text() assert 'content="http://localhost:8888/img/img.png"' in ret
def test_theme_cssfile(siteroot: SiteRoot) -> None: site = siteroot.load({}, {}) ipynb.init(site) siteroot.write_text( siteroot.contents / "test.html", """ {{ipynb.load_css(page)}} """, ) site = siteroot.load({"themes": ["miyadaiku.themes.ipynb"]}, {}) site.build() s = (siteroot.outputs / "test.html").read_text() assert '<link href="static/ipynb/ipynb.css" rel="stylesheet"/>' in s assert (siteroot.outputs / "static/ipynb/ipynb.css").exists()
def test_index(siteroot: SiteRoot) -> None: for i in range(21): f"tag{i % 2 + 1}" siteroot.write_text( siteroot.contents / f"htmldir/{i}.html", f""" html{i} """, ) siteroot.write_text( siteroot.contents / "htmldir/index.yml", """ type: index """, ) site = siteroot.load({}, {}) builders = builder.create_builders( site, site.files.get_content((("htmldir", ), "index.yml"))) indexbuilders = cast(List[builder.IndexBuilder], builders) jinjaenv = site.build_jinjaenv() for b in indexbuilders: ctx = b.build_context(site, jinjaenv) (f, ) = ctx.build() assert len(indexbuilders) == 4 assert sum(len(b.items) for b in indexbuilders) == 21
def test_site(siteroot: SiteRoot) -> None: siteroot.write_text( siteroot.contents / "root1.yml", """ root_prop: root_prop_value generate_metadata_file: true """, ) siteroot.write_text(siteroot.contents / "root1.txt", "content_root1") siteroot.write_text(siteroot.contents / "root2.rst", "content_root2") siteroot.write_text(siteroot.files / "root1.txt", "file_root1") site = siteroot.load( { "themes": ["package3"], "root_prop": "root_prop_value" }, {"prop": "prop_value"}) assert (siteroot.contents / "root1.txt.props.yml").exists() assert (siteroot.contents / "root2.rst.props.yml").exists() assert len(site.files._contentfiles) == 6 assert ((), "root1.txt") in site.files._contentfiles assert ((), "package_root.rst") in site.files._contentfiles assert ((), "package3_root.rst") in site.files._contentfiles assert ((), "package3_files_1.rst") in site.files._contentfiles assert ((), "package4_root.rst") in site.files._contentfiles assert site.config.get((), "prop") == "prop_value" assert site.config.get((), "root_prop") == "root_prop_value" assert site.config.get((), "package3_prop") == "package3_prop_value" assert site.config.get((), "package3_prop_a1") == "value_package3_a1" assert site.config.get((), "package4_prop") == "package4_prop_value"
def test_refs(siteroot: SiteRoot) -> None: siteroot.write_text( siteroot.contents / "file1.rst", """ :jinja:`{{ page.link_to("./file2.rst") }}` """, ) siteroot.write_text( siteroot.contents / "file2.rst", """ """, ) site = siteroot.load({}, {}) ok, err, deps, results, errors = site.build() orig_outputinfos = depends.update_outputinfos(site, [], results) depends.save_deps(site, deps, orig_outputinfos, errors) # test update depends (siteroot.contents / "file2.rst").write_text("") site.load(site.root, {}) rebuild, updated, depdict, outputfinos = depends.check_depends(site) assert rebuild is False assert updated == set(( ((), "file1.rst"), ((), "file2.rst"), ))
def test_update_file(siteroot: SiteRoot) -> None: siteroot.write_text( siteroot.contents / "file1.rst", """ :jinja:`{{ page.link_to("./file2.rst") }}` """, ) siteroot.write_text( siteroot.contents / "file2.rst", """ """, ) site = siteroot.load({}, {}) ok, err, deps, results, errors = site.build() orig_outputinfos = depends.update_outputinfos(site, [], results) depends.save_deps(site, deps, orig_outputinfos, errors) output = siteroot.outputs / "file1.html" output.unlink() rebuild, updated, depdict, outputinfos = depends.check_depends(site) assert rebuild is False assert updated == set((((), "file1.rst"), )) ok, err, deps, results, errors = site.build() assert deps[((), "file1.rst")][2] == {"file1.html"} assert deps[((), "file2.rst")][2] == {"file2.html"}
def test_get_contents_none(siteroot: SiteRoot) -> None: siteroot.write_text( siteroot.contents / "a.rst", """ .. article:: :date: 2017-01-01 :category: A :tags: tag1, tag2 :prop1: propvalue1 test """, ) siteroot.write_text( siteroot.contents / "b.rst", """ .. article:: :date: 2017-01-01 test """, ) s = siteroot.load({}, {}) found = s.files.get_contents(s, filters={"tags": None}) assert set(f.src.contentpath for f in found) == set([((), "b.rst")]) found = s.files.get_contents(s, filters={"prop1": None}) assert set(f.src.contentpath for f in found) == set([((), "b.rst")])
def test_load(siteroot: SiteRoot) -> None: siteroot.write_text( siteroot.path / "hooks.py", """ from miyadaiku.extend import * @pre_load def pre_load1(site, contentsrc, binary): contentsrc.metadata['title'] = 'hook_pre' return contentsrc @post_load def post_load1(site, contentsrc, binary, bytes): contentsrc.metadata['title'] = contentsrc.metadata['title'] + 'hook_post' return contentsrc, bytes @load_finished def load_finished1(site): load_finished1.called = 1 """, ) siteroot.write_text(siteroot.contents / "root1.txt", "content_root1") extend.load_hook(siteroot.path) site = siteroot.load({}, {}) root1 = site.files.get_content(((), "root1.txt")) assert root1.src.metadata["title"] == "hook_prehook_post" assert 1 == extend.hooks_load_finished[0].called # type: ignore
def test_theme(siteroot: SiteRoot) -> None: site = siteroot.load({}, {}) ipynb.init(site) site = siteroot.load({"themes": ["miyadaiku.themes.ipynb"]}, {}) jinjaenv = site.build_jinjaenv() template = jinjaenv.from_string("{{ipynb.set_header()}}") ret = template.render() soup = BeautifulSoup(ret, "html.parser") assert soup.head is None assert soup.body is None assert soup.title is None assert soup.meta is None assert len(soup.find_all("style")) > 1 assert len(soup.find_all("script")) > 1
def test_index_filter(siteroot: SiteRoot) -> None: for i in range(21): tag = f"tag{i + 1}" siteroot.write_text( siteroot.contents / f"htmldir/{i}.html", f""" tags: {tag} html{i} - tag: {tag} """, ) siteroot.write_text( siteroot.contents / "htmldir/index.yml", """ type: index filters: tags: ['tag1', 'tag20'] """, ) site = siteroot.load({}, {}) (bldr, ) = builder.create_builders( site, site.files.get_content((("htmldir", ), "index.yml"))) item1, item2 = sorted(cast(builder.IndexBuilder, bldr).items) assert item1[1] == "0.html" assert item2[1] == "19.html" siteroot.write_text( siteroot.contents / "htmldir/index.yml", """ type: index filters: tags: ['tag1', 'tag20'] excludes: tags: ['tag1'] """, ) site = siteroot.load({}, {}) (bldr, ) = builder.create_builders( site, site.files.get_content((("htmldir", ), "index.yml"))) (item1, ) = cast(builder.IndexBuilder, bldr).items assert item1[1] == "19.html"
def test_binarycontext(siteroot: SiteRoot) -> None: siteroot.write_text(siteroot.files / "subdir" / "file1.txt", "subdir/file1") site = siteroot.load({}, {}) jinjaenv = site.build_jinjaenv() ctx = context.BinaryOutput(site, jinjaenv, (("subdir", ), "file1.txt")) (outputinfo, ) = ctx.build() assert Path(outputinfo.filename) == site.outputdir / "subdir/file1.txt" assert Path(outputinfo.filename).read_text() == "subdir/file1"
def test_indexbuilder_group(siteroot: SiteRoot) -> None: for i in range(21): tag = f"tag{i % 2 + 1}" siteroot.write_text( siteroot.contents / f"htmldir/{i}.html", f""" tags: {tag} html{i} - tag: {tag} """, ) siteroot.write_text( siteroot.contents / "htmldir/index.yml", """ type: index groupby: tags """, ) site = siteroot.load({}, {}) builders = builder.create_builders( site, site.files.get_content((("htmldir", ), "index.yml"))) indexbuilders = cast(List[builder.IndexBuilder], builders) assert len(indexbuilders) == 4 assert indexbuilders[0].cur_page == 1 assert indexbuilders[0].num_pages == 2 assert indexbuilders[0].value == "tag1" assert len(indexbuilders[0].items) == 5 assert indexbuilders[1].cur_page == 2 assert indexbuilders[1].num_pages == 2 assert indexbuilders[1].value == "tag1" assert len(indexbuilders[1].items) == 6 assert indexbuilders[2].cur_page == 1 assert indexbuilders[2].num_pages == 2 assert indexbuilders[2].value == "tag2" assert len(indexbuilders[2].items) == 5 assert indexbuilders[3].cur_page == 2 assert indexbuilders[3].num_pages == 2 assert indexbuilders[3].value == "tag2" assert len(indexbuilders[3].items) == 5 jinjaenv = site.build_jinjaenv() context = indexbuilders[0].build_context(site, jinjaenv) assert context.contentpath == (("htmldir", ), "index.yml")
def test_yaml(siteroot: SiteRoot) -> None: siteroot.write_text(siteroot.contents / "file1.rst", "") site = siteroot.load({}, {}) ok, err, deps, results, errors = site.build() orig_outputinfos = depends.update_outputinfos(site, [], results) depends.save_deps(site, deps, orig_outputinfos, errors) siteroot.write_text(siteroot.contents / "file2.yml", "") rebuild, updated, depdict, outputresults = depends.check_depends(site) assert rebuild is True
def test_load(siteroot: SiteRoot) -> None: siteroot.write_text(siteroot.contents / "A/B/C/file1.html", "A/B/C/file1.html") siteroot.write_text(siteroot.contents / "A/B/D/file2.html", "A/B/D/file1.html") site = siteroot.load({}, {}) jinjaenv = site.build_jinjaenv() ctx = context.JinjaOutput(site, jinjaenv, (("A", "B", "C"), "file1.html")) proxy = context.ContentProxy(ctx, ctx.content) file2 = proxy.load("../D/file2.html") assert file2.contentpath == (("A", "B", "D"), "file2.html")
def test_macro(siteroot: SiteRoot) -> None: siteroot.write_text(siteroot.contents / "file1.rst", "") site = siteroot.load({}, {}) ok, err, deps, results, errors = site.build() orig_outputinfos = depends.update_outputinfos(site, [], results) depends.save_deps(site, deps, orig_outputinfos, errors) # test macro (siteroot.modules / "file1.rst").write_text(""" file1-new-title ---------------------------- """)
def test_get_contents_exclude(siteroot: SiteRoot) -> None: siteroot.write_text( siteroot.contents / "a.rst", """ .. article:: :date: 2017-01-01 :category: A :tags: tag1, tag2 :prop1: propvalue1 test """, ) siteroot.write_text( siteroot.contents / "b.rst", """ .. article:: :date: 2017-01-02 :category: B :prop1: propvalue2 test """, ) siteroot.write_text( siteroot.contents / "c.rst", """ .. article:: :date: 2017-01-03 :tags: tag2, tag3 test """, ) s = site.Site() s = siteroot.load({}, {}) found = s.files.get_contents(s, excludes=dict(prop1="propvalue1")) assert set(f.src.contentpath for f in found) == set([((), "b.rst"), ((), "c.rst")]) found = s.files.get_contents(s, excludes=dict(category=[None])) assert set(f.src.contentpath for f in found) == set([((), "a.rst"), ((), "b.rst")]) found = s.files.get_contents(s, excludes=dict(tags=None)) assert set(f.src.contentpath for f in found) == set([((), "a.rst"), ((), "c.rst")])
def test_htmlcontext(siteroot: SiteRoot) -> None: siteroot.write_text(siteroot.contents / "doc.html", "hello<a>{{1+1}}</a>") siteroot.write_text(siteroot.templates / "page_article.html", "<div>{{ page.html }}</div>") site = siteroot.load({}, {}) jinjaenv = site.build_jinjaenv() ctx = context.JinjaOutput(site, jinjaenv, ((), "doc.html")) (outputinfo, ) = ctx.build() html = Path(outputinfo.filename).read_text() assert html == "<div>hello<a>2</a></div>" assert ctx.get_url() == "http://localhost:8888/doc.html"
def test_package(siteroot: SiteRoot) -> None: site = siteroot.load({}, {}) ipynb.init(site) contentsrc = ContentSrc( package="pkg_ipynb", srcpath="files/test.ipynb", metadata={}, contentpath=((), "test.html"), mtime=0, ) ((src, text), ) = ipynb.load(contentsrc) assert src.metadata["type"] == "article" print(text)
def test_split(siteroot: SiteRoot) -> None: site = siteroot.load({}, {}) ipynb.init(site) contentsrc = ContentSrc( package=None, srcpath=str(DIR / "test_splitsrc.ipynb"), metadata={}, contentpath=((), "test_splitsrc.ipynb"), mtime=0, ) ( (src1, text1), (src2, text2), ) = ipynb.load(contentsrc) assert src1.contentpath == ((), "file1") soup = BeautifulSoup(text1, "html.parser") print(soup.text) assert "%%%" not in soup.text assert "1+1" in soup.text assert "2+2" in soup.text assert "meta" not in soup.text assert "test1" in soup.text assert src1.metadata == { "type": "article", "meta": "value1", "has_jinja": True, "loader": "ipynb", } assert src2.contentpath == ((), "file2") soup = BeautifulSoup(text2, "html.parser") print(soup.text) assert "%%%" not in soup.text assert "3+3" in soup.text assert "meta" not in soup.text assert src2.metadata == { "type": "article", "meta": "value2", "has_jinja": True, "loader": "ipynb", }
def test_mpbuild(log: Any, siteroot: SiteRoot) -> None: mp_log.init_logging() siteroot.write_text( siteroot.templates / "page_article.html", """ {{ page.html }} """, ) siteroot.write_text(siteroot.contents / "test.html", "test") site = siteroot.load({}, {}, debug=False) site.build() args, kwargs = log.call_args_list[0] assert str(siteroot.contents / "test.html") in kwargs["extra"]["msgdict"]["msg"]
def test_load(siteroot: SiteRoot) -> None: site = siteroot.load({}, {}) ipynb.init(site) contentsrc = ContentSrc( package=None, srcpath=str(DIR / "test.ipynb"), metadata={}, contentpath=((), "test.html"), mtime=0, ) ((src, text), ) = ipynb.load(contentsrc) assert src.metadata["type"] == "article" assert "{{ 1+1 }}" in text assert "<p>{{ 1+1 }}</p>" not in text assert "hidden cell" not in text
def test_image(siteroot: SiteRoot) -> None: siteroot.write_text(siteroot.contents / "img/img.png", "") siteroot.write_text( siteroot.contents / "index.rst", """ .. jinja:: {{ macros.image(page.load('/img/img.png')) }} """, ) site = siteroot.load({}, {}) site.build() ret = (siteroot.outputs / "index.html").read_text() assert "img/img.png" in ret
def test_theme_pygments(siteroot: SiteRoot) -> None: siteroot.write_text( siteroot.contents / "index.rst", """ title ---------------- .. jinja:: {{ pygments.load_css(page) }} """, ) site = siteroot.load({"themes": ["miyadaiku.themes.pygments"]}, {}) site.build() s = (siteroot.outputs / "index.html").read_text() assert '<link href="static/pygments/native.css" rel="stylesheet"/>' in s assert (siteroot.outputs / "static/pygments/native.css").exists()
def test_loadfiles(siteroot: SiteRoot) -> None: siteroot.write_text(siteroot.contents / "root1.yml", "root_prop: root_prop_value") siteroot.write_text(siteroot.contents / "root1.txt", "content_root1") siteroot.write_text(siteroot.contents / "root_content1.txt", "root_content1") siteroot.write_text(siteroot.files / "root1.txt", "file_root1") siteroot.write_text(siteroot.files / "root_file1.txt", "root_file1") siteroot.write_text(siteroot.files / "root_file2.rst", "root_file2") files = loader.ContentFiles() cfg = config.Config({}) root = siteroot.path ignores: Set[str] = set() themes = ["package3", "package4"] site = siteroot.load({}, {}) loader.loadfiles(site, files, cfg, root, ignores, themes) assert len(files._contentfiles) == 8 assert files._contentfiles[((), "root1.txt")].get_body() == b"content_root1" assert files._contentfiles[( (), "root_content1.txt")].get_body() == b"root_content1" assert files._contentfiles[((), "root_file1.txt")].get_body() == b"root_file1" assert isinstance(files._contentfiles[((), "root_file2.rst")], contents.BinContent) assert (files._contentfiles[((), "package3_root.rst")].get_body().strip() == b"<p>package3/contents/package3_root.rst</p>") assert (files._contentfiles[((), "package_root.rst")].get_body().strip() == b"<p>package3/contents/package_root.rst</p>") package3_files_1 = files._contentfiles[((), "package3_files_1.rst")] assert isinstance(package3_files_1, contents.BinContent) assert package3_files_1.get_body().strip( ) == b"package3/files/package3_file_1.rst" assert (files._contentfiles[((), "package4_root.rst")].get_body().strip() == b"<p>package4/contents/package4_root.rst</p>") assert cfg.get((), "root_prop") == "root_prop_value" assert cfg.get((), "package3_prop_a1") == "value_package3_a1"