From 89c8168337393682f9caf9fa3b06f8ab6c73eb1a Mon Sep 17 00:00:00 2001 From: "Danke, Joel" Date: Mon, 24 Jan 2022 16:56:28 -0500 Subject: [PATCH 1/3] add tag to cli for only content --- jupyter_book/cli/main.py | 12 +++++++++++ tests/pages/single_page.ipynb | 27 ++++++++++++++++++++++++ tests/test_build.py | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/jupyter_book/cli/main.py b/jupyter_book/cli/main.py index dca33459e..847d11773 100644 --- a/jupyter_book/cli/main.py +++ b/jupyter_book/cli/main.py @@ -124,6 +124,12 @@ def main(): "This can only be used with --builder=custom. Valid options listed at " "https://www.sphinx-doc.org/en/master/man/sphinx-build.html", ) +@click.option( + "--tag", + multiple=True, + default=None, + help="Identify tag for including conditional content using the only directive.", +) @click.option( "-v", "--verbose", count=True, help="increase verbosity (can be repeated)" ) @@ -150,6 +156,7 @@ def build( freshenv, builder, custom_builder, + tag, verbose, quiet, individualpages, @@ -302,6 +309,10 @@ def build( click.style("Output Path: ", bold=True, fg="blue") + click.format_filename(f"{OUTPUT_PATH}") ) + click.echo( + click.style("Including tag: ", bold=True, fg="blue") + + click.style(f"{tag}") + ) # Now call the Sphinx commands to build result = build_sphinx( @@ -312,6 +323,7 @@ def build( path_config=path_config, confoverrides=config_overrides, builder=BUILDER_OPTS[builder], + tags=tag, warningiserror=warningiserror, keep_going=keep_going, freshenv=freshenv, diff --git a/tests/pages/single_page.ipynb b/tests/pages/single_page.ipynb index 9a4b3e63e..9511a1036 100644 --- a/tests/pages/single_page.ipynb +++ b/tests/pages/single_page.ipynb @@ -44,6 +44,33 @@ "````" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Conditional content\n", + "## Single tag\n", + "\n", + "The only directive can use a single tag:\n", + "\n", + "```{only} cowboy\n", + "This note is not for city slickers!\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Boolean tag\n", + "\n", + "And you can use multiple tags:\n", + "\n", + "```{only} cowboy and cowgirl\n", + "This note is for cowboys and cowgirls!\n", + "```" + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/tests/test_build.py b/tests/test_build.py index 7f03f8e4d..ef2f91386 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -309,3 +309,42 @@ def test_toc_numbered( basename=toc_file.split(".")[0], extension=f"{SPHINX_VERSION}.html", ) + +def test_only(pages, cli): + """Test {only} content is not built.""" + page = pages.joinpath("single_page.ipynb") + html = pages.joinpath("_build", "_page", "single_page", "html") + index = html.joinpath("index.html") + result = cli.invoke(commands.build, [page.as_posix(), "-n", "-W", "--keep-going"]) + assert result.exit_code == 0, result.output + assert html.joinpath("single_page.html").exists() + outpage = html.joinpath("single_page.html") + assert not html.joinpath("extra_page.html").exists() + assert 'slickers' not in outpage.read_text(encoding="utf8") + assert 'cowgirls' not in outpage.read_text(encoding="utf8") + +def test_only_tag(pages, cli): + """Test tag cli builds {only} content.""" + page = pages.joinpath("single_page.ipynb") + html = pages.joinpath("_build", "_page", "single_page", "html") + index = html.joinpath("index.html") + result = cli.invoke(commands.build, [page.as_posix(), "-n", "-W", "--keep-going", "--tag", "cowboy"]) + assert result.exit_code == 0, result.output + assert html.joinpath("single_page.html").exists() + outpage = html.joinpath("single_page.html") + assert not html.joinpath("extra_page.html").exists() + assert 'slickers' in outpage.read_text(encoding="utf8") + assert 'cowgirls' not in outpage.read_text(encoding="utf8") + +def test_both_tags(pages, cli): + """Test multiple tag creates boolean condition.""" + page = pages.joinpath("single_page.ipynb") + html = pages.joinpath("_build", "_page", "single_page", "html") + index = html.joinpath("index.html") + result = cli.invoke(commands.build, [page.as_posix(), "-n", "-W", "--keep-going", "--tag", "cowboy", "--tag", "cowgirl"]) + assert result.exit_code == 0, result.output + assert html.joinpath("single_page.html").exists() + outpage = html.joinpath("single_page.html") + assert not html.joinpath("extra_page.html").exists() + assert 'slickers' in outpage.read_text(encoding="utf8") + assert 'cowgirls' in outpage.read_text(encoding="utf8") From df7204250d2e67febc8f23d1c535a047ea8f04cb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Jan 2022 21:03:27 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- jupyter_book/cli/main.py | 3 +-- tests/test_build.py | 33 +++++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/jupyter_book/cli/main.py b/jupyter_book/cli/main.py index 847d11773..24a226808 100644 --- a/jupyter_book/cli/main.py +++ b/jupyter_book/cli/main.py @@ -310,8 +310,7 @@ def build( + click.format_filename(f"{OUTPUT_PATH}") ) click.echo( - click.style("Including tag: ", bold=True, fg="blue") - + click.style(f"{tag}") + click.style("Including tag: ", bold=True, fg="blue") + click.style(f"{tag}") ) # Now call the Sphinx commands to build diff --git a/tests/test_build.py b/tests/test_build.py index ef2f91386..c44b38a24 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -310,6 +310,7 @@ def test_toc_numbered( extension=f"{SPHINX_VERSION}.html", ) + def test_only(pages, cli): """Test {only} content is not built.""" page = pages.joinpath("single_page.ipynb") @@ -320,31 +321,47 @@ def test_only(pages, cli): assert html.joinpath("single_page.html").exists() outpage = html.joinpath("single_page.html") assert not html.joinpath("extra_page.html").exists() - assert 'slickers' not in outpage.read_text(encoding="utf8") - assert 'cowgirls' not in outpage.read_text(encoding="utf8") + assert "slickers" not in outpage.read_text(encoding="utf8") + assert "cowgirls" not in outpage.read_text(encoding="utf8") + def test_only_tag(pages, cli): """Test tag cli builds {only} content.""" page = pages.joinpath("single_page.ipynb") html = pages.joinpath("_build", "_page", "single_page", "html") index = html.joinpath("index.html") - result = cli.invoke(commands.build, [page.as_posix(), "-n", "-W", "--keep-going", "--tag", "cowboy"]) + result = cli.invoke( + commands.build, [page.as_posix(), "-n", "-W", "--keep-going", "--tag", "cowboy"] + ) assert result.exit_code == 0, result.output assert html.joinpath("single_page.html").exists() outpage = html.joinpath("single_page.html") assert not html.joinpath("extra_page.html").exists() - assert 'slickers' in outpage.read_text(encoding="utf8") - assert 'cowgirls' not in outpage.read_text(encoding="utf8") + assert "slickers" in outpage.read_text(encoding="utf8") + assert "cowgirls" not in outpage.read_text(encoding="utf8") + def test_both_tags(pages, cli): """Test multiple tag creates boolean condition.""" page = pages.joinpath("single_page.ipynb") html = pages.joinpath("_build", "_page", "single_page", "html") index = html.joinpath("index.html") - result = cli.invoke(commands.build, [page.as_posix(), "-n", "-W", "--keep-going", "--tag", "cowboy", "--tag", "cowgirl"]) + result = cli.invoke( + commands.build, + [ + page.as_posix(), + "-n", + "-W", + "--keep-going", + "--tag", + "cowboy", + "--tag", + "cowgirl", + ], + ) assert result.exit_code == 0, result.output assert html.joinpath("single_page.html").exists() outpage = html.joinpath("single_page.html") assert not html.joinpath("extra_page.html").exists() - assert 'slickers' in outpage.read_text(encoding="utf8") - assert 'cowgirls' in outpage.read_text(encoding="utf8") + assert "slickers" in outpage.read_text(encoding="utf8") + assert "cowgirls" in outpage.read_text(encoding="utf8") From 5d4b3247730bb436ea4f28b22cfcea2f67adab5b Mon Sep 17 00:00:00 2001 From: Joel Danke Date: Fri, 28 Jan 2022 16:37:48 -0500 Subject: [PATCH 3/3] remove unused index vars --- tests/test_build.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/test_build.py b/tests/test_build.py index c44b38a24..b49160de1 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -315,7 +315,6 @@ def test_only(pages, cli): """Test {only} content is not built.""" page = pages.joinpath("single_page.ipynb") html = pages.joinpath("_build", "_page", "single_page", "html") - index = html.joinpath("index.html") result = cli.invoke(commands.build, [page.as_posix(), "-n", "-W", "--keep-going"]) assert result.exit_code == 0, result.output assert html.joinpath("single_page.html").exists() @@ -329,7 +328,6 @@ def test_only_tag(pages, cli): """Test tag cli builds {only} content.""" page = pages.joinpath("single_page.ipynb") html = pages.joinpath("_build", "_page", "single_page", "html") - index = html.joinpath("index.html") result = cli.invoke( commands.build, [page.as_posix(), "-n", "-W", "--keep-going", "--tag", "cowboy"] ) @@ -345,7 +343,6 @@ def test_both_tags(pages, cli): """Test multiple tag creates boolean condition.""" page = pages.joinpath("single_page.ipynb") html = pages.joinpath("_build", "_page", "single_page", "html") - index = html.joinpath("index.html") result = cli.invoke( commands.build, [