Skip to content

Commit 1b52558

Browse files
author
Marek Suchánek
committed
Implement the snippet module type; #13
1 parent 32af816 commit 1b52558

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/module.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub enum ModuleType {
1212
Concept,
1313
Procedure,
1414
Reference,
15+
Snippet,
1516
}
1617

1718
// Implement human-readable string display for the module type
@@ -22,6 +23,7 @@ impl fmt::Display for ModuleType {
2223
Self::Concept => "concept",
2324
Self::Procedure => "procedure",
2425
Self::Reference => "reference",
26+
Self::Snippet => "snippet",
2527
};
2628
write!(f, "{}", name)
2729
}
@@ -128,7 +130,7 @@ impl Input {
128130
];
129131

130132
// Perform all the defined replacements on the title
131-
for (old, new) in substitutions.iter() {
133+
for (old, new) in substitutions.into_iter() {
132134
title_with_replacements = title_with_replacements.replace(old, new);
133135
}
134136

@@ -140,7 +142,7 @@ impl Input {
140142

141143
let prefix = self.prefix();
142144

143-
prefix + &title_with_replacements
145+
format!("{}{}", prefix, title_with_replacements)
144146
}
145147

146148
/// Prepare the file name for the generated file.
@@ -152,20 +154,20 @@ impl Input {
152154
self.id() + suffix
153155
}
154156

155-
fn prefix(&self) -> String {
157+
fn prefix(&self) -> &'static str {
156158
if self.options.prefixes {
157159
// If prefixes are enabled, pick the right file prefix
158160
match self.mod_type {
159161
ModuleType::Assembly => "assembly_",
160162
ModuleType::Concept => "con_",
161163
ModuleType::Procedure => "proc_",
162164
ModuleType::Reference => "ref_",
165+
ModuleType::Snippet => "snip_",
163166
}
164167
} else {
165168
// If prefixes are disabled, use an empty string for the prefix
166169
""
167170
}
168-
.to_string()
169171
}
170172

171173
/// Prepare an include statement that can be used to include the generated file from elsewhere.
@@ -193,9 +195,10 @@ impl Input {
193195
/// to determine it automatically.
194196
fn infer_include_dir(&self) -> Option<PathBuf> {
195197
// The first directory in the include path is either `assemblies/` or `modules/`,
196-
// based on the module type.
198+
// based on the module type, or `snippets/` for snippet files.
197199
let include_root = match &self.mod_type {
198200
ModuleType::Assembly => "assemblies",
201+
ModuleType::Snippet => "snippets",
199202
_ => "modules",
200203
};
201204

src/templating.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ struct ReferenceTemplate<'a> {
4848
examples: bool,
4949
}
5050

51+
#[derive(Template)]
52+
#[template(path = "snippet.adoc", escape = "none")]
53+
struct SnippetTemplate<'a> {
54+
module_title: &'a str,
55+
examples: bool,
56+
}
57+
5158
// We're implementing the template functions on the Input struct, not on Module,
5259
// because the templating happens at the point when newdoc composes the text of the module,
5360
// which is part of the module creation. The module then stores the rendered template.
@@ -98,6 +105,11 @@ impl Input {
98105
examples: self.options.examples,
99106
}
100107
.render(),
108+
ModuleType::Snippet => SnippetTemplate {
109+
module_title: &self.title,
110+
examples: self.options.examples,
111+
}
112+
.render(),
101113
}
102114
.expect("Failed to construct the document from the template");
103115

0 commit comments

Comments
 (0)