Skip to content

Commit 67fde37

Browse files
authored
Merge pull request #756 from mattico/test-dir
Fix relative paths in index.html
2 parents b5fd170 + b2eb1ac commit 67fde37

File tree

1 file changed

+16
-35
lines changed

1 file changed

+16
-35
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use book::{Book, BookItem, Chapter};
1+
use book::{Book, BookItem};
22
use config::{Config, HtmlConfig, Playpen};
33
use errors::*;
44
use renderer::html_handlebars::helpers;
@@ -8,8 +8,7 @@ use utils;
88

99
use std::collections::BTreeMap;
1010
use std::collections::HashMap;
11-
use std::fs::{self, File};
12-
use std::io::Read;
11+
use std::fs;
1312
use std::path::{Path, PathBuf};
1413

1514
use handlebars::Handlebars;
@@ -74,11 +73,21 @@ impl HtmlHandlebars {
7473
let rendered = self.post_process(rendered, &ctx.html_config.playpen);
7574

7675
// Write to file
77-
debug!("Creating {}", filepath.display());
78-
utils::fs::write_file(&ctx.destination, &filepath, &rendered.into_bytes())?;
76+
debug!("Creating {}", filepath.display());
77+
utils::fs::write_file(&ctx.destination, &filepath, rendered.as_bytes())?;
7978

8079
if ctx.is_index {
81-
self.render_index(ch, &ctx.destination)?;
80+
ctx.data.insert("path".to_owned(), json!("index.html"));
81+
ctx.data.insert("path_to_root".to_owned(), json!(""));
82+
let rendered_index = ctx.handlebars.render("index", &ctx.data)?;
83+
let rendered_index =
84+
self.post_process(rendered_index, &ctx.html_config.playpen);
85+
debug!("Creating index.html from {}", path);
86+
utils::fs::write_file(
87+
&ctx.destination,
88+
"index.html",
89+
rendered_index.as_bytes(),
90+
)?;
8291
}
8392
}
8493
_ => {}
@@ -87,34 +96,6 @@ impl HtmlHandlebars {
8796
Ok(())
8897
}
8998

90-
/// Create an index.html from the first element in SUMMARY.md
91-
fn render_index(&self, ch: &Chapter, destination: &Path) -> Result<()> {
92-
debug!("index.html");
93-
94-
let mut content = String::new();
95-
96-
File::open(destination.join(&ch.path.with_extension("html")))?
97-
.read_to_string(&mut content)?;
98-
99-
// This could cause a problem when someone displays
100-
// code containing <base href=...>
101-
// on the front page, however this case should be very very rare...
102-
content = content
103-
.lines()
104-
.filter(|line| !line.contains("<base href="))
105-
.collect::<Vec<&str>>()
106-
.join("\n");
107-
108-
utils::fs::write_file(destination, "index.html", content.as_bytes())?;
109-
110-
debug!(
111-
"Creating index.html from {} ✓",
112-
destination.join(&ch.path.with_extension("html")).display()
113-
);
114-
115-
Ok(())
116-
}
117-
11899
#[cfg_attr(feature = "cargo-clippy", allow(let_and_return))]
119100
fn post_process(&self, rendered: String, playpen_config: &Playpen) -> String {
120101
let rendered = build_header_links(&rendered);
@@ -363,7 +344,7 @@ impl Renderer for HtmlHandlebars {
363344

364345
let rendered = self.post_process(rendered, &html_config.playpen);
365346

366-
utils::fs::write_file(&destination, "print.html", &rendered.into_bytes())?;
347+
utils::fs::write_file(&destination, "print.html", rendered.as_bytes())?;
367348
debug!("Creating print.html ✓");
368349

369350
debug!("Copy static files");

0 commit comments

Comments
 (0)