Skip to content

Commit f8975ff

Browse files
committed
Fill out the include path by default; #16
1 parent bf20538 commit f8975ff

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/module.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/// This module defines the `Module` struct, its builder struct, and methods on both structs.
22
3+
use std::path::{Path, PathBuf};
4+
35
use crate::Options;
46

57
/// All possible types of the AsciiDoc module
@@ -153,7 +155,41 @@ impl Input {
153155

154156
/// Prepare an include statement that can be used to include the generated file from elsewhere.
155157
fn include_statement(&self) -> String {
156-
format!("include::<path>/{}[leveloffset=+1]", &self.file_name())
158+
// Determine the start of the include statement from the target path.
159+
// The first directory in the include path is either `assemblies/` or `modules/`,
160+
// based on the module type.
161+
let include_root = match &self.mod_type {
162+
ModuleType::Assembly => "assemblies",
163+
_ => "modules",
164+
};
165+
166+
// TODO: Maybe convert the path earlier in the module building.
167+
let target_path = Path::new(&self.options.target_dir).canonicalize().unwrap();
168+
169+
// Split the target path into components
170+
let mut component_vec: Vec<_> = target_path
171+
.as_path()
172+
.components()
173+
.map(|c| c.as_os_str())
174+
.collect();
175+
176+
// Find the position of the component that matches the root element,
177+
// searching from the end of the path forward.
178+
let root_position = component_vec.iter().rposition(|&c| c == include_root);
179+
180+
// If there is such a root element in the path, construct the include path.
181+
// TODO: To be safe, check that the root path element still exists in a Git repository.
182+
let include_path = if let Some(position) = root_position {
183+
component_vec
184+
.split_off(position)
185+
.iter()
186+
.collect::<PathBuf>()
187+
// If no appropriate root element was found, use a generic placeholder.
188+
} else {
189+
Path::new("<path>").to_path_buf()
190+
};
191+
192+
format!("include::{}/{}[leveloffset=+1]", include_path.display(), &self.file_name())
157193
}
158194

159195
/// Perform string replacements in the modular template that matches the `ModuleType`.

0 commit comments

Comments
 (0)