Skip to content

Commit 9a13ca5

Browse files
committed
Improve the comments in the new templating file
1 parent cd60f25 commit 9a13ca5

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/templating.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
use regex::{Regex, RegexBuilder};
2-
use askama::Template; // bring trait in scope
2+
use askama::Template;
33

44
use crate::module::{Input, ModuleType};
55

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.
613

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,
1322
module_title: &'a str,
1423
include_statements: &'a str,
1524
examples: bool,
@@ -40,6 +49,9 @@ struct ReferenceTemplate<'a> {
4049
}
4150

4251

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.
4355
impl Input {
4456
/// Render the include statements that appear inside an assembly
4557
/// into the final format. If the assembly includes nothing, use

0 commit comments

Comments
 (0)