Prohibit 'print.md' files (#524)

* Prohibit 'print.md' files

Fix #258 by emmiting an error whenever an mdBook contains a
"print.md" file in its root.

* Apply suggested changes

* Add tests
This commit is contained in:
Jan Likar 2018-01-06 17:02:23 +01:00 committed by Michael Bryan
parent e461610dab
commit dedc208a6a
3 changed files with 33 additions and 0 deletions

View File

@ -129,6 +129,8 @@ pub use renderer::Renderer;
/// The error types used through out this crate.
pub mod errors {
use std::path::PathBuf;
error_chain!{
foreign_links {
Io(::std::io::Error);
@ -147,6 +149,11 @@ pub mod errors {
description("A SUMMARY.md parsing error")
display("Error at line {}, column {}: {}", line, col, message)
}
ReservedFilenameError(filename: PathBuf) {
description("Reserved Filename")
display("{} is reserved for internal use", filename.display())
}
}
}

View File

@ -54,6 +54,11 @@ impl HtmlHandlebars {
to str")
})?;
// "print.html" is used for the print page.
if ch.path == Path::new("print.md") {
bail!(ErrorKind::ReservedFilenameError(ch.path.clone()));
};
// Non-lexical lifetimes needed :'(
let title: String;
{

View File

@ -2,6 +2,7 @@ extern crate mdbook;
#[macro_use]
extern crate pretty_assertions;
extern crate select;
extern crate tempdir;
extern crate walkdir;
mod dummy_book;
@ -9,11 +10,13 @@ mod dummy_book;
use dummy_book::{assert_contains_strings, DummyBook};
use std::fs;
use std::io::Write;
use std::path::Path;
use std::ffi::OsStr;
use walkdir::{DirEntry, WalkDir};
use select::document::Document;
use select::predicate::{Class, Name, Predicate};
use tempdir::TempDir;
use mdbook::errors::*;
use mdbook::utils::fs::file_to_string;
use mdbook::config::Config;
@ -304,3 +307,21 @@ fn example_book_can_build() {
let got = md.build();
assert!(got.is_ok());
}
#[test]
fn book_with_a_reserved_filename_does_not_build() {
let tmp_dir = TempDir::new("mdBook").unwrap();
let src_path = tmp_dir.path().join("src");
fs::create_dir(&src_path).unwrap();
let summary_path = src_path.join("SUMMARY.md");
let print_path = src_path.join("print.md");
fs::File::create(print_path).unwrap();
let mut summary_file = fs::File::create(summary_path).unwrap();
writeln!(summary_file, "[print](print.md)").unwrap();
let mut md = MDBook::load(tmp_dir.path()).unwrap();
let got = md.build();
assert!(got.is_err());
}