1
1
//! The library crate for the mdbook LAD preprocessor.
2
2
#![ allow( missing_docs) ]
3
3
4
- use mdbook:: { errors:: Error , preprocess:: Preprocessor } ;
4
+ use mdbook:: { errors:: Error , preprocess:: Preprocessor , BookItem } ;
5
+ use sections:: Section ;
5
6
mod argument_visitor;
6
7
mod markdown;
7
8
mod sections;
@@ -23,59 +24,143 @@ impl Preprocessor for LADPreprocessor {
23
24
let mut errors = Vec :: default ( ) ;
24
25
25
26
book. for_each_mut ( |item| {
26
- if let mdbook:: BookItem :: Chapter ( chapter) = item {
27
- let is_lad_chapter = chapter
28
- . source_path
29
- . as_ref ( )
30
- . and_then ( |a| a. file_name ( ) )
31
- . is_some_and ( |a| a. to_string_lossy ( ) . ends_with ( LAD_EXTENSION ) ) ;
32
-
33
- if !is_lad_chapter {
34
- log:: debug!( "Skipping non-LAD chapter: {:?}" , chapter. source_path) ;
35
- log:: trace!(
36
- "Non-LAD chapter: {}" ,
37
- serde_json:: to_string_pretty( & chapter) . unwrap_or_default( )
38
- ) ;
39
- return ;
27
+ if let mdbook:: BookItem :: Chapter ( parent) = item {
28
+ let mut replacements = Vec :: default ( ) ;
29
+ for ( ladfile_idx, child) in parent. sub_items . iter ( ) . enumerate ( ) {
30
+ if let BookItem :: Chapter ( child) = child {
31
+ let is_lad_file = child
32
+ . source_path
33
+ . as_ref ( )
34
+ . and_then ( |a| a. file_name ( ) )
35
+ . is_some_and ( |a| a. to_string_lossy ( ) . ends_with ( LAD_EXTENSION ) ) ;
36
+
37
+ if !is_lad_file {
38
+ continue ;
39
+ }
40
+
41
+ let chapter_title =
42
+ child. name . clone ( ) . trim_end_matches ( ".lad.json" ) . to_owned ( ) ;
43
+
44
+ let ladfile = match ladfile:: parse_lad_file ( & child. content ) {
45
+ Ok ( lad) => lad,
46
+ Err ( e) => {
47
+ log:: debug!( "Failed to parse LAD file: {:?}" , e) ;
48
+ errors. push ( Error :: new ( e) . context ( "Failed to parse LAD file" ) ) ;
49
+ continue ;
50
+ }
51
+ } ;
52
+
53
+ log:: debug!(
54
+ "Parsed LAD file: {}" ,
55
+ serde_json:: to_string_pretty( & ladfile) . unwrap_or_default( )
56
+ ) ;
57
+
58
+ let new_chapter = Section :: Summary {
59
+ ladfile : & ladfile,
60
+ title : Some ( chapter_title) ,
61
+ }
62
+ . into_chapter ( Some ( parent) , ladfile_idx) ;
63
+
64
+ log:: debug!(
65
+ "New chapter: {}" ,
66
+ serde_json:: to_string_pretty( & new_chapter) . unwrap_or_default( )
67
+ ) ;
68
+
69
+ // replace
70
+ replacements. push ( ( ladfile_idx, BookItem :: Chapter ( new_chapter) ) ) ;
71
+ }
40
72
}
41
73
42
- let chapter_title = chapter. name . clone ( ) ;
43
-
44
- let lad = match ladfile:: parse_lad_file ( & chapter. content ) {
45
- Ok ( lad) => lad,
46
- Err ( e) => {
47
- log:: debug!( "Failed to parse LAD file: {:?}" , e) ;
48
- errors. push ( Error :: new ( e) . context ( "Failed to parse LAD file" ) ) ;
49
- return ;
50
- }
51
- } ;
52
-
53
- log:: debug!(
54
- "Parsed LAD file: {}" ,
55
- serde_json:: to_string_pretty( & lad) . unwrap_or_default( )
56
- ) ;
57
-
58
- let sections = sections:: lad_file_to_sections ( & lad, Some ( chapter_title) ) ;
59
-
60
- let new_chapter = sections:: section_to_chapter (
61
- sections,
62
- Some ( chapter) ,
63
- chapter. parent_names . clone ( ) ,
64
- chapter. number . clone ( ) ,
65
- None ,
66
- None ,
67
- ) ;
68
-
69
- // serialize chapter to json
70
- log:: debug!(
71
- "New chapter: {}" ,
72
- serde_json:: to_string_pretty( & new_chapter) . unwrap_or_default( )
73
- ) ;
74
-
75
- * chapter = new_chapter;
74
+ for ( idx, replacement) in replacements {
75
+ log:: debug!(
76
+ "Replacing chapter at index {}. With : \n {}" ,
77
+ idx,
78
+ serde_json:: to_string_pretty( & replacement) . unwrap_or_default( )
79
+ ) ;
80
+ parent. sub_items [ idx] = replacement;
81
+ }
76
82
}
77
83
} ) ;
78
84
85
+ // book.for_each_mut(|item| {
86
+ // if let mdbook::BookItem::Chapter(chapter) = item {
87
+ // let is_lad_chapter = chapter
88
+ // .source_path
89
+ // .as_ref()
90
+ // .and_then(|a| a.file_name())
91
+ // .is_some_and(|a| a.to_string_lossy().ends_with(LAD_EXTENSION));
92
+
93
+ // if !is_lad_chapter {
94
+ // log::debug!("Skipping non-LAD chapter: {:?}", chapter.source_path);
95
+ // log::trace!(
96
+ // "Non-LAD chapter: {}",
97
+ // serde_json::to_string_pretty(&chapter).unwrap_or_default()
98
+ // );
99
+ // return;
100
+ // }
101
+
102
+ // let chapter_title = chapter
103
+ // .name
104
+ // .clone()
105
+ // .trim_end_matches(".lad.json")
106
+ // .to_owned();
107
+
108
+ // let lad = match ladfile::parse_lad_file(&chapter.content) {
109
+ // Ok(lad) => lad,
110
+ // Err(e) => {
111
+ // log::debug!("Failed to parse LAD file: {:?}", e);
112
+ // errors.push(Error::new(e).context("Failed to parse LAD file"));
113
+ // return;
114
+ // }
115
+ // };
116
+
117
+ // log::debug!(
118
+ // "Parsed LAD file: {}",
119
+ // serde_json::to_string_pretty(&lad).unwrap_or_default()
120
+ // );
121
+
122
+ // let mut new_chapter = Section::Summary {
123
+ // ladfile: &lad,
124
+ // title: Some(chapter_title),
125
+ // }
126
+ // .into_chapter(None, 0);
127
+
128
+ // new_chapter.path = new_chapter
129
+ // .path
130
+ // .map(|m| chapter.path.as_ref().cloned().unwrap_or_default().join(m));
131
+
132
+ // new_chapter.source_path = new_chapter.source_path.map(|m| {
133
+ // chapter
134
+ // .source_path
135
+ // .as_ref()
136
+ // .cloned()
137
+ // .unwrap_or_default()
138
+ // .join(m)
139
+ // });
140
+
141
+ // new_chapter.parent_names = chapter.parent_names.clone();
142
+
143
+ // // let sections = sections::lad_file_to_sections(&lad, Some(chapter_title));
144
+
145
+ // // let new_chapter = sections::section_to_chapter(
146
+ // // sections,
147
+ // // Some(chapter),
148
+ // // chapter.parent_names.clone(),
149
+ // // chapter.number.clone(),
150
+ // // None,
151
+ // // None,
152
+ // // );
153
+
154
+ // // serialize chapter to json
155
+ // log::debug!(
156
+ // "New chapter: {}",
157
+ // serde_json::to_string_pretty(&new_chapter).unwrap_or_default()
158
+ // );
159
+
160
+ // *chapter = new_chapter;
161
+ // }
162
+ // });
163
+
79
164
if !errors. is_empty ( ) {
80
165
// return on first error
81
166
for error in errors {
0 commit comments