|
1 | 1 | /// This module defines the `Module` struct, its builder struct, and methods on both structs. |
2 | 2 |
|
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | + |
3 | 5 | use crate::Options; |
4 | 6 |
|
5 | 7 | /// All possible types of the AsciiDoc module |
@@ -153,7 +155,41 @@ impl Input { |
153 | 155 |
|
154 | 156 | /// Prepare an include statement that can be used to include the generated file from elsewhere. |
155 | 157 | 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()) |
157 | 193 | } |
158 | 194 |
|
159 | 195 | /// Perform string replacements in the modular template that matches the `ModuleType`. |
|
0 commit comments