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"] }
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"

View File

@ -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() {

View File

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

View File

@ -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)?;
}
},
_ => {},