Skip to content

Commit b7aa78c

Browse files
Minor refactoring
1 parent 2568986 commit b7aa78c

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl HtmlHandlebars {
2828
}
2929

3030
fn render_item(&self, item: &BookItem, book: &MDBook, data: &mut serde_json::Map<String, serde_json::Value>,
31-
print_content: &mut String, handlebars: &mut Handlebars, index: &mut bool)
31+
print_content: &mut String, handlebars: &mut Handlebars, index: &mut bool, destination: &Path)
3232
-> Result<(), Box<Error>> {
3333
match *item {
3434
BookItem::Chapter(_, ref ch) |
@@ -66,15 +66,10 @@ impl HtmlHandlebars {
6666
// Render the handlebars template with the data
6767
debug!("[*]: Render template");
6868
let rendered = handlebars.render("index", &data)?;
69+
let rendered = self.post_processing(rendered);
6970

7071
let filename = Path::new(&ch.path).with_extension("html");
7172

72-
// Do several kinds of post-processing
73-
let rendered = build_header_links(rendered, filename.to_str().unwrap_or(""));
74-
let rendered = fix_anchor_links(rendered, filename.to_str().unwrap_or(""));
75-
let rendered = fix_code_blocks(rendered);
76-
let rendered = add_playpen_pre(rendered);
77-
7873
// Write to file
7974
info!("[*] Creating {:?} ✓", filename.display());
8075
book.write_file(filename, &rendered.into_bytes())?;
@@ -86,9 +81,7 @@ impl HtmlHandlebars {
8681
let mut content = String::new();
8782

8883
let _source = File::open(
89-
book.get_destination()
90-
.expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (3)")
91-
.join(&ch.path.with_extension("html"))
84+
destination.join(&ch.path.with_extension("html"))
9285
)?.read_to_string(&mut content);
9386

9487
// This could cause a problem when someone displays
@@ -148,7 +141,6 @@ impl HtmlHandlebars {
148141
Ok(())
149142
}
150143

151-
152144
fn write_custom_file(&self, custom_file: &Path, book: &MDBook) -> Result<(), Box<Error>> {
153145
let mut data = Vec::new();
154146
let mut f = File::open(custom_file)?;
@@ -200,10 +192,8 @@ impl Renderer for HtmlHandlebars {
200192
debug!("[fn]: render");
201193
let mut handlebars = Handlebars::new();
202194

203-
// Load theme
204195
let theme = theme::Theme::new(book.get_theme_path());
205196

206-
// Register template
207197
debug!("[*]: Register handlebars template");
208198
handlebars
209199
.register_template_string("index", String::from_utf8(theme.index.clone())?)?;
@@ -214,20 +204,20 @@ impl Renderer for HtmlHandlebars {
214204
let mut data = make_data(book)?;
215205

216206
// Print version
217-
let mut print_content: String = String::new();
207+
let mut print_content = String::new();
218208

219-
debug!("[*]: Check if destination directory exists");
220209
let destination = book.get_destination()
221210
.expect("If the HTML renderer is called, one would assume the HtmlConfig is set... (2)");
222211

212+
debug!("[*]: Check if destination directory exists");
223213
if fs::create_dir_all(&destination).is_err() {
224214
return Err(Box::new(io::Error::new(io::ErrorKind::Other,
225215
"Unexpected error when constructing destination path")));
226216
}
227217

228218
let mut index = true;
229219
for item in book.iter() {
230-
self.render_item(item, book, &mut data, &mut print_content, &mut handlebars, &mut index)?;
220+
self.render_item(item, book, &mut data, &mut print_content, &mut handlebars, &mut index, &destination)?;
231221
}
232222

233223
// Print version

0 commit comments

Comments
 (0)