Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Add tag to cli for "only" content #1618

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions jupyter_book/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
)
Expand All @@ -150,6 +156,7 @@ def build(
freshenv,
builder,
custom_builder,
tag,
verbose,
quiet,
individualpages,
Expand Down Expand Up @@ -302,6 +309,9 @@ 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(
Expand All @@ -312,6 +322,7 @@ def build(
path_config=path_config,
confoverrides=config_overrides,
builder=BUILDER_OPTS[builder],
tags=tag,
warningiserror=warningiserror,
keep_going=keep_going,
freshenv=freshenv,
Expand Down
27 changes: 27 additions & 0 deletions tests/pages/single_page.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down
53 changes: 53 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,56 @@ 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")
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")
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")
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")