Skip to content

Commit

Permalink
Add command for individual templates
Browse files Browse the repository at this point in the history
  • Loading branch information
percyfal committed Mar 7, 2024
1 parent 533ecee commit 3d8f4f4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
40 changes: 36 additions & 4 deletions src/nbis/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
__shortname__ = __name__.rsplit(".", maxsplit=1)[-1]


def _add_doc_and_assets(outdir, ext, template="running-slides", **kw):
def _add_doc_and_assets(outdir, ext, template_name="running-slides", **kw):
assets = outdir / kw["assets"]
path = outdir / f"index.{ext}"
www = assets / "www"
Expand All @@ -35,7 +35,7 @@ def _add_doc_and_assets(outdir, ext, template="running-slides", **kw):
www.mkdir(parents=True)
css.mkdir()
logos.mkdir()
templates.add_template(path, f"docs/{template}.{ext}.j2", **kw)
templates.add_template(path, f"docs/{template_name}.{ext}.j2", **kw)
templates.add_template(www / "title-slide.html", "docs/assets/www/title-slide.html")
templates.add_template(www / "tikzfig.tex", "docs/assets/www/tikzfig.tex")
templates.add_template(
Expand All @@ -54,7 +54,7 @@ def main():
@click.option(
"slide_type", "--type", type=click.Choice(["quarto", "rmarkdown"]), default="quarto"
)
@click.option("--show", is_flag=True, help="show template")
@click.option("--show", is_flag=True, help="show rendered template")
@click.option("--title", help="title", default="title")
@click.option("--subtitle", help="subtitle", default="subtitle")
@click.option("--author", help="author")
Expand Down Expand Up @@ -100,7 +100,7 @@ def running_slides(env, slide_type, show, **kw):
type=click.Choice(["quarto", "markdown"]),
default="quarto",
)
@click.option("--show", is_flag=True, help="show template")
@click.option("--show", is_flag=True, help="show rendered template")
@click.option("--title", help="title", default="title")
@click.option("--subtitle", help="subtitle", default="subtitle")
@click.option("--author", help="author")
Expand Down Expand Up @@ -151,6 +151,38 @@ def diary(env, doc_type, show, **kw):
_add_doc_and_assets(outdir, ext, template="diary", **kw)


@main.command()
@click.option(
"template_name",
"--template",
"-t",
type=click.Choice(templates.INDIVIDUAL_TEMPLATES.keys()),
)
@click.option("--show", is_flag=True, help="show rendered template")
@click.option(
"--name",
help="output template to named directory",
type=click.Path(exists=False),
)
@pass_environment
def template(env, template_name, show, **kw):
"""Add individual template"""
logger.info("Adding template %s", template_name)
if template_name is None:
click.echo("No template specified; exiting.")
return
if kw["name"] is None:
kw["name"] = env.home
outdir = pathlib.Path(kw["name"])
tpl = templates.INDIVIDUAL_TEMPLATES[template_name]
path = outdir / pathlib.Path(tpl).with_suffix("").name
if show:
t = templates.render_template(tpl, **kw)
click.echo(t)
else:
templates.add_template(path, tpl, **kw)


@main.command(name="command-group")
@click.argument("command_group")
@click.option("--path", help="output template to path", type=click.Path(exists=False))
Expand Down
12 changes: 12 additions & 0 deletions src/nbis/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
file_loader = FileSystemLoader(template_path)
env = Environment(loader=file_loader)

INDIVIDUAL_TEMPLATES = {
"pyproject.toml": "pyproject.toml.j2",
"gitignore": ".gitignore.j2",
"prettierignore": ".prettierignore.j2",
"markdownlint": ".markdownlint.yaml.j2",
"prettierrc": ".prettierrc.yml.j2",
"editorconfig": ".editorconfig.j2",
"readme": "README.md.j2",
"pre-commit-config": ".pre-commit-config.yaml.j2",
"quarto": "docs/_quarto.yml.j2",
}


def add_template(filename, template, **kwargs):
"""Generic function to render template to filename"""
Expand Down
9 changes: 9 additions & 0 deletions tests/test_add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Test commands."""

from nbis.cli import cli


def test_add_template(runner):
"""Test adding individual template"""
result = runner.invoke(cli, ["add", "template", "-t", "quarto", "--show"])
assert not result.exception
5 changes: 2 additions & 3 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@

def test_init_diary(runner):
"""Test initialization of diary."""
result = runner.invoke(cli, ["diary"])
print(result.stdout)
print(result)
result = runner.invoke(cli, ["add", "diary", "--show"])
assert not result.exception

0 comments on commit 3d8f4f4

Please sign in to comment.