@@ -22,58 +22,78 @@ pub fn make_subcommand() -> Command {
2222 Command :: new ( "shelf" ) . about ( "Build a bookshelf from shelf.toml file" )
2323}
2424
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 (
2732 index_file : & mut File ,
28- summary : & mut File ,
33+ summary_file : & mut File ,
2934 shelf_source : & PathBuf ,
30- shelf_url : & PathBuf ,
35+ root_prefix : & str ,
36+ context : BookContext ,
3137) -> 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-
5038 // 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+ ) ;
5144 writeln ! ( index_file, "{book_link}" ) ?;
5245 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) ?;
5547
5648 // Create a separate chapter file for the book
57- let fixed_title = title. replace ( ' ' , "_" ) ;
49+ let fixed_title = context . title . replace ( ' ' , "_" ) ;
5850 let file_name = format ! ( "{fixed_title}.md" ) ;
5951 let mut file_path = shelf_source. clone ( ) ;
6052 file_path. push ( & file_name) ;
6153 let mut bf = File :: create ( file_path) ?;
6254 writeln ! ( bf, "{book_link}" ) ?;
6355 writeln ! ( bf) ?;
64- writeln ! ( bf, "{desc}" ) ?;
56+ writeln ! ( bf, "{desc}" , desc = context . desc ) ?;
6557 writeln ! ( bf) ?;
6658 writeln ! ( bf) ?;
67- let authors = book. config . book . authors . join ( ", " ) ;
68- writeln ! ( bf, "*{authors}*" ) ?;
59+ writeln ! ( bf, "*{authors}*" , authors = context. authors) ?;
6960
7061 // 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) ?;
7368
7469 Ok ( ( ) )
7570}
7671
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+
7797pub fn execute ( _args : & ArgMatches ) -> Result < ( ) > {
7898 let mut file = File :: open ( "shelf.toml" ) ?;
7999 let mut contents = String :: new ( ) ;
@@ -107,9 +127,9 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
107127
108128 let mut summary_file_name = shelf_book. source_dir ( ) ;
109129 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})" ) ?;
113133
114134 for sb in & shelf_config. books {
115135 let book_path = if let Some ( url) = & sb. git_url {
@@ -122,13 +142,14 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
122142 } ;
123143
124144 if let Some ( path) = book_path {
125- process_book (
126- & path ,
145+ let update_context = process_book ( & path , & shelf_url ) ? ;
146+ let _ = update_index (
127147 & mut index_file,
128- & mut summary ,
148+ & mut summary_file ,
129149 & shelf_source,
130- & shelf_url,
131- ) ?
150+ & shelf_url_prefix,
151+ update_context,
152+ ) ?;
132153 }
133154 }
134155
0 commit comments