@@ -155,7 +155,24 @@ impl Input {
155155
156156 /// Prepare an include statement that can be used to include the generated file from elsewhere.
157157 fn include_statement ( & self ) -> String {
158- // Determine the start of the include statement from the target path.
158+ let path_placeholder = Path :: new ( "<path>" ) . to_path_buf ( ) ;
159+
160+ let include_path = if self . options . detect_directory {
161+ match self . infer_include_dir ( ) {
162+ Some ( path) => path,
163+ None => path_placeholder,
164+ }
165+ } else {
166+ path_placeholder
167+ } ;
168+
169+ format ! ( "include::{}/{}[leveloffset=+1]" , include_path. display( ) , & self . file_name( ) )
170+ }
171+
172+ /// Determine the start of the include statement from the target path.
173+ /// Returns the relative path that can be used in the include statement, if it's possible
174+ /// to determine it automatically.
175+ fn infer_include_dir ( & self ) -> Option < PathBuf > {
159176 // The first directory in the include path is either `assemblies/` or `modules/`,
160177 // based on the module type.
161178 let include_root = match & self . mod_type {
@@ -179,19 +196,19 @@ impl Input {
179196
180197 // If there is such a root element in the path, construct the include path.
181198 // 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
199+ if let Some ( position) = root_position {
200+ let include_path = component_vec
184201 . split_off ( position)
185202 . iter ( )
186- . collect :: < PathBuf > ( )
203+ . collect :: < PathBuf > ( ) ;
204+ Some ( include_path)
187205 // If no appropriate root element was found, use a generic placeholder.
188206 } else {
189- Path :: new ( "<path>" ) . to_path_buf ( )
190- } ;
191-
192- format ! ( "include::{}/{}[leveloffset=+1]" , include_path. display( ) , & self . file_name( ) )
207+ None
208+ }
193209 }
194210
211+
195212 /// Perform string replacements in the modular template that matches the `ModuleType`.
196213 /// Return the template text with all replacements.
197214 pub fn text ( & self ) -> String {
0 commit comments