@@ -3,14 +3,14 @@ use std::io::Read;
3
3
use std:: io:: Write ;
4
4
use std:: path:: PathBuf ;
5
5
6
+ use mdbook:: config:: BookshelfConfig ;
6
7
use resolve_path:: PathResolveExt ;
7
8
8
9
use super :: command_prelude:: * ;
9
- use mdbook:: config:: ShelfConfig ;
10
10
use mdbook:: errors:: Result ;
11
11
use mdbook:: MDBook ;
12
12
13
- const SHELF_DIR : & str = "shelf " ;
13
+ const INDEX_BOOK_DIR : & str = "index " ;
14
14
const REPOS_DIR : & str = "repositories" ;
15
15
const INDEX_MD_FILE : & str = "index.md" ;
16
16
const INDEX_HTML_FILE : & str = "index.html" ;
@@ -28,7 +28,7 @@ struct BookContext {
28
28
authors : String ,
29
29
}
30
30
31
- struct ShelfContext {
31
+ struct BookshelfContext {
32
32
book_dir : PathBuf ,
33
33
source_dir : PathBuf ,
34
34
url_prefix : String ,
@@ -37,7 +37,7 @@ struct ShelfContext {
37
37
summary_file_name : PathBuf ,
38
38
}
39
39
40
- fn update_index (
40
+ fn update_index_with_book (
41
41
index_file : & mut File ,
42
42
summary_file : & mut File ,
43
43
shelf_source : & PathBuf ,
@@ -46,7 +46,7 @@ fn update_index(
46
46
) -> Result < ( ) > {
47
47
// Create post in index file
48
48
let book_link = format ! (
49
- "## [{title}](<{prefix}/{BOOKSHELF_DIR}/{BOOKS_DIR}/{title}/{INDEX_HTML_FILE}>)" ,
49
+ "### [{title}](<{prefix}/{BOOKSHELF_DIR}/{BOOKS_DIR}/{title}/{INDEX_HTML_FILE}>)" ,
50
50
title = context. title,
51
51
prefix = root_prefix
52
52
) ;
@@ -73,7 +73,6 @@ fn update_index(
73
73
"- [{title}](./{file_name})" ,
74
74
title = context. title
75
75
) ?;
76
- writeln ! ( summary_file) ?;
77
76
78
77
Ok ( ( ) )
79
78
}
@@ -101,8 +100,8 @@ fn process_book(path: &str, books_dir: &PathBuf, shelf_url: &str) -> Result<Book
101
100
Ok ( book_context)
102
101
}
103
102
104
- fn setup_shelf_book ( config : & ShelfConfig ) -> Result < ShelfContext > {
105
- let book_dir = format ! ( "{BOOKSHELF_DIR}/{SHELF_DIR }" ) ;
103
+ fn setup_bookshelf_book ( config : & BookshelfConfig ) -> Result < BookshelfContext > {
104
+ let book_dir = format ! ( "{BOOKSHELF_DIR}/{INDEX_BOOK_DIR }" ) ;
106
105
let book = MDBook :: init ( & book_dir) . build ( ) ?;
107
106
let build_dir = book. config . build . build_dir . to_str ( ) . unwrap_or_default ( ) ;
108
107
let url_prefix = if !config. root_url_prefix . is_empty ( ) {
@@ -120,7 +119,7 @@ fn setup_shelf_book(config: &ShelfConfig) -> Result<ShelfContext> {
120
119
let mut summary_file_name = book. source_dir ( ) ;
121
120
summary_file_name. push ( SUMMARY_MD_FILE ) ;
122
121
123
- Ok ( ShelfContext {
122
+ Ok ( BookshelfContext {
124
123
book_dir : book_dir. into ( ) ,
125
124
source_dir : book. source_dir ( ) ,
126
125
url_prefix,
@@ -131,51 +130,64 @@ fn setup_shelf_book(config: &ShelfConfig) -> Result<ShelfContext> {
131
130
}
132
131
133
132
pub fn execute ( _args : & ArgMatches ) -> Result < ( ) > {
134
- let mut file = File :: open ( "shelf.toml" ) ?;
135
- let mut contents = String :: new ( ) ;
136
- file. read_to_string ( & mut contents) ?;
137
- let shelf_config: ShelfConfig = toml:: from_str ( & contents) ?;
138
-
133
+ // Make sure everything is clean
139
134
let _ = std:: fs:: remove_dir_all ( BOOKSHELF_DIR ) ;
140
135
let _ = std:: fs:: remove_dir_all ( REPOS_DIR ) ;
141
136
142
- let shelf_context = setup_shelf_book ( & shelf_config) ?;
137
+ let mut file = File :: open ( "shelf.toml" ) ?;
138
+ let mut contents = String :: new ( ) ;
139
+ file. read_to_string ( & mut contents) ?;
140
+ let bookshelf_config: BookshelfConfig = toml:: from_str ( & contents) ?;
141
+ let shelf_context = setup_bookshelf_book ( & bookshelf_config) ?;
143
142
144
143
let mut index_file = File :: create ( shelf_context. index_file_name ) . unwrap ( ) ;
145
- writeln ! ( index_file, "# {title}" , title = shelf_config . title) ?;
144
+ writeln ! ( index_file, "# {title}" , title = bookshelf_config . title) ?;
146
145
writeln ! ( index_file) ?;
147
146
148
147
let mut summary_file = File :: create ( shelf_context. summary_file_name ) . unwrap ( ) ;
149
148
writeln ! ( summary_file, "# Summary" ) ?;
150
149
writeln ! (
151
150
summary_file,
152
- "- [{title}](./{INDEX_MD_FILE})" ,
153
- title = shelf_config . title
151
+ "[{title}](./{INDEX_MD_FILE})" ,
152
+ title = bookshelf_config . title
154
153
) ?;
155
154
156
155
let mut books_build_dir = std:: env:: current_dir ( ) ?;
157
156
books_build_dir. push ( BOOKSHELF_DIR ) ;
158
157
books_build_dir. push ( BOOKS_DIR ) ;
158
+ let books_build_dir = books_build_dir;
159
159
160
- for sb in & shelf_config. books {
161
- let book_path = if let Some ( url) = & sb. git_url {
162
- prepare_git ( sb, url)
163
- } else if let Some ( path) = & sb. path {
164
- Some ( path. to_owned ( ) )
165
- } else {
166
- warn ! ( "Neither path or git specified. Invalid book" ) ;
167
- None
168
- } ;
160
+ let shelves = if let Some ( shelves) = bookshelf_config. shelves {
161
+ shelves
162
+ } else if let Some ( shelf) = bookshelf_config. shelf_config {
163
+ vec ! [ shelf]
164
+ } else {
165
+ error ! ( "No shelves or default shelf found in config" ) ;
166
+ Vec :: new ( )
167
+ } ;
169
168
170
- if let Some ( path) = book_path {
171
- let update_context = process_book ( & path, & books_build_dir, & shelf_context. url ) ?;
172
- let _ = update_index (
173
- & mut index_file,
174
- & mut summary_file,
175
- & shelf_context. source_dir ,
176
- & shelf_context. url_prefix ,
177
- update_context,
178
- ) ?;
169
+ for shelf_config in shelves {
170
+ let _ = start_shelf ( & mut index_file, & mut summary_file, & shelf_config. title ) ;
171
+ for sb in & shelf_config. books {
172
+ let book_path = if let Some ( url) = & sb. git_url {
173
+ prepare_git ( sb, url)
174
+ } else if let Some ( path) = & sb. path {
175
+ Some ( path. to_owned ( ) )
176
+ } else {
177
+ warn ! ( "Neither path or git specified. Invalid book" ) ;
178
+ None
179
+ } ;
180
+
181
+ if let Some ( path) = book_path {
182
+ let update_context = process_book ( & path, & books_build_dir, & shelf_context. url ) ?;
183
+ let _ = update_index_with_book (
184
+ & mut index_file,
185
+ & mut summary_file,
186
+ & shelf_context. source_dir ,
187
+ & shelf_context. url_prefix ,
188
+ update_context,
189
+ ) ?;
190
+ }
179
191
}
180
192
}
181
193
@@ -185,6 +197,14 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
185
197
Ok ( ( ) )
186
198
}
187
199
200
+ fn start_shelf ( index_file : & mut File , summary_file : & mut File , title : & str ) -> Result < ( ) > {
201
+ writeln ! ( summary_file, "# {title}" ) ?;
202
+ writeln ! ( summary_file) ?;
203
+
204
+ writeln ! ( index_file, "## {title}" ) ?;
205
+ Ok ( ( ) )
206
+ }
207
+
188
208
fn prepare_git ( sb : & mdbook:: config:: ShelfBook , url : & String ) -> Option < String > {
189
209
println ! ( "{:?}" , sb) ;
190
210
@@ -252,20 +272,20 @@ git_url = "secondurl"
252
272
[[book]]
253
273
path = "../test_book"
254
274
"# ;
255
- let cfg: ShelfConfig = toml:: from_str ( & toml) . unwrap ( ) ;
275
+ let cfg: BookshelfConfig = toml:: from_str ( & toml) . unwrap ( ) ;
256
276
assert_eq ! ( cfg. root_url_prefix, "myprefix" ) ;
257
277
258
- let book = & cfg. books [ 0 ] ;
278
+ let book = & cfg. shelf_config . clone ( ) . unwrap ( ) . books [ 0 ] ;
259
279
assert_eq ! ( book. git_url. clone( ) . unwrap( ) , "firsturl" ) ;
260
280
assert_eq ! ( book. git_ref. clone( ) . unwrap( ) , "shelf" ) ;
261
281
assert_eq ! ( book. path. clone( ) . unwrap( ) , "guide" ) ;
262
282
263
- let book = & cfg. books [ 1 ] ;
283
+ let book = & cfg. shelf_config . clone ( ) . unwrap ( ) . books [ 1 ] ;
264
284
assert_eq ! ( book. git_url. clone( ) . unwrap( ) , "secondurl" ) ;
265
285
assert ! ( book. git_ref. is_none( ) ) ;
266
286
assert ! ( book. path. is_none( ) ) ;
267
287
268
- let book = & cfg. books [ 2 ] ;
288
+ let book = & cfg. shelf_config . clone ( ) . unwrap ( ) . books [ 2 ] ;
269
289
assert_eq ! ( book. path. clone( ) . unwrap( ) , "../test_book" ) ;
270
290
}
271
291
@@ -275,7 +295,7 @@ fn test_config_defaults() {
275
295
[[book]]
276
296
path = "../test_book"
277
297
"# ;
278
- let cfg: ShelfConfig = toml:: from_str ( & toml) . unwrap ( ) ;
298
+ let cfg: BookshelfConfig = toml:: from_str ( & toml) . unwrap ( ) ;
279
299
assert_eq ! ( cfg. root_url_prefix, "" . to_owned( ) ) ;
280
300
assert_eq ! ( cfg. title, "Bookshelf" . to_owned( ) ) ;
281
301
}
0 commit comments