@@ -22,58 +22,78 @@ pub fn make_subcommand() -> Command {
22
22
Command :: new ( "shelf" ) . about ( "Build a bookshelf from shelf.toml file" )
23
23
}
24
24
25
- fn process_book (
26
- path : & str ,
25
+ struct BookContext {
26
+ title : String ,
27
+ desc : String ,
28
+ authors : String ,
29
+ }
30
+
31
+ fn update_index (
27
32
index_file : & mut File ,
28
- summary : & mut File ,
33
+ summary_file : & mut File ,
29
34
shelf_source : & PathBuf ,
30
- shelf_url : & PathBuf ,
35
+ root_prefix : & str ,
36
+ context : BookContext ,
31
37
) -> Result < ( ) > {
32
- let book_dir = path. try_resolve ( ) ?;
33
- let book_dir = std:: fs:: canonicalize ( book_dir) ?;
34
- let mut book = MDBook :: load ( book_dir) ?;
35
-
36
- // Build book
37
- let title = book. config . book . title . clone ( ) . unwrap ( ) ;
38
- let mut path = current_dir ( ) ?;
39
- path. push ( BOOKSHELF_DIR ) ;
40
- path. push ( BOOKS_DIR ) ;
41
- path. push ( title) ;
42
- book. config . build . build_dir = path;
43
- // Create back reference to bookshelf
44
- book. config . book . shelf_url = Some ( shelf_url. to_path_buf ( ) ) ;
45
- book. build ( ) ?;
46
-
47
- let title = book. config . book . title . unwrap_or_default ( ) ;
48
- let book_link = format ! ( "## [{title}](<../../{BOOKS_DIR}/{title}/{INDEX_HTML_FILE}>)" ) ;
49
-
50
38
// Create post in index file
39
+ let book_link = format ! (
40
+ "## [{title}](<{prefix}/{BOOKSHELF_DIR}/{BOOKS_DIR}/{title}/{INDEX_HTML_FILE}>)" ,
41
+ title = context. title,
42
+ prefix = root_prefix
43
+ ) ;
51
44
writeln ! ( index_file, "{book_link}" ) ?;
52
45
writeln ! ( index_file) ?;
53
- let desc = book. config . book . description . unwrap_or_default ( ) ;
54
- writeln ! ( index_file, "{desc}" ) ?;
46
+ writeln ! ( index_file, "{desc}" , desc = context. desc) ?;
55
47
56
48
// Create a separate chapter file for the book
57
- let fixed_title = title. replace ( ' ' , "_" ) ;
49
+ let fixed_title = context . title . replace ( ' ' , "_" ) ;
58
50
let file_name = format ! ( "{fixed_title}.md" ) ;
59
51
let mut file_path = shelf_source. clone ( ) ;
60
52
file_path. push ( & file_name) ;
61
53
let mut bf = File :: create ( file_path) ?;
62
54
writeln ! ( bf, "{book_link}" ) ?;
63
55
writeln ! ( bf) ?;
64
- writeln ! ( bf, "{desc}" ) ?;
56
+ writeln ! ( bf, "{desc}" , desc = context . desc ) ?;
65
57
writeln ! ( bf) ?;
66
58
writeln ! ( bf) ?;
67
- let authors = book. config . book . authors . join ( ", " ) ;
68
- writeln ! ( bf, "*{authors}*" ) ?;
59
+ writeln ! ( bf, "*{authors}*" , authors = context. authors) ?;
69
60
70
61
// Add the chapter to the summary
71
- writeln ! ( summary, "- [{title}](./{file_name})" ) ?;
72
- writeln ! ( summary) ?;
62
+ writeln ! (
63
+ summary_file,
64
+ "- [{title}](./{file_name})" ,
65
+ title = context. title
66
+ ) ?;
67
+ writeln ! ( summary_file) ?;
73
68
74
69
Ok ( ( ) )
75
70
}
76
71
72
+ fn process_book ( path : & str , shelf_url : & PathBuf ) -> Result < BookContext > {
73
+ let book_dir = path. try_resolve ( ) ?;
74
+ let book_dir = std:: fs:: canonicalize ( book_dir) ?;
75
+ let mut book = MDBook :: load ( book_dir) ?;
76
+
77
+ // Build book
78
+ let title = book. config . book . title . clone ( ) . unwrap ( ) ;
79
+ let mut path = current_dir ( ) ?;
80
+ path. push ( BOOKSHELF_DIR ) ;
81
+ path. push ( BOOKS_DIR ) ;
82
+ path. push ( title) ;
83
+ book. config . build . build_dir = path;
84
+ // Create back reference to bookshelf
85
+ book. config . book . shelf_url = Some ( shelf_url. to_owned ( ) ) ;
86
+ book. build ( ) ?;
87
+
88
+ let book_context = BookContext {
89
+ title : book. config . book . title . unwrap_or_default ( ) ,
90
+ desc : book. config . book . description . unwrap_or_default ( ) ,
91
+ authors : book. config . book . authors . join ( ", " ) ,
92
+ } ;
93
+
94
+ Ok ( book_context)
95
+ }
96
+
77
97
pub fn execute ( _args : & ArgMatches ) -> Result < ( ) > {
78
98
let mut file = File :: open ( "shelf.toml" ) ?;
79
99
let mut contents = String :: new ( ) ;
@@ -107,9 +127,9 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
107
127
108
128
let mut summary_file_name = shelf_book. source_dir ( ) ;
109
129
summary_file_name. push ( "SUMMARY.md" ) ;
110
- let mut summary = File :: create ( summary_file_name) . unwrap ( ) ;
111
- writeln ! ( summary , "# Summary" ) ?;
112
- writeln ! ( summary , "- [Index](./{INDEX_MD_FILE})" ) ?;
130
+ let mut summary_file = File :: create ( summary_file_name) . unwrap ( ) ;
131
+ writeln ! ( summary_file , "# Summary" ) ?;
132
+ writeln ! ( summary_file , "- [Index](./{INDEX_MD_FILE})" ) ?;
113
133
114
134
for sb in & shelf_config. books {
115
135
let book_path = if let Some ( url) = & sb. git_url {
@@ -122,13 +142,14 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
122
142
} ;
123
143
124
144
if let Some ( path) = book_path {
125
- process_book (
126
- & path ,
145
+ let update_context = process_book ( & path , & shelf_url ) ? ;
146
+ let _ = update_index (
127
147
& mut index_file,
128
- & mut summary ,
148
+ & mut summary_file ,
129
149
& shelf_source,
130
- & shelf_url,
131
- ) ?
150
+ & shelf_url_prefix,
151
+ update_context,
152
+ ) ?;
132
153
}
133
154
}
134
155
0 commit comments