Skip to main content

ctl_core/surface/
templates.rs

1//! `MiniJinja` rendering for a CLI's generated documentation files.
2
3use minijinja::{Environment, Error, context};
4use serde::Serialize;
5
6use super::Surface;
7
8/// Template source for `ctl/version.md.jinja`: the macro
9/// `version_line(version)` renders the `version:` line of a generated file's
10/// frontmatter.
11pub const VERSION_FRAGMENT: &str = r"{% macro version_line(version) -%}
12version: {{ version }}
13{%- endmacro %}";
14
15/// Template source for `ctl/invocation.md.jinja`: the macro
16/// `mounted_invocation(surface, examples)` renders example `mise run` lines
17/// and the rule that they take no `--`.
18pub const INVOCATION_FRAGMENT: &str = r"{% macro mounted_invocation(surface, examples) -%}
19## Invocation
20
21```sh
22{% for example in examples -%}
23mise run {{ surface.mount }} {{ example }}
24{% endfor -%}
25```
26
27Never `mise run {{ surface.mount }} --`. The `--` in `#USAGE mount` is mise's
28completion bootstrap.
29{%- endmacro %}";
30
31/// Template source for `ctl/commands.md.jinja`: the macro
32/// `command_inventory(surface)` renders a table of the visible top-level
33/// commands, with their visible aliases and descriptions.
34pub const COMMANDS_FRAGMENT: &str = r#"{% macro command_inventory(surface) -%}
35## Commands
36
37| Command | Aliases | Purpose |
38|:--|:--|:--|
39{% for command in surface.commands if not command.hidden -%}
40| `{{ command.name }}` | {% if command.visible_aliases %}`{{ command.visible_aliases | join("`, `") }}`{% else %}—{% endif %} | {{ command.about | replace("|", "\\|") | replace("\n", " ") }} |
41{% endfor -%}
42{{- "" -}}
43{%- endmacro %}"#;
44
45const FRAGMENTS: [(&str, &str); 3] = [
46    ("ctl/version.md.jinja", VERSION_FRAGMENT),
47    ("ctl/invocation.md.jinja", INVOCATION_FRAGMENT),
48    ("ctl/commands.md.jinja", COMMANDS_FRAGMENT),
49];
50
51/// Add ctl-core's shared fragments to `environment`, under the names
52/// `ctl/version.md.jinja`, `ctl/invocation.md.jinja`, and
53/// `ctl/commands.md.jinja`. A template imports them from there.
54///
55/// # Errors
56///
57/// Returns minijinja's [`Error`] when a fragment does not compile under
58/// `environment`'s syntax.
59pub fn add_fragments(environment: &mut Environment<'static>) -> Result<(), Error> {
60    for (name, source) in FRAGMENTS {
61        environment.add_template(name, source)?;
62    }
63    Ok(())
64}
65
66/// Create an [`Environment`] that holds the shared fragments.
67///
68/// It is strict: an undefined variable is an error, not an empty string. It
69/// keeps the trailing newline of a template.
70///
71/// # Errors
72///
73/// Returns the [`Error`] from [`add_fragments`].
74pub fn environment() -> Result<Environment<'static>, Error> {
75    let mut environment = Environment::new();
76    environment.set_undefined_behavior(minijinja::UndefinedBehavior::Strict);
77    environment.set_keep_trailing_newline(true);
78    add_fragments(&mut environment)?;
79    Ok(environment)
80}
81
82/// Render the template `source`, named `name`, in a fresh
83/// [`environment`].
84///
85/// The template sees two variables: `surface`, the [`Surface`], and `content`,
86/// the CLI's own serializable context.
87///
88/// # Errors
89///
90/// Returns minijinja's [`Error`] when `source` does not compile, or when
91/// rendering fails, such as on a variable the strict environment does not
92/// define.
93pub fn render<T: Serialize>(
94    name: &'static str,
95    source: &'static str,
96    surface: &Surface,
97    content: &T,
98) -> Result<String, Error> {
99    let mut environment = environment()?;
100    environment.add_template(name, source)?;
101    environment
102        .get_template(name)?
103        .render(context! { surface, content })
104}