Skip to content

Commit

Permalink
fix issues from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu David authored and ehuss committed May 10, 2020
1 parent d605938 commit 43008ef
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 110 deletions.
7 changes: 4 additions & 3 deletions src/book/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,15 @@ fn load_summary_item<P: AsRef<Path> + Clone>(
}
}

fn load_chapter<P: AsRef<Path> + Clone>(
fn load_chapter<P: AsRef<Path>>(
link: &Link,
src_dir: P,
parent_names: Vec<String>,
) -> Result<Chapter> {
let src_dir = src_dir.as_ref();

let mut ch = if let Some(ref link_location) = link.location {
debug!("Loading {} ({})", link.name, link_location.display());
let src_dir = src_dir.as_ref();

let location = if link_location.is_absolute() {
link_location.clone()
Expand Down Expand Up @@ -276,7 +277,7 @@ fn load_chapter<P: AsRef<Path> + Clone>(
let sub_items = link
.nested_items
.iter()
.map(|i| load_summary_item(i, src_dir.clone(), sub_item_parents.clone()))
.map(|i| load_summary_item(i, src_dir, sub_item_parents.clone()))
.collect::<Result<Vec<_>>>()?;

ch.sub_items = sub_items;
Expand Down
61 changes: 31 additions & 30 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,40 +251,41 @@ impl MDBook {

for item in book.iter() {
if let BookItem::Chapter(ref ch) = *item {
if let Some(ref chapter_path) = ch.path {
if !chapter_path.as_os_str().is_empty() {
let path = self.source_dir().join(&chapter_path);
info!("Testing file: {:?}", path);

// write preprocessed file to tempdir
let path = temp_dir.path().join(&chapter_path);
let mut tmpf = utils::fs::create_file(&path)?;
tmpf.write_all(ch.content.as_bytes())?;

let mut cmd = Command::new("rustdoc");
cmd.arg(&path).arg("--test").args(&library_args);

if let Some(edition) = self.config.rust.edition {
match edition {
RustEdition::E2015 => {
cmd.args(&["--edition", "2015"]);
}
RustEdition::E2018 => {
cmd.args(&["--edition", "2018"]);
}
}
let chapter_path = match ch.path {
Some(ref path) if !path.as_os_str().is_empty() => path,
_ => continue,
};

let path = self.source_dir().join(&chapter_path);
info!("Testing file: {:?}", path);

// write preprocessed file to tempdir
let path = temp_dir.path().join(&chapter_path);
let mut tmpf = utils::fs::create_file(&path)?;
tmpf.write_all(ch.content.as_bytes())?;

let mut cmd = Command::new("rustdoc");
cmd.arg(&path).arg("--test").args(&library_args);

if let Some(edition) = self.config.rust.edition {
match edition {
RustEdition::E2015 => {
cmd.args(&["--edition", "2015"]);
}

let output = cmd.output()?;

if !output.status.success() {
bail!(ErrorKind::Subprocess(
"Rustdoc returned an error".to_string(),
output
));
RustEdition::E2018 => {
cmd.args(&["--edition", "2018"]);
}
}
}

let output = cmd.output()?;

if !output.status.success() {
bail!(ErrorKind::Subprocess(
"Rustdoc returned an error".to_string(),
output
));
}
}
}
Ok(())
Expand Down
138 changes: 68 additions & 70 deletions src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,84 +30,82 @@ impl HtmlHandlebars {
print_content: &mut String,
) -> Result<()> {
// FIXME: This should be made DRY-er and rely less on mutable state
if let BookItem::Chapter(ref ch) = *item {
if let Some(ref path) = ch.path {
let content = ch.content.clone();
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);

let fixed_content = utils::render_markdown_with_path(
&ch.content,
ctx.html_config.curly_quotes,
Some(&path),
);
print_content.push_str(&fixed_content);

// Update the context with data for this file
let ctx_path = path
.to_str()
.chain_err(|| "Could not convert path to str")?;
let filepath = Path::new(&ctx_path).with_extension("html");
let (ch, path) = match item {
BookItem::Chapter(ch) if !ch.is_draft_chapter() => (ch, ch.path.as_ref().unwrap()),
_ => return Ok(()),
};

// "print.html" is used for the print page.
if path == Path::new("print.md") {
bail!(ErrorKind::ReservedFilenameError(path.clone()));
};
let content = ch.content.clone();
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);

// Non-lexical lifetimes needed :'(
let title: String;
{
let book_title = ctx
.data
.get("book_title")
.and_then(serde_json::Value::as_str)
.unwrap_or("");

title = match book_title {
"" => ch.name.clone(),
_ => ch.name.clone() + " - " + book_title,
}
}
let fixed_content = utils::render_markdown_with_path(
&ch.content,
ctx.html_config.curly_quotes,
Some(&path),
);
print_content.push_str(&fixed_content);

ctx.data.insert("path".to_owned(), json!(path));
ctx.data.insert("content".to_owned(), json!(content));
ctx.data.insert("chapter_title".to_owned(), json!(ch.name));
ctx.data.insert("title".to_owned(), json!(title));
ctx.data.insert(
"path_to_root".to_owned(),
json!(utils::fs::path_to_root(&path)),
);
if let Some(ref section) = ch.number {
ctx.data
.insert("section".to_owned(), json!(section.to_string()));
}
// Update the context with data for this file
let ctx_path = path
.to_str()
.chain_err(|| "Could not convert path to str")?;
let filepath = Path::new(&ctx_path).with_extension("html");

// Render the handlebars template with the data
debug!("Render template");
let rendered = ctx.handlebars.render("index", &ctx.data)?;

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

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

if ctx.is_index {
ctx.data.insert("path".to_owned(), json!("index.md"));
ctx.data.insert("path_to_root".to_owned(), json!(""));
ctx.data.insert("is_index".to_owned(), json!("true"));
let rendered_index = ctx.handlebars.render("index", &ctx.data)?;
let rendered_index =
self.post_process(rendered_index, &ctx.html_config.playpen, ctx.edition);
debug!("Creating index.html from {}", ctx_path);
utils::fs::write_file(
&ctx.destination,
"index.html",
rendered_index.as_bytes(),
)?;
}
// "print.html" is used for the print page.
if path == Path::new("print.md") {
bail!(ErrorKind::ReservedFilenameError(path.clone()));
};

// Non-lexical lifetimes needed :'(
let title: String;
{
let book_title = ctx
.data
.get("book_title")
.and_then(serde_json::Value::as_str)
.unwrap_or("");

title = match book_title {
"" => ch.name.clone(),
_ => ch.name.clone() + " - " + book_title,
}
}

ctx.data.insert("path".to_owned(), json!(path));
ctx.data.insert("content".to_owned(), json!(content));
ctx.data.insert("chapter_title".to_owned(), json!(ch.name));
ctx.data.insert("title".to_owned(), json!(title));
ctx.data.insert(
"path_to_root".to_owned(),
json!(utils::fs::path_to_root(&path)),
);
if let Some(ref section) = ch.number {
ctx.data
.insert("section".to_owned(), json!(section.to_string()));
}

// Render the handlebars template with the data
debug!("Render template");
let rendered = ctx.handlebars.render("index", &ctx.data)?;

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

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

if ctx.is_index {
ctx.data.insert("path".to_owned(), json!("index.md"));
ctx.data.insert("path_to_root".to_owned(), json!(""));
ctx.data.insert("is_index".to_owned(), json!("true"));
let rendered_index = ctx.handlebars.render("index", &ctx.data)?;
let rendered_index =
self.post_process(rendered_index, &ctx.html_config.playpen, ctx.edition);
debug!("Creating index.html from {}", ctx_path);
utils::fs::write_file(&ctx.destination, "index.html", rendered_index.as_bytes())?;
}

Ok(())
}

Expand Down
8 changes: 1 addition & 7 deletions src/renderer/html_handlebars/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,7 @@ fn render_item(
item: &BookItem,
) -> Result<()> {
let chapter = match *item {
BookItem::Chapter(ref ch) => {
if let Some(_) = ch.path {
ch
} else {
return Ok(());
}
}
BookItem::Chapter(ref ch) if !ch.is_draft_chapter() => ch,
_ => return Ok(()),
};

Expand Down

0 comments on commit 43008ef

Please sign in to comment.