11/// This module defines the `Module` struct, its builder struct, and methods on both structs.
22use std:: path:: { Path , PathBuf } ;
3-
43use log:: debug;
5- use regex:: { Regex , RegexBuilder } ;
64
75use crate :: Options ;
86
7+
98/// All possible types of the AsciiDoc module
109#[ derive( Debug , Clone , Copy , PartialEq ) ]
1110pub 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 ) ]
2019pub 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.
4639impl 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
329221impl From < Input > for Module {
0 commit comments