Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix relative paths in index.html #756

Merged
merged 1 commit into from
Aug 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 16 additions & 35 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use book::{Book, BookItem, Chapter};
use book::{Book, BookItem};
use config::{Config, HtmlConfig, Playpen};
use errors::*;
use renderer::html_handlebars::helpers;
Expand All @@ -8,8 +8,7 @@ use utils;

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::Read;
use std::fs;
use std::path::{Path, PathBuf};

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

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

if ctx.is_index {
self.render_index(ch, &ctx.destination)?;
ctx.data.insert("path".to_owned(), json!("index.html"));
ctx.data.insert("path_to_root".to_owned(), json!(""));
let rendered_index = ctx.handlebars.render("index", &ctx.data)?;
let rendered_index =
self.post_process(rendered_index, &ctx.html_config.playpen);
debug!("Creating index.html from {}", path);
utils::fs::write_file(
&ctx.destination,
"index.html",
rendered_index.as_bytes(),
)?;
}
}
_ => {}
Expand All @@ -87,34 +96,6 @@ impl HtmlHandlebars {
Ok(())
}

/// Create an index.html from the first element in SUMMARY.md
fn render_index(&self, ch: &Chapter, destination: &Path) -> Result<()> {
debug!("index.html");

let mut content = String::new();

File::open(destination.join(&ch.path.with_extension("html")))?
.read_to_string(&mut content)?;

// This could cause a problem when someone displays
// code containing <base href=...>
// on the front page, however this case should be very very rare...
content = content
.lines()
.filter(|line| !line.contains("<base href="))
.collect::<Vec<&str>>()
.join("\n");

utils::fs::write_file(destination, "index.html", content.as_bytes())?;

debug!(
"Creating index.html from {} ✓",
destination.join(&ch.path.with_extension("html")).display()
);

Ok(())
}

#[cfg_attr(feature = "cargo-clippy", allow(let_and_return))]
fn post_process(&self, rendered: String, playpen_config: &Playpen) -> String {
let rendered = build_header_links(&rendered);
Expand Down Expand Up @@ -363,7 +344,7 @@ impl Renderer for HtmlHandlebars {

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

utils::fs::write_file(&destination, "print.html", &rendered.into_bytes())?;
utils::fs::write_file(&destination, "print.html", rendered.as_bytes())?;
debug!("Creating print.html ✓");

debug!("Copy static files");
Expand Down