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`
This commit is contained in:
Michal Budzynski 2017-08-07 01:36:29 +02:00 committed by Michał Budzyński
parent c36eca15c2
commit ddf31dcc08
4 changed files with 49 additions and 49 deletions

View file

@ -28,6 +28,7 @@ env_logger = "0.4.0"
toml = { version = "0.4", features = ["serde"] } toml = { version = "0.4", features = ["serde"] }
open = "1.1" open = "1.1"
regex = "0.2.1" regex = "0.2.1"
tempdir = "0.3.4"
# Watch feature # Watch feature
notify = { version = "4.0", optional = true } notify = { version = "4.0", optional = true }
@ -39,10 +40,6 @@ iron = { version = "0.5", optional = true }
staticfile = { version = "0.4", optional = true } staticfile = { version = "0.4", optional = true }
ws = { version = "0.7", optional = true} ws = { version = "0.7", optional = true}
# Tests
[dev-dependencies]
tempdir = "0.3.4"
[build-dependencies] [build-dependencies]
error-chain = "0.10" error-chain = "0.10"

View file

@ -6,9 +6,11 @@ use std::path::{Path, PathBuf};
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::process::Command; use std::process::Command;
use tempdir::TempDir;
use {theme, parse, utils}; use {theme, parse, utils};
use renderer::{Renderer, HtmlHandlebars}; use renderer::{Renderer, HtmlHandlebars};
use preprocess;
use errors::*; use errors::*;
use config::BookConfig; use config::BookConfig;
@ -358,15 +360,26 @@ impl MDBook {
.zip(library_paths.into_iter()) .zip(library_paths.into_iter())
.flat_map(|x| vec![x.0, x.1]) .flat_map(|x| vec![x.0, x.1])
.collect(); .collect();
let temp_dir = TempDir::new("mdbook")?;
for item in self.iter() { for item in self.iter() {
if let BookItem::Chapter(_, ref ch) = *item { 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 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); 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()?; let output = Command::new("rustdoc").arg(&path).arg("--test").args(&library_args).output()?;
if !output.status.success() { if !output.status.success() {

View file

@ -85,8 +85,6 @@ extern crate serde_derive;
extern crate serde; extern crate serde;
#[macro_use] #[macro_use]
extern crate serde_json; extern crate serde_json;
#[cfg(test)]
extern crate tempdir; extern crate tempdir;
mod parse; mod parse;

View file

@ -33,55 +33,47 @@ impl HtmlHandlebars {
// FIXME: This should be made DRY-er and rely less on mutable state // FIXME: This should be made DRY-er and rely less on mutable state
match *item { match *item {
BookItem::Chapter(_, ref ch) | BookItem::Chapter(_, ref ch) |
BookItem::Affix(ref ch) => { BookItem::Affix(ref ch) if !ch.path.as_os_str().is_empty() => {
if ch.path != PathBuf::new() {
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); // Parse and expand links
let mut f = File::open(&path)?; let content = preprocess::links::replace_all(&content, base)?;
let mut content: String = String::new(); let content = utils::render_markdown(&content, ctx.book.get_curly_quotes());
print_content.push_str(&content);
debug!("[*]: Reading file"); // Update the context with data for this file
f.read_to_string(&mut content)?; 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 ctx.data.insert("path".to_owned(), json!(path));
if let Some(p) = path.parent() { ctx.data.insert("content".to_owned(), json!(content));
content = preprocess::links::replace_all(&content, p)?; 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()); // Render the handlebars template with the data
print_content.push_str(&content); debug!("[*]: Render template");
let rendered = ctx.handlebars.render("index", &ctx.data)?;
// Update the context with data for this file let filename = Path::new(&ch.path).with_extension("html");
let path = ch.path.to_str().ok_or_else(|| { let rendered = self.post_process(rendered,
io::Error::new(io::ErrorKind::Other, "Could not convert path to str") filename.file_name().unwrap().to_str().unwrap_or(""),
})?; ctx.book.get_html_config().get_playpen_config());
ctx.data.insert("path".to_owned(), json!(path)); // Write to file
ctx.data.insert("content".to_owned(), json!(content)); info!("[*] Creating {:?} ✓", filename.display());
ctx.data.insert("chapter_title".to_owned(), json!(ch.name)); ctx.book.write_file(filename, &rendered.into_bytes())?;
ctx.data.insert(
"path_to_root".to_owned(),
json!(utils::fs::path_to_root(&ch.path)),
);
// Render the handlebars template with the data if ctx.is_index {
debug!("[*]: Render template"); self.render_index(ctx.book, ch, &ctx.destination)?;
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)?;
}
} }
}, },
_ => {}, _ => {},