Skip to content

Commit

Permalink
Add delimiters option
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed Dec 18, 2022
1 parent ccc21bd commit e3cfdab
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ Arguments:
SOURCE The template file to use. [required]

Options:
-c, --context PATH Context file(s) to use.
-e, --eval Parse source as template string.
-v, --var TEXT Context variable(s) to apply.
-c, --context PATH Context file(s) to use.
-f, --format [env|ini|json|toml|yaml|yml]
Context file format (default: guess from
file extension).
-o, --output FILENAME Target output (default: stdout)
-v, --var PATH Context variable(s) to apply.
--delimiters TEXT Template delimiters [default: {{,}}]
--version Show the version and exit.
--install-completion [bash|zsh|fish|powershell|pwsh]
Install completion for the specified shell.
Expand Down
37 changes: 25 additions & 12 deletions noir/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .__version__ import __version__
from ._patch import typer
from .ctx import ContextExt, load_context_file
from .templating import templater
from .templating import templater as _build_templater
from .types import ContextFilePath, ContextVar, Source
from .utils import obj_to_adict

Expand Down Expand Up @@ -52,18 +52,24 @@ def main(
...,
help="The template file to use."
),
context: List[ContextFilePath] = typer.Option(
[],
"--context",
"-c",
help="Context file(s) to use."
),
eval: bool = typer.Option(
False,
"--eval",
"-e",
help="Parse source as template string."
),
var: List[ContextVar] = typer.Option(
[],
"--var",
"-v",
help="Context variable(s) to apply."
),
context: List[ContextFilePath] = typer.Option(
[],
"--context",
"-c",
help="Context file(s) to use."
),
format: Optional[ContextExt] = typer.Option(
None,
"--format",
Expand All @@ -77,11 +83,10 @@ def main(
show_default=False,
help="Target output (default: stdout)"
),
var: List[ContextVar] = typer.Option(
[],
"--var",
"-v",
help="Context variable(s) to apply."
delimiters: str = typer.Option(
"{{,}}",
"--delimiters",
help="Template delimiters"
),
_: Optional[bool] = typer.Option(
None, "--version", callback=version_callback, is_eager=True,
Expand All @@ -94,6 +99,12 @@ def main(

if not source.is_path and not eval:
error(f"Invalid value for 'SOURCE': Path '{source.data}' does not exist.")
try:
delimiters_items = tuple(delimiters.split(","))
assert len(delimiters_items) == 2
except Exception:
error(f"Invalid value for delimiters: '{delimiters}'")

stream_reader = lambda: typer.get_text_stream("stdin")
tctx = {}
for ctx_path in context:
Expand All @@ -112,6 +123,8 @@ def main(
rctx[ns] = rctx.get(ns) or {}
rctx = rctx[ns]
rctx[var_param.key] = var_param.val

templater = _build_templater(delimiters_items)
try:
rendered = (
templater.render(source.data, obj_to_adict(tctx)) if not eval else
Expand Down
12 changes: 10 additions & 2 deletions noir/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import random

from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, Tuple

import tomli_w
import yaml
Expand All @@ -30,6 +30,15 @@ class Templater(Renoir):
_writers = {**Renoir._writers, **{ESCAPES.common: Writer}}


def templater(delimiters: Tuple[str, str]):
return Templater(
mode=MODES.plain,
delimiters=delimiters,
adjust_indent=True,
contexts=[base_ctx]
)


def _indent(text: str, spaces: int = 2) -> str:
offset = " " * spaces
rv = f"\n{offset}".join(text.split("\n"))
Expand Down Expand Up @@ -63,4 +72,3 @@ def base_ctx(ctx: Dict[str, Any]):


yaml.add_representer(adict, yaml.representer.Representer.represent_dict)
templater = Templater(mode=MODES.plain, adjust_indent=True, contexts=[base_ctx])
4 changes: 3 additions & 1 deletion tests/test_render.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json
import os

from noir.templating import templater
from noir.templating import templater as _build_templater

templater = _build_templater(('{{', '}}'))


def test_env():
Expand Down

0 comments on commit e3cfdab

Please sign in to comment.