|
1 | 1 | use regex::{Regex, RegexBuilder}; |
2 | | -use askama::Template; // bring trait in scope |
| 2 | +use askama::Template; |
3 | 3 |
|
4 | 4 | use crate::module::{Input, ModuleType}; |
5 | 5 |
|
| 6 | +// A note on the structure of this file: |
| 7 | +// This file repeats a lot of code when it configures the Askama templates. |
| 8 | +// Either we could fix it by using a generics trick, which I haven't learned yet, |
| 9 | +// or the repetition is inherent to the way the templates share some properties, but only |
| 10 | +// by accident, not as a rule. |
| 11 | +// For now, the code works as intended, and the file is short enough that I'm not bothered |
| 12 | +// to see the questionable esthetics. |
6 | 13 |
|
7 | | -#[derive(Template)] // this will generate the code... |
8 | | -#[template(path = "assembly.adoc", escape = "none")] // using the template in this path, relative |
9 | | - // to the `templates` dir in the crate root |
10 | | -struct AssemblyTemplate<'a> { // the name of the struct can be anything |
11 | | - module_id: &'a str, // the field name should match the variable name |
12 | | - // in your template |
| 14 | +// Specify a template in terms of the Askama engine |
| 15 | +#[derive(Template)] |
| 16 | +// Askama loads the template files from the `data/templates` directory, |
| 17 | +// which is configured in the `askama.toml` file. |
| 18 | +#[template(path = "assembly.adoc", escape = "none")] |
| 19 | +struct AssemblyTemplate<'a> { |
| 20 | + // The field name must match the variable name in the template |
| 21 | + module_id: &'a str, |
13 | 22 | module_title: &'a str, |
14 | 23 | include_statements: &'a str, |
15 | 24 | examples: bool, |
@@ -40,6 +49,9 @@ struct ReferenceTemplate<'a> { |
40 | 49 | } |
41 | 50 |
|
42 | 51 |
|
| 52 | +// We're implementing the template functions on the Input struct, not on Module, |
| 53 | +// because the templating happens at the point when newdoc composes the text of the module, |
| 54 | +// which is part of the module creation. The module then stores the rendered template. |
43 | 55 | impl Input { |
44 | 56 | /// Render the include statements that appear inside an assembly |
45 | 57 | /// into the final format. If the assembly includes nothing, use |
|
0 commit comments