Skip to content

Commit 43008ef

Browse files
Mathieu Davidehuss
Mathieu David
authored andcommitted
fix issues from code review
1 parent d605938 commit 43008ef

File tree

4 files changed

+104
-110
lines changed

4 files changed

+104
-110
lines changed

src/book/book.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,15 @@ fn load_summary_item<P: AsRef<Path> + Clone>(
237237
}
238238
}
239239

240-
fn load_chapter<P: AsRef<Path> + Clone>(
240+
fn load_chapter<P: AsRef<Path>>(
241241
link: &Link,
242242
src_dir: P,
243243
parent_names: Vec<String>,
244244
) -> Result<Chapter> {
245+
let src_dir = src_dir.as_ref();
246+
245247
let mut ch = if let Some(ref link_location) = link.location {
246248
debug!("Loading {} ({})", link.name, link_location.display());
247-
let src_dir = src_dir.as_ref();
248249

249250
let location = if link_location.is_absolute() {
250251
link_location.clone()
@@ -276,7 +277,7 @@ fn load_chapter<P: AsRef<Path> + Clone>(
276277
let sub_items = link
277278
.nested_items
278279
.iter()
279-
.map(|i| load_summary_item(i, src_dir.clone(), sub_item_parents.clone()))
280+
.map(|i| load_summary_item(i, src_dir, sub_item_parents.clone()))
280281
.collect::<Result<Vec<_>>>()?;
281282

282283
ch.sub_items = sub_items;

src/book/mod.rs

+31-30
Original file line numberDiff line numberDiff line change
@@ -251,40 +251,41 @@ impl MDBook {
251251

252252
for item in book.iter() {
253253
if let BookItem::Chapter(ref ch) = *item {
254-
if let Some(ref chapter_path) = ch.path {
255-
if !chapter_path.as_os_str().is_empty() {
256-
let path = self.source_dir().join(&chapter_path);
257-
info!("Testing file: {:?}", path);
258-
259-
// write preprocessed file to tempdir
260-
let path = temp_dir.path().join(&chapter_path);
261-
let mut tmpf = utils::fs::create_file(&path)?;
262-
tmpf.write_all(ch.content.as_bytes())?;
263-
264-
let mut cmd = Command::new("rustdoc");
265-
cmd.arg(&path).arg("--test").args(&library_args);
266-
267-
if let Some(edition) = self.config.rust.edition {
268-
match edition {
269-
RustEdition::E2015 => {
270-
cmd.args(&["--edition", "2015"]);
271-
}
272-
RustEdition::E2018 => {
273-
cmd.args(&["--edition", "2018"]);
274-
}
275-
}
254+
let chapter_path = match ch.path {
255+
Some(ref path) if !path.as_os_str().is_empty() => path,
256+
_ => continue,
257+
};
258+
259+
let path = self.source_dir().join(&chapter_path);
260+
info!("Testing file: {:?}", path);
261+
262+
// write preprocessed file to tempdir
263+
let path = temp_dir.path().join(&chapter_path);
264+
let mut tmpf = utils::fs::create_file(&path)?;
265+
tmpf.write_all(ch.content.as_bytes())?;
266+
267+
let mut cmd = Command::new("rustdoc");
268+
cmd.arg(&path).arg("--test").args(&library_args);
269+
270+
if let Some(edition) = self.config.rust.edition {
271+
match edition {
272+
RustEdition::E2015 => {
273+
cmd.args(&["--edition", "2015"]);
276274
}
277-
278-
let output = cmd.output()?;
279-
280-
if !output.status.success() {
281-
bail!(ErrorKind::Subprocess(
282-
"Rustdoc returned an error".to_string(),
283-
output
284-
));
275+
RustEdition::E2018 => {
276+
cmd.args(&["--edition", "2018"]);
285277
}
286278
}
287279
}
280+
281+
let output = cmd.output()?;
282+
283+
if !output.status.success() {
284+
bail!(ErrorKind::Subprocess(
285+
"Rustdoc returned an error".to_string(),
286+
output
287+
));
288+
}
288289
}
289290
}
290291
Ok(())

src/renderer/html_handlebars/hbs_renderer.rs

+68-70
Original file line numberDiff line numberDiff line change
@@ -30,84 +30,82 @@ impl HtmlHandlebars {
3030
print_content: &mut String,
3131
) -> Result<()> {
3232
// FIXME: This should be made DRY-er and rely less on mutable state
33-
if let BookItem::Chapter(ref ch) = *item {
34-
if let Some(ref path) = ch.path {
35-
let content = ch.content.clone();
36-
let content = utils::render_markdown(&content, ctx.html_config.curly_quotes);
37-
38-
let fixed_content = utils::render_markdown_with_path(
39-
&ch.content,
40-
ctx.html_config.curly_quotes,
41-
Some(&path),
42-
);
43-
print_content.push_str(&fixed_content);
4433

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

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

56-
// Non-lexical lifetimes needed :'(
57-
let title: String;
58-
{
59-
let book_title = ctx
60-
.data
61-
.get("book_title")
62-
.and_then(serde_json::Value::as_str)
63-
.unwrap_or("");
64-
65-
title = match book_title {
66-
"" => ch.name.clone(),
67-
_ => ch.name.clone() + " - " + book_title,
68-
}
69-
}
42+
let fixed_content = utils::render_markdown_with_path(
43+
&ch.content,
44+
ctx.html_config.curly_quotes,
45+
Some(&path),
46+
);
47+
print_content.push_str(&fixed_content);
7048

71-
ctx.data.insert("path".to_owned(), json!(path));
72-
ctx.data.insert("content".to_owned(), json!(content));
73-
ctx.data.insert("chapter_title".to_owned(), json!(ch.name));
74-
ctx.data.insert("title".to_owned(), json!(title));
75-
ctx.data.insert(
76-
"path_to_root".to_owned(),
77-
json!(utils::fs::path_to_root(&path)),
78-
);
79-
if let Some(ref section) = ch.number {
80-
ctx.data
81-
.insert("section".to_owned(), json!(section.to_string()));
82-
}
49+
// Update the context with data for this file
50+
let ctx_path = path
51+
.to_str()
52+
.chain_err(|| "Could not convert path to str")?;
53+
let filepath = Path::new(&ctx_path).with_extension("html");
8354

84-
// Render the handlebars template with the data
85-
debug!("Render template");
86-
let rendered = ctx.handlebars.render("index", &ctx.data)?;
87-
88-
let rendered = self.post_process(rendered, &ctx.html_config.playpen, ctx.edition);
89-
90-
// Write to file
91-
debug!("Creating {}", filepath.display());
92-
utils::fs::write_file(&ctx.destination, &filepath, rendered.as_bytes())?;
93-
94-
if ctx.is_index {
95-
ctx.data.insert("path".to_owned(), json!("index.md"));
96-
ctx.data.insert("path_to_root".to_owned(), json!(""));
97-
ctx.data.insert("is_index".to_owned(), json!("true"));
98-
let rendered_index = ctx.handlebars.render("index", &ctx.data)?;
99-
let rendered_index =
100-
self.post_process(rendered_index, &ctx.html_config.playpen, ctx.edition);
101-
debug!("Creating index.html from {}", ctx_path);
102-
utils::fs::write_file(
103-
&ctx.destination,
104-
"index.html",
105-
rendered_index.as_bytes(),
106-
)?;
107-
}
55+
// "print.html" is used for the print page.
56+
if path == Path::new("print.md") {
57+
bail!(ErrorKind::ReservedFilenameError(path.clone()));
58+
};
59+
60+
// Non-lexical lifetimes needed :'(
61+
let title: String;
62+
{
63+
let book_title = ctx
64+
.data
65+
.get("book_title")
66+
.and_then(serde_json::Value::as_str)
67+
.unwrap_or("");
68+
69+
title = match book_title {
70+
"" => ch.name.clone(),
71+
_ => ch.name.clone() + " - " + book_title,
10872
}
10973
}
11074

75+
ctx.data.insert("path".to_owned(), json!(path));
76+
ctx.data.insert("content".to_owned(), json!(content));
77+
ctx.data.insert("chapter_title".to_owned(), json!(ch.name));
78+
ctx.data.insert("title".to_owned(), json!(title));
79+
ctx.data.insert(
80+
"path_to_root".to_owned(),
81+
json!(utils::fs::path_to_root(&path)),
82+
);
83+
if let Some(ref section) = ch.number {
84+
ctx.data
85+
.insert("section".to_owned(), json!(section.to_string()));
86+
}
87+
88+
// Render the handlebars template with the data
89+
debug!("Render template");
90+
let rendered = ctx.handlebars.render("index", &ctx.data)?;
91+
92+
let rendered = self.post_process(rendered, &ctx.html_config.playpen, ctx.edition);
93+
94+
// Write to file
95+
debug!("Creating {}", filepath.display());
96+
utils::fs::write_file(&ctx.destination, &filepath, rendered.as_bytes())?;
97+
98+
if ctx.is_index {
99+
ctx.data.insert("path".to_owned(), json!("index.md"));
100+
ctx.data.insert("path_to_root".to_owned(), json!(""));
101+
ctx.data.insert("is_index".to_owned(), json!("true"));
102+
let rendered_index = ctx.handlebars.render("index", &ctx.data)?;
103+
let rendered_index =
104+
self.post_process(rendered_index, &ctx.html_config.playpen, ctx.edition);
105+
debug!("Creating index.html from {}", ctx_path);
106+
utils::fs::write_file(&ctx.destination, "index.html", rendered_index.as_bytes())?;
107+
}
108+
111109
Ok(())
112110
}
113111

src/renderer/html_handlebars/search.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,7 @@ fn render_item(
7171
item: &BookItem,
7272
) -> Result<()> {
7373
let chapter = match *item {
74-
BookItem::Chapter(ref ch) => {
75-
if let Some(_) = ch.path {
76-
ch
77-
} else {
78-
return Ok(());
79-
}
80-
}
74+
BookItem::Chapter(ref ch) if !ch.is_draft_chapter() => ch,
8175
_ => return Ok(()),
8276
};
8377

0 commit comments

Comments
 (0)