Usage: makejinja [OPTIONS]
makejinja can be used to automatically generate files from Jinja templates.
Instead of passing CLI options, you can also write them to a file called makejinja.toml in your working directory.
Note: In this file, options may be named differently. Please refer to the file makejinja/config.py to see their actual
names. You will also find an example here: makejinja/tests/data/makejinja.toml. To override its location, you can set
the environment variable MAKEJINJA_SETTINGS to the path of your config file.
╭─ Input/Output ───────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --input -i PATH Path to a directory containing template files or a single template file. │
│ It is passed to Jinja's FileSystemLoader when creating the environment. │
│ Note: This option may be passed multiple times to pass a list of values. │
│ If a template exists in multiple inputs, the last value with be used. │
│ * --output -o PATH Path to a directory where the rendered templates are stored. makejinja │
│ preserves the relative paths in the process, meaning that you can even use │
│ it on nested directories. │
│ [required] │
│ --include-pattern,--include -I TEXT Glob patterns to search for files in inputs. Accepts all pattern supported │
│ by fnmatch. If a file is matched by this pattern and does not end with the │
│ specified jinja-suffix, it is copied over to output. Multiple can be │
│ provided. Note: Do not add a special suffix used by your template files │
│ here, instead use the jinja-suffix option. │
│ [default: **/*] │
│ --exclude-pattern,--exclude -E TEXT Glob patterns pattern to exclude files matched. Applied against files │
│ discovered through include_patterns. Multiple can be provided. │
│ --jinja-suffix TEXT File ending of Jinja template files. All files with this suffix in inputs │
│ matched by pattern are passed to the Jinja renderer. This suffix is not │
│ enforced for individual files passed to inputs. Note: Should be provided │
│ with the leading dot. If empty, all files are considered to be Jinja │
│ templates. │
│ [default: .jinja] │
│ --keep-jinja-suffix Decide whether the specified jinja-suffix is removed from the file name │
│ after rendering. │
│ --keep-empty Some Jinja template files may be empty after rendering (e.g., if they only │
│ contain macros that are imported by other templates). By default, we do │
│ not copy such empty files. If there is a need to have them available │
│ anyway, you can adjust that. │
│ --copy-metadata -m Copy the file metadata (e.g., created/modified/permissions) from the input │
│ file using shutil.copystat │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Jinja Environment ──────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --data -d PATH Load variables from yaml/yml/toml/json files for use in your │
│ Jinja templates. The definitions are passed to Jinja as │
│ globals. Can either be a file or a directory containing files. │
│ Note: This option may be passed multiple times to pass a list │
│ of values. If multiple files are supplied, beware that previous │
│ declarations will be overwritten by newer ones. │
│ --data-var -D KEY=VALUE Load variables from the command line for use in your Jinja │
│ templates. The definitions are applied after loading the data │
│ from files. When using dotted keys (e.g., foo.bar=42), the │
│ value is converted to a nested dictionary. Consequently, you │
│ can override values loaded from files. Note: This option may be │
│ passed multiple times. │
│ --plugin -p TEXT Use custom Python code to adjust the used Jinja environment to │
│ your needs. The specified Python file should export a class │
│ containing a subset of the following functions: filters, │
│ globals, data, and extensions. In addition, you may add an │
│ __init__ function that receives two positional arguments: the │
│ created Jinja environment and the data parsed from the files │
│ supplied to makejinja's data option. This allows you to apply │
│ arbitrary logic to makejinja. An import path can be specified │
│ either in dotted notation (your.custom.Plugin) or with a colon │
│ as object delimiter (your.custom:Plugin). Note: This option may │
│ be passed multiple times to pass a list of values. │
│ --import-path DIRECTORY In order to load plugins or Jinja extensions, the PYTHONPATH │
│ variable needs to be patched. The default value works for most │
│ use cases, but you may load other paths as well. │
│ [default: (current working directory)] │
│ --extension -e TEXT List of Jinja extensions to use as strings of import paths. An │
│ overview of the built-in ones can be found on the project │
│ website. Note: This option may be passed multiple times to pass │
│ a list of values. │
│ --undefined [default|chainable|debug|strict] Whenever the template engine is unable to look up a name or │
│ access an attribute one of those objects is created and │
│ returned. Some operations on undefined values are then allowed, │
│ others fail. The closest to regular Python behavior is strict │
│ which disallows all operations beside testing if it is an │
│ undefined object. │
│ [default: default] │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Jinja Whitespace ───────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --lstrip-blocks/--no-lstrip-blocks If this is set to True, leading spaces and tabs are │
│ stripped from the start of a line to a block. │
│ [default: lstrip-blocks] │
│ --trim-blocks/--no-trim-blocks If this is set to True, the first newline after a block is │
│ removed (block, not variable tag!). │
│ [default: trim-blocks] │
│ --keep-trailing-newline/--strip-trailing-newline Preserve the trailing newline when rendering templates. │
│ The default is False, which causes a single newline, if │
│ present, to be stripped from the end of the template. │
│ [default: keep-trailing-newline] │
│ --newline-sequence TEXT The sequence that starts a newline. The default is │
│ tailored for UNIX-like systems (Linux/macOS). │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Jinja Delimiters ───────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --delimiter-block-start TEXT The string marking the beginning of a block. │
│ [default: {%] │
│ --delimiter-block-end TEXT The string marking the end of a block. │
│ [default: %}] │
│ --delimiter-comment-start TEXT The string marking the beginning of a comment. │
│ [default: {#] │
│ --delimiter-comment-end TEXT The string marking the end of a comment. │
│ [default: #}] │
│ --delimiter-variable-start TEXT The string marking the beginning of a print statement. │
│ [default: {{] │
│ --delimiter-variable-end TEXT The string marking the end of a print statement. │
│ [default: }}] │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Jinja Prefixes ─────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --prefix-line-statement TEXT If given and a string, this will be used as prefix for line based statements. │
│ --prefix-line-comment TEXT If given and a string, this will be used as prefix for line based comments. │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Shell Hooks ────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --exec-pre TEXT Shell commands to execute before rendering. │
│ --exec-post TEXT Shell commands to execute after rendering. │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --version -v Show the version and exit. │
│ --clean -c Whether to remove the output directory if it exists. │
│ --force -f Whether to overwrite existing files in the output directory. │
│ --quiet -q Print no information about the rendering process. │
│ --help -h Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
1"""
2.. include:: ../manpage.md
3"""
4
5from pathlib import Path
6
7import rich_click as click
8import typed_settings as ts
9
10from makejinja.config import OPTION_GROUPS, Config
11
12from .app import makejinja
13
14__all__: list[str] = []
15
16click.rich_click.USE_MARKDOWN = True
17click.rich_click.OPTION_GROUPS = OPTION_GROUPS
18
19_ts_loaders = ts.default_loaders(
20 appname="makejinja", config_files=(Path("makejinja.toml"),)
21)
22
23
24@click.command("makejinja", context_settings={"help_option_names": ("--help", "-h")})
25@click.version_option(None, "--version", "-v")
26@ts.click_options(Config, _ts_loaders)
27def makejinja_cli(config: Config):
28 """makejinja can be used to automatically generate files from [Jinja templates](https://jinja.palletsprojects.com/en/3.1.x/templates/).
29
30 Instead of passing CLI options, you can also write them to a file called `makejinja.toml` in your working directory.
31 **Note**: In this file, options may be named differently.
32 Please refer to the file [`makejinja/config.py`](https://github.com/mirkolenz/makejinja/blob/main/makejinja/config.py) to see their actual names.
33 You will also find an example here: [`makejinja/tests/data/makejinja.toml`](https://github.com/mirkolenz/makejinja/blob/main/tests/data/makejinja.toml).
34 To override its location, you can set the environment variable `MAKEJINJA_SETTINGS` to the path of your config file.
35 """
36
37 makejinja(config)
38
39
40if __name__ == "__main__":
41 makejinja_cli()