From dedc208a6a4ffd7d5bf81bf675d915138f8d8860 Mon Sep 17 00:00:00 2001 From: Jan Likar Date: Sat, 6 Jan 2018 17:02:23 +0100 Subject: [PATCH] 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 --- src/lib.rs | 7 +++++++ src/renderer/html_handlebars/hbs_renderer.rs | 5 +++++ tests/rendered_output.rs | 21 ++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 72b4df63..3efef1d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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()) + } } } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index aae3cdac..28b49376 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -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; { diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index cff98899..dd630847 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -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()); +}