@@ -2,7 +2,8 @@ use renderer::html_handlebars::helpers;
2
2
use renderer:: Renderer ;
3
3
use book:: MDBook ;
4
4
use book:: bookitem:: BookItem ;
5
- use { utils, theme} ;
5
+ use utils;
6
+ use theme:: { self , Theme } ;
6
7
use regex:: { Regex , Captures } ;
7
8
8
9
use std:: ascii:: AsciiExt ;
@@ -115,8 +116,75 @@ impl HtmlHandlebars {
115
116
116
117
Ok ( ( ) )
117
118
}
119
+
120
+ fn post_processing ( & self , rendered : String ) -> String {
121
+ let rendered = build_header_links ( rendered, "print.html" ) ;
122
+ let rendered = fix_anchor_links ( rendered, "print.html" ) ;
123
+ let rendered = fix_code_blocks ( rendered) ;
124
+ let rendered = add_playpen_pre ( rendered) ;
125
+
126
+ rendered
127
+ }
128
+
129
+ fn copy_static_files ( & self , book : & MDBook , theme : & Theme ) -> Result < ( ) , Box < Error > > {
130
+ book. write_file ( "book.js" , & theme. js ) ?;
131
+ book. write_file ( "book.css" , & theme. css ) ?;
132
+ book. write_file ( "favicon.png" , & theme. favicon ) ?;
133
+ book. write_file ( "jquery.js" , & theme. jquery ) ?;
134
+ book. write_file ( "highlight.css" , & theme. highlight_css ) ?;
135
+ book. write_file ( "tomorrow-night.css" , & theme. tomorrow_night_css ) ?;
136
+ book. write_file ( "ayu-highlight.css" , & theme. ayu_highlight_css ) ?;
137
+ book. write_file ( "highlight.js" , & theme. highlight_js ) ?;
138
+ book. write_file ( "clipboard.min.js" , & theme. clipboard_js ) ?;
139
+ book. write_file ( "store.js" , & theme. store_js ) ?;
140
+ book. write_file ( "_FontAwesome/css/font-awesome.css" , theme:: FONT_AWESOME ) ?;
141
+ book. write_file ( "_FontAwesome/fonts/fontawesome-webfont.eot" , theme:: FONT_AWESOME_EOT ) ?;
142
+ book. write_file ( "_FontAwesome/fonts/fontawesome-webfont.svg" , theme:: FONT_AWESOME_SVG ) ?;
143
+ book. write_file ( "_FontAwesome/fonts/fontawesome-webfont.ttf" , theme:: FONT_AWESOME_TTF ) ?;
144
+ book. write_file ( "_FontAwesome/fonts/fontawesome-webfont.woff" , theme:: FONT_AWESOME_WOFF ) ?;
145
+ book. write_file ( "_FontAwesome/fonts/fontawesome-webfont.woff2" , theme:: FONT_AWESOME_WOFF2 ) ?;
146
+ book. write_file ( "_FontAwesome/fonts/FontAwesome.ttf" , theme:: FONT_AWESOME_TTF ) ?;
147
+
148
+ Ok ( ( ) )
149
+ }
150
+
151
+
152
+ fn write_custom_file ( & self , custom_file : & Path , book : & MDBook ) -> Result < ( ) , Box < Error > > {
153
+ let mut data = Vec :: new ( ) ;
154
+ let mut f = File :: open ( custom_file) ?;
155
+ f. read_to_end ( & mut data) ?;
156
+
157
+ let name = match custom_file. strip_prefix ( book. get_root ( ) ) {
158
+ Ok ( p) => p. to_str ( ) . expect ( "Could not convert to str" ) ,
159
+ Err ( _) => {
160
+ custom_file
161
+ . file_name ( )
162
+ . expect ( "File has a file name" )
163
+ . to_str ( )
164
+ . expect ( "Could not convert to str" )
165
+ } ,
166
+ } ;
167
+
168
+ book. write_file ( name, & data) ?;
169
+
170
+ Ok ( ( ) )
171
+ }
172
+
173
+ /// Update the context with data for this file
174
+ fn configure_print_version ( & self , data : & mut serde_json:: Map < String , serde_json:: Value > , print_content : & str ) {
175
+ data. insert ( "path" . to_owned ( ) , json ! ( "print.md" ) ) ;
176
+ data. insert ( "content" . to_owned ( ) , json ! ( print_content) ) ;
177
+ data. insert ( "path_to_root" . to_owned ( ) , json ! ( utils:: fs:: path_to_root( Path :: new( "print.md" ) ) ) ) ;
178
+ }
179
+
180
+ fn register_hbs_helpers ( & self , handlebars : & mut Handlebars ) {
181
+ handlebars. register_helper ( "toc" , Box :: new ( helpers:: toc:: RenderToc ) ) ;
182
+ handlebars. register_helper ( "previous" , Box :: new ( helpers:: navigation:: previous) ) ;
183
+ handlebars. register_helper ( "next" , Box :: new ( helpers:: navigation:: next) ) ;
184
+ }
118
185
}
119
186
187
+
120
188
impl Renderer for HtmlHandlebars {
121
189
fn render ( & self , book : & MDBook ) -> Result < ( ) , Box < Error > > {
122
190
debug ! ( "[fn]: render" ) ;
@@ -128,101 +196,55 @@ impl Renderer for HtmlHandlebars {
128
196
// Register template
129
197
debug ! ( "[*]: Register handlebars template" ) ;
130
198
handlebars
131
- . register_template_string ( "index" , String :: from_utf8 ( theme. index ) ?) ?;
199
+ . register_template_string ( "index" , String :: from_utf8 ( theme. index . clone ( ) ) ?) ?;
132
200
133
201
// Register helpers
134
202
debug ! ( "[*]: Register handlebars helpers" ) ;
135
- handlebars. register_helper ( "toc" , Box :: new ( helpers:: toc:: RenderToc ) ) ;
136
- handlebars. register_helper ( "previous" , Box :: new ( helpers:: navigation:: previous) ) ;
137
- handlebars. register_helper ( "next" , Box :: new ( helpers:: navigation:: next) ) ;
203
+ self . register_hbs_helpers ( & mut handlebars) ;
138
204
139
205
let mut data = make_data ( book) ?;
140
206
141
207
// Print version
142
208
let mut print_content: String = String :: new ( ) ;
143
209
144
- // Check if dest directory exists
145
210
debug ! ( "[*]: Check if destination directory exists" ) ;
146
- if fs:: create_dir_all ( book. get_destination ( ) . expect ( "If the HTML renderer is called, one would assume the HtmlConfig is set... (2)" ) ) . is_err ( ) {
211
+ let destination = book. get_destination ( )
212
+ . expect ( "If the HTML renderer is called, one would assume the HtmlConfig is set... (2)" ) ;
213
+
214
+ if fs:: create_dir_all ( & destination) . is_err ( ) {
147
215
return Err ( Box :: new ( io:: Error :: new ( io:: ErrorKind :: Other ,
148
216
"Unexpected error when constructing destination path" ) ) ) ;
149
217
}
150
218
151
- // Render a file for every entry in the book
152
219
let mut index = true ;
153
220
for item in book. iter ( ) {
154
221
self . render_item ( item, book, & mut data, & mut print_content, & mut handlebars, & mut index) ?;
155
222
}
156
223
157
224
// Print version
158
-
159
- // Update the context with data for this file
160
- data. insert ( "path" . to_owned ( ) , json ! ( "print.md" ) ) ;
161
- data. insert ( "content" . to_owned ( ) , json ! ( print_content) ) ;
162
- data. insert ( "path_to_root" . to_owned ( ) , json ! ( utils:: fs:: path_to_root( Path :: new( "print.md" ) ) ) ) ;
225
+ self . configure_print_version ( & mut data, & print_content) ;
163
226
164
227
// Render the handlebars template with the data
165
228
debug ! ( "[*]: Render template" ) ;
166
229
167
230
let rendered = handlebars. render ( "index" , & data) ?;
168
-
169
- // do several kinds of post-processing
170
- let rendered = build_header_links ( rendered, "print.html" ) ;
171
- let rendered = fix_anchor_links ( rendered, "print.html" ) ;
172
- let rendered = fix_code_blocks ( rendered) ;
173
- let rendered = add_playpen_pre ( rendered) ;
231
+ let rendered = self . post_processing ( rendered) ;
174
232
175
233
book. write_file ( Path :: new ( "print" ) . with_extension ( "html" ) , & rendered. into_bytes ( ) ) ?;
176
234
info ! ( "[*] Creating print.html ✓" ) ;
177
235
178
236
// Copy static files (js, css, images, ...)
179
-
180
237
debug ! ( "[*] Copy static files" ) ;
181
- book. write_file ( "book.js" , & theme. js ) ?;
182
- book. write_file ( "book.css" , & theme. css ) ?;
183
- book. write_file ( "favicon.png" , & theme. favicon ) ?;
184
- book. write_file ( "jquery.js" , & theme. jquery ) ?;
185
- book. write_file ( "highlight.css" , & theme. highlight_css ) ?;
186
- book. write_file ( "tomorrow-night.css" , & theme. tomorrow_night_css ) ?;
187
- book. write_file ( "ayu-highlight.css" , & theme. ayu_highlight_css ) ?;
188
- book. write_file ( "highlight.js" , & theme. highlight_js ) ?;
189
- book. write_file ( "clipboard.min.js" , & theme. clipboard_js ) ?;
190
- book. write_file ( "store.js" , & theme. store_js ) ?;
191
- book. write_file ( "_FontAwesome/css/font-awesome.css" , theme:: FONT_AWESOME ) ?;
192
- book. write_file ( "_FontAwesome/fonts/fontawesome-webfont.eot" , theme:: FONT_AWESOME_EOT ) ?;
193
- book. write_file ( "_FontAwesome/fonts/fontawesome-webfont.svg" , theme:: FONT_AWESOME_SVG ) ?;
194
- book. write_file ( "_FontAwesome/fonts/fontawesome-webfont.ttf" , theme:: FONT_AWESOME_TTF ) ?;
195
- book. write_file ( "_FontAwesome/fonts/fontawesome-webfont.woff" , theme:: FONT_AWESOME_WOFF ) ?;
196
- book. write_file ( "_FontAwesome/fonts/fontawesome-webfont.woff2" , theme:: FONT_AWESOME_WOFF2 ) ?;
197
- book. write_file ( "_FontAwesome/fonts/FontAwesome.ttf" , theme:: FONT_AWESOME_TTF ) ?;
238
+ self . copy_static_files ( book, & theme) ?;
198
239
199
240
for custom_file in book. get_additional_css ( )
200
241
. iter ( )
201
242
. chain ( book. get_additional_js ( ) . iter ( ) ) {
202
- let mut data = Vec :: new ( ) ;
203
- let mut f = File :: open ( custom_file) ?;
204
- f. read_to_end ( & mut data) ?;
205
-
206
- let name = match custom_file. strip_prefix ( book. get_root ( ) ) {
207
- Ok ( p) => p. to_str ( ) . expect ( "Could not convert to str" ) ,
208
- Err ( _) => {
209
- custom_file
210
- . file_name ( )
211
- . expect ( "File has a file name" )
212
- . to_str ( )
213
- . expect ( "Could not convert to str" )
214
- } ,
215
- } ;
216
-
217
- book. write_file ( name, & data) ?;
243
+ self . write_custom_file ( custom_file, book) ?;
218
244
}
219
245
220
246
// Copy all remaining files
221
- utils:: fs:: copy_files_except_ext (
222
- book. get_source ( ) ,
223
- book. get_destination ( )
224
- . expect ( "If the HTML renderer is called, one would assume the HtmlConfig is set... (5)" ) , true , & [ "md" ]
225
- ) ?;
247
+ utils:: fs:: copy_files_except_ext ( book. get_source ( ) , & destination, true , & [ "md" ] ) ?;
226
248
227
249
Ok ( ( ) )
228
250
}
0 commit comments