Skip to content

Commit 352df97

Browse files
committed
Move templating to a separate file
1 parent 4705d53 commit 352df97

File tree

3 files changed

+119
-113
lines changed

3 files changed

+119
-113
lines changed

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use log::{debug, info};
33
mod cmd_line;
44
mod logging;
55
mod module;
6+
mod templating;
67
mod write;
78

89
use module::{Input, Module, ModuleType};

src/module.rs

Lines changed: 5 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/// This module defines the `Module` struct, its builder struct, and methods on both structs.
22
use std::path::{Path, PathBuf};
3-
43
use log::debug;
5-
use regex::{Regex, RegexBuilder};
64

75
use crate::Options;
86

7+
98
/// All possible types of the AsciiDoc module
109
#[derive(Debug, Clone, Copy, PartialEq)]
1110
pub enum ModuleType {
@@ -18,10 +17,10 @@ pub enum ModuleType {
1817
/// An initial representation of the module with input data, used to construct the `Module` struct
1918
#[derive(Debug)]
2019
pub struct Input {
21-
mod_type: ModuleType,
22-
title: String,
23-
options: Options,
24-
includes: Option<Vec<String>>,
20+
pub mod_type: ModuleType,
21+
pub title: String,
22+
pub options: Options,
23+
pub includes: Option<Vec<String>>,
2524
}
2625

2726
/// A representation of the module with all its metadata and the generated AsciiDoc content
@@ -36,12 +35,6 @@ pub struct Module {
3635
pub text: String,
3736
}
3837

39-
// Load the AsciiDoc templates at build time
40-
const ASSEMBLY_TEMPLATE: &str = include_str!("../data/templates/assembly.adoc");
41-
const CONCEPT_TEMPLATE: &str = include_str!("../data/templates/concept.adoc");
42-
const PROCEDURE_TEMPLATE: &str = include_str!("../data/templates/procedure.adoc");
43-
const REFERENCE_TEMPLATE: &str = include_str!("../data/templates/reference.adoc");
44-
4538
/// Construct a basic builder for `Module`, storing information from the user input.
4639
impl Input {
4740
pub fn new(mod_type: &ModuleType, title: &str, options: &Options) -> Input {
@@ -223,107 +216,6 @@ impl Input {
223216
None
224217
}
225218
}
226-
227-
/// Perform string replacements in the modular template that matches the `ModuleType`.
228-
/// Return the template text with all replacements.
229-
pub fn text(&self) -> String {
230-
// TODO: Add a comment in the generated file with a pre-filled include statement
231-
232-
// Pick the right template
233-
let current_template = match self.mod_type {
234-
ModuleType::Assembly => ASSEMBLY_TEMPLATE,
235-
ModuleType::Concept => CONCEPT_TEMPLATE,
236-
ModuleType::Procedure => PROCEDURE_TEMPLATE,
237-
ModuleType::Reference => REFERENCE_TEMPLATE,
238-
};
239-
240-
// Define the strings that will be replaced in the template
241-
let replacements = [
242-
("${module_title}", &self.title),
243-
("${module_id}", &self.id()),
244-
];
245-
246-
// Perform substitutions in the template
247-
// TODO: Create a separate function to perform a replacement
248-
let mut template_with_replacements = String::from(current_template);
249-
250-
for (old, new) in replacements.iter() {
251-
template_with_replacements = template_with_replacements.replace(old, new);
252-
}
253-
254-
if let Some(include_statements) = &self.includes {
255-
// The includes should never be empty thanks to the required group in clap
256-
assert!(!include_statements.is_empty());
257-
// Join the includes into a block of text, with blank lines in between to prevent
258-
// the AsciiDoc syntax to blend between modules
259-
let includes_text = include_statements.join("\n\n");
260-
261-
template_with_replacements =
262-
template_with_replacements.replace("${include_statements}", &includes_text);
263-
} else if self.options.examples {
264-
template_with_replacements = template_with_replacements
265-
.replace("${include_statements}", "Include modules here.");
266-
} else {
267-
template_with_replacements =
268-
template_with_replacements.replace("${include_statements}\n", "");
269-
}
270-
271-
// If the `--no-examples` option is active, remove all lines between the <example> tags.
272-
if !self.options.examples {
273-
let examples: Regex = RegexBuilder::new(r"^// <example>\n[\s\S]*\n^// </example>\n")
274-
.multi_line(true)
275-
.swap_greed(true)
276-
.build()
277-
.unwrap();
278-
template_with_replacements = examples
279-
.replace_all(&template_with_replacements, "")
280-
.to_string();
281-
// If the `--no-examples` option isn't active, remove just the <example> tags.
282-
} else {
283-
let example_tags: Regex = RegexBuilder::new(r"^// </?example>\n")
284-
.multi_line(true)
285-
.swap_greed(true)
286-
.build()
287-
.unwrap();
288-
template_with_replacements = example_tags
289-
.replace_all(&template_with_replacements, "")
290-
.to_string();
291-
}
292-
293-
// If comments are disabled via an option, delete comment lines from the content
294-
if !self.options.comments {
295-
// Delete multi-line (block) comments
296-
let multi_comments: Regex = RegexBuilder::new(r"^////[\s\S\n]*^////[\s]*\n")
297-
.multi_line(true)
298-
.swap_greed(true)
299-
.build()
300-
.unwrap();
301-
template_with_replacements = multi_comments
302-
.replace_all(&template_with_replacements, "")
303-
.to_string();
304-
305-
// Delete single-line comments
306-
let single_comments: Regex = RegexBuilder::new(r"^//.*\n")
307-
.multi_line(true)
308-
.swap_greed(true)
309-
.build()
310-
.unwrap();
311-
template_with_replacements = single_comments
312-
.replace_all(&template_with_replacements, "")
313-
.to_string();
314-
315-
// Delete leading white space left over by the deleted comments
316-
let leading_whitespace: Regex = RegexBuilder::new(r"^[\s\n]*")
317-
.multi_line(true)
318-
.build()
319-
.unwrap();
320-
template_with_replacements = leading_whitespace
321-
.replace(&template_with_replacements, "")
322-
.to_string();
323-
}
324-
325-
template_with_replacements
326-
}
327219
}
328220

329221
impl From<Input> for Module {

src/templating.rs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
use regex::{Regex, RegexBuilder};
2+
3+
use crate::module::{Input, ModuleType};
4+
5+
// Load the AsciiDoc templates at build time
6+
const ASSEMBLY_TEMPLATE: &str = include_str!("../data/templates/assembly.adoc");
7+
const CONCEPT_TEMPLATE: &str = include_str!("../data/templates/concept.adoc");
8+
const PROCEDURE_TEMPLATE: &str = include_str!("../data/templates/procedure.adoc");
9+
const REFERENCE_TEMPLATE: &str = include_str!("../data/templates/reference.adoc");
10+
11+
12+
impl Input {
13+
/// Perform string replacements in the modular template that matches the `ModuleType`.
14+
/// Return the template text with all replacements.
15+
pub fn text(&self) -> String {
16+
// TODO: Add a comment in the generated file with a pre-filled include statement
17+
18+
// Pick the right template
19+
let current_template = match self.mod_type {
20+
ModuleType::Assembly => ASSEMBLY_TEMPLATE,
21+
ModuleType::Concept => CONCEPT_TEMPLATE,
22+
ModuleType::Procedure => PROCEDURE_TEMPLATE,
23+
ModuleType::Reference => REFERENCE_TEMPLATE,
24+
};
25+
26+
// Define the strings that will be replaced in the template
27+
let replacements = [
28+
("${module_title}", &self.title),
29+
("${module_id}", &self.id()),
30+
];
31+
32+
// Perform substitutions in the template
33+
// TODO: Create a separate function to perform a replacement
34+
let mut template_with_replacements = String::from(current_template);
35+
36+
for (old, new) in replacements.iter() {
37+
template_with_replacements = template_with_replacements.replace(old, new);
38+
}
39+
40+
if let Some(include_statements) = &self.includes {
41+
// The includes should never be empty thanks to the required group in clap
42+
assert!(!include_statements.is_empty());
43+
// Join the includes into a block of text, with blank lines in between to prevent
44+
// the AsciiDoc syntax to blend between modules
45+
let includes_text = include_statements.join("\n\n");
46+
47+
template_with_replacements =
48+
template_with_replacements.replace("${include_statements}", &includes_text);
49+
} else if self.options.examples {
50+
template_with_replacements = template_with_replacements
51+
.replace("${include_statements}", "Include modules here.");
52+
} else {
53+
template_with_replacements =
54+
template_with_replacements.replace("${include_statements}\n", "");
55+
}
56+
57+
// If the `--no-examples` option is active, remove all lines between the <example> tags.
58+
if !self.options.examples {
59+
let examples: Regex = RegexBuilder::new(r"^// <example>\n[\s\S]*\n^// </example>\n")
60+
.multi_line(true)
61+
.swap_greed(true)
62+
.build()
63+
.unwrap();
64+
template_with_replacements = examples
65+
.replace_all(&template_with_replacements, "")
66+
.to_string();
67+
// If the `--no-examples` option isn't active, remove just the <example> tags.
68+
} else {
69+
let example_tags: Regex = RegexBuilder::new(r"^// </?example>\n")
70+
.multi_line(true)
71+
.swap_greed(true)
72+
.build()
73+
.unwrap();
74+
template_with_replacements = example_tags
75+
.replace_all(&template_with_replacements, "")
76+
.to_string();
77+
}
78+
79+
// If comments are disabled via an option, delete comment lines from the content
80+
if !self.options.comments {
81+
// Delete multi-line (block) comments
82+
let multi_comments: Regex = RegexBuilder::new(r"^////[\s\S\n]*^////[\s]*\n")
83+
.multi_line(true)
84+
.swap_greed(true)
85+
.build()
86+
.unwrap();
87+
template_with_replacements = multi_comments
88+
.replace_all(&template_with_replacements, "")
89+
.to_string();
90+
91+
// Delete single-line comments
92+
let single_comments: Regex = RegexBuilder::new(r"^//.*\n")
93+
.multi_line(true)
94+
.swap_greed(true)
95+
.build()
96+
.unwrap();
97+
template_with_replacements = single_comments
98+
.replace_all(&template_with_replacements, "")
99+
.to_string();
100+
101+
// Delete leading white space left over by the deleted comments
102+
let leading_whitespace: Regex = RegexBuilder::new(r"^[\s\n]*")
103+
.multi_line(true)
104+
.build()
105+
.unwrap();
106+
template_with_replacements = leading_whitespace
107+
.replace(&template_with_replacements, "")
108+
.to_string();
109+
}
110+
111+
template_with_replacements
112+
}
113+
}

0 commit comments

Comments
 (0)