From ddf31dcc086ffc45defebfd30aa0de8b318f7348 Mon Sep 17 00:00:00 2001 From: Michal Budzynski Date: Mon, 7 Aug 2017 01:36:29 +0200 Subject: [PATCH] Fixed `mdbook test` for {{#playpen file.rs}} - now `mdbook test` does full link expansion to temp file prior to running - also minor reformat and cleanup of `HtmlHandlebars::render_item` --- Cargo.toml | 5 +- src/book/mod.rs | 17 ++++- src/lib.rs | 2 - src/renderer/html_handlebars/hbs_renderer.rs | 74 +++++++++----------- 4 files changed, 49 insertions(+), 49 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1ba96365..9387d76a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ env_logger = "0.4.0" toml = { version = "0.4", features = ["serde"] } open = "1.1" regex = "0.2.1" +tempdir = "0.3.4" # Watch feature notify = { version = "4.0", optional = true } @@ -39,10 +40,6 @@ iron = { version = "0.5", optional = true } staticfile = { version = "0.4", optional = true } ws = { version = "0.7", optional = true} -# Tests -[dev-dependencies] -tempdir = "0.3.4" - [build-dependencies] error-chain = "0.10" diff --git a/src/book/mod.rs b/src/book/mod.rs index f07ab665..437b13d1 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -6,9 +6,11 @@ use std::path::{Path, PathBuf}; use std::fs::{self, File}; use std::io::{Read, Write}; use std::process::Command; +use tempdir::TempDir; use {theme, parse, utils}; use renderer::{Renderer, HtmlHandlebars}; +use preprocess; use errors::*; use config::BookConfig; @@ -358,15 +360,26 @@ impl MDBook { .zip(library_paths.into_iter()) .flat_map(|x| vec![x.0, x.1]) .collect(); + let temp_dir = TempDir::new("mdbook")?; for item in self.iter() { if let BookItem::Chapter(_, ref ch) = *item { - if ch.path != PathBuf::new() { + if !ch.path.as_os_str().is_empty() { let path = self.get_source().join(&ch.path); - + let base = path.parent().ok_or_else( + || String::from("Invalid bookitem path!"), + )?; + let content = utils::fs::file_to_string(&path)?; + // Parse and expand links + let content = preprocess::links::replace_all(&content, base)?; println!("[*]: Testing file: {:?}", path); + //write preprocessed file to tempdir + let path = temp_dir.path().join(&ch.path); + let mut tmpf = utils::fs::create_file(&path)?; + tmpf.write_all(content.as_bytes())?; + let output = Command::new("rustdoc").arg(&path).arg("--test").args(&library_args).output()?; if !output.status.success() { diff --git a/src/lib.rs b/src/lib.rs index e6c54443..fff77bbd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,8 +85,6 @@ extern crate serde_derive; extern crate serde; #[macro_use] extern crate serde_json; - -#[cfg(test)] extern crate tempdir; mod parse; diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 2e3972f4..9ace5dcc 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -33,55 +33,47 @@ impl HtmlHandlebars { // FIXME: This should be made DRY-er and rely less on mutable state match *item { BookItem::Chapter(_, ref ch) | - BookItem::Affix(ref ch) => { - if ch.path != PathBuf::new() { + BookItem::Affix(ref ch) if !ch.path.as_os_str().is_empty() => { - let path = ctx.book.get_source().join(&ch.path); + let path = ctx.book.get_source().join(&ch.path); + let content = utils::fs::file_to_string(&path)?; + let base = path.parent().ok_or_else( + || String::from("Invalid bookitem path!"), + )?; - debug!("[*]: Opening file: {:?}", path); - let mut f = File::open(&path)?; - let mut content: String = String::new(); + // Parse and expand links + let content = preprocess::links::replace_all(&content, base)?; + let content = utils::render_markdown(&content, ctx.book.get_curly_quotes()); + print_content.push_str(&content); - debug!("[*]: Reading file"); - f.read_to_string(&mut content)?; + // Update the context with data for this file + let path = ch.path.to_str().ok_or_else(|| { + io::Error::new(io::ErrorKind::Other, "Could not convert path to str") + })?; - // Parse and expand links - if let Some(p) = path.parent() { - content = preprocess::links::replace_all(&content, p)?; - } + 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( + "path_to_root".to_owned(), + json!(utils::fs::path_to_root(&ch.path)), + ); - content = utils::render_markdown(&content, ctx.book.get_curly_quotes()); - print_content.push_str(&content); + // Render the handlebars template with the data + debug!("[*]: Render template"); + let rendered = ctx.handlebars.render("index", &ctx.data)?; - // Update the context with data for this file - let path = ch.path.to_str().ok_or_else(|| { - io::Error::new(io::ErrorKind::Other, "Could not convert path to str") - })?; + let filename = Path::new(&ch.path).with_extension("html"); + let rendered = self.post_process(rendered, + filename.file_name().unwrap().to_str().unwrap_or(""), + ctx.book.get_html_config().get_playpen_config()); - 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( - "path_to_root".to_owned(), - json!(utils::fs::path_to_root(&ch.path)), - ); + // Write to file + info!("[*] Creating {:?} ✓", filename.display()); + ctx.book.write_file(filename, &rendered.into_bytes())?; - // Render the handlebars template with the data - debug!("[*]: Render template"); - let rendered = ctx.handlebars.render("index", &ctx.data)?; - - let filename = Path::new(&ch.path).with_extension("html"); - let rendered = self.post_process(rendered, - filename.file_name().unwrap().to_str().unwrap_or(""), - ctx.book.get_html_config().get_playpen_config()); - - // Write to file - info!("[*] Creating {:?} ✓", filename.display()); - ctx.book.write_file(filename, &rendered.into_bytes())?; - - if ctx.is_index { - self.render_index(ctx.book, ch, &ctx.destination)?; - } + if ctx.is_index { + self.render_index(ctx.book, ch, &ctx.destination)?; } }, _ => {},