2015-12-30 07:46:55 +08:00
|
|
|
extern crate pulldown_cmark;
|
|
|
|
|
2016-01-03 20:02:04 +08:00
|
|
|
use std::path::{Path, Component};
|
2015-08-07 05:04:19 +08:00
|
|
|
use std::error::Error;
|
2016-01-03 20:02:04 +08:00
|
|
|
use std::io;
|
2015-08-07 05:04:19 +08:00
|
|
|
use std::fs::{self, metadata, File};
|
|
|
|
|
2016-01-03 19:02:39 +08:00
|
|
|
use self::pulldown_cmark::{Parser, html, Options, OPTION_ENABLE_TABLES, OPTION_ENABLE_FOOTNOTES};
|
2015-12-30 07:46:55 +08:00
|
|
|
|
2015-08-07 05:04:19 +08:00
|
|
|
/// Takes a path and returns a path containing just enough `../` to point to the root of the given path.
|
|
|
|
///
|
|
|
|
/// This is mostly interesting for a relative path to point back to the directory from where the
|
|
|
|
/// path starts.
|
|
|
|
///
|
|
|
|
/// ```ignore
|
|
|
|
/// let mut path = Path::new("some/relative/path");
|
|
|
|
///
|
|
|
|
/// println!("{}", path_to_root(&path));
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// **Outputs**
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// "../../"
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// **note:** it's not very fool-proof, if you find a situation where it doesn't return the correct
|
|
|
|
/// path. Consider [submitting a new issue](https://github.com/azerupi/mdBook/issues) or a
|
|
|
|
/// [pull-request](https://github.com/azerupi/mdBook/pulls) to improve it.
|
|
|
|
|
|
|
|
pub fn path_to_root(path: &Path) -> String {
|
|
|
|
debug!("[fn]: path_to_root");
|
|
|
|
// Remove filename and add "../" for every directory
|
|
|
|
|
2016-03-18 05:31:28 +08:00
|
|
|
path.to_path_buf()
|
|
|
|
.parent()
|
|
|
|
.expect("")
|
|
|
|
.components()
|
|
|
|
.fold(String::new(), |mut s, c| {
|
2015-08-07 05:04:19 +08:00
|
|
|
match c {
|
|
|
|
Component::Normal(_) => s.push_str("../"),
|
|
|
|
_ => {
|
|
|
|
debug!("[*]: Other path component... {:?}", c);
|
2016-03-18 05:31:28 +08:00
|
|
|
},
|
2015-08-07 05:04:19 +08:00
|
|
|
}
|
|
|
|
s
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// This function creates a file and returns it. But before creating the file it checks every
|
|
|
|
/// directory in the path to see if it exists, and if it does not it will be created.
|
|
|
|
|
|
|
|
pub fn create_file(path: &Path) -> Result<File, Box<Error>> {
|
|
|
|
debug!("[fn]: create_file");
|
|
|
|
|
|
|
|
// Construct path
|
|
|
|
if let Some(p) = path.parent() {
|
2016-01-03 20:02:04 +08:00
|
|
|
debug!("Parent directory is: {:?}", p);
|
|
|
|
|
|
|
|
try!(fs::create_dir_all(p));
|
2015-08-07 05:04:19 +08:00
|
|
|
}
|
|
|
|
|
2015-08-13 16:46:56 +08:00
|
|
|
debug!("[*]: Create file: {:?}", path);
|
2016-01-03 20:02:04 +08:00
|
|
|
let f = match File::create(path) {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(e) => {
|
|
|
|
debug!("File::create: {}", e);
|
2016-03-18 05:31:28 +08:00
|
|
|
return Err(Box::new(io::Error::new(io::ErrorKind::Other, format!("{}", e))));
|
2016-01-03 20:02:04 +08:00
|
|
|
},
|
|
|
|
};
|
2015-08-07 05:04:19 +08:00
|
|
|
|
|
|
|
Ok(f)
|
|
|
|
}
|
2015-08-16 19:20:10 +08:00
|
|
|
|
|
|
|
/// Removes all the content of a directory but not the directory itself
|
|
|
|
|
|
|
|
pub fn remove_dir_content(dir: &Path) -> Result<(), Box<Error>> {
|
|
|
|
for item in try!(fs::read_dir(dir)) {
|
|
|
|
if let Ok(item) = item {
|
|
|
|
let item = item.path();
|
2016-03-18 05:31:28 +08:00
|
|
|
if item.is_dir() {
|
|
|
|
try!(fs::remove_dir_all(item));
|
|
|
|
} else {
|
|
|
|
try!(fs::remove_file(item));
|
|
|
|
}
|
2015-08-16 19:20:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-09-17 01:01:53 +08:00
|
|
|
|
2015-12-30 07:46:55 +08:00
|
|
|
///
|
2015-09-17 01:01:53 +08:00
|
|
|
///
|
|
|
|
/// Copies all files of a directory to another one except the files with the extensions given in the
|
|
|
|
/// `ext_blacklist` array
|
|
|
|
|
2015-09-17 05:35:16 +08:00
|
|
|
pub fn copy_files_except_ext(from: &Path, to: &Path, recursive: bool, ext_blacklist: &[&str]) -> Result<(), Box<Error>> {
|
2015-09-18 01:45:06 +08:00
|
|
|
debug!("[fn] copy_files_except_ext");
|
2015-09-17 01:01:53 +08:00
|
|
|
// Check that from and to are different
|
2016-03-18 05:31:28 +08:00
|
|
|
if from == to {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2015-09-18 01:45:06 +08:00
|
|
|
debug!("[*] Loop");
|
2015-09-17 01:01:53 +08:00
|
|
|
for entry in try!(fs::read_dir(from)) {
|
|
|
|
let entry = try!(entry);
|
2015-09-18 01:45:06 +08:00
|
|
|
debug!("[*] {:?}", entry.path());
|
2015-09-17 01:01:53 +08:00
|
|
|
let metadata = try!(entry.metadata());
|
|
|
|
|
|
|
|
// If the entry is a dir and the recursive option is enabled, call itself
|
|
|
|
if metadata.is_dir() && recursive {
|
2016-03-18 05:31:28 +08:00
|
|
|
if entry.path() == to.to_path_buf() {
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-18 01:45:06 +08:00
|
|
|
debug!("[*] is dir");
|
2015-09-18 21:28:58 +08:00
|
|
|
|
|
|
|
// check if output dir already exists
|
|
|
|
if !to.join(entry.file_name()).exists() {
|
|
|
|
try!(fs::create_dir(&to.join(entry.file_name())));
|
|
|
|
}
|
|
|
|
|
2016-03-18 05:31:28 +08:00
|
|
|
try!(copy_files_except_ext(&from.join(entry.file_name()),
|
|
|
|
&to.join(entry.file_name()),
|
|
|
|
true,
|
|
|
|
ext_blacklist));
|
2015-09-17 01:01:53 +08:00
|
|
|
} else if metadata.is_file() {
|
|
|
|
|
|
|
|
// Check if it is in the blacklist
|
|
|
|
if let Some(ext) = entry.path().extension() {
|
2016-03-18 05:31:28 +08:00
|
|
|
if ext_blacklist.contains(&ext.to_str().unwrap()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
debug!("[*] creating path for file: {:?}",
|
|
|
|
&to.join(entry.path().file_name().expect("a file should have a file name...")));
|
|
|
|
|
|
|
|
output!("[*] copying file: {:?}\n to {:?}",
|
|
|
|
entry.path(),
|
|
|
|
&to.join(entry.path().file_name().expect("a file should have a file name...")));
|
|
|
|
try!(fs::copy(entry.path(),
|
|
|
|
&to.join(entry.path().file_name().expect("a file should have a file name..."))));
|
2015-09-17 01:01:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2015-09-17 05:35:16 +08:00
|
|
|
|
|
|
|
|
2015-12-30 07:46:55 +08:00
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Wrapper around the pulldown-cmark parser and renderer to render markdown
|
|
|
|
|
|
|
|
pub fn render_markdown(text: &str) -> String {
|
|
|
|
let mut s = String::with_capacity(text.len() * 3 / 2);
|
2016-01-03 19:02:39 +08:00
|
|
|
|
|
|
|
let mut opts = Options::empty();
|
|
|
|
opts.insert(OPTION_ENABLE_TABLES);
|
|
|
|
opts.insert(OPTION_ENABLE_FOOTNOTES);
|
|
|
|
|
|
|
|
let p = Parser::new_ext(&text, opts);
|
2015-12-30 07:46:55 +08:00
|
|
|
html::push_html(&mut s, p);
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-17 05:35:16 +08:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// tests
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
extern crate tempdir;
|
|
|
|
|
|
|
|
use super::copy_files_except_ext;
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn copy_files_except_ext_test() {
|
|
|
|
let tmp = match tempdir::TempDir::new("") {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(_) => panic!("Could not create a temp dir"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create a couple of files
|
2016-03-18 05:31:28 +08:00
|
|
|
if let Err(_) = fs::File::create(&tmp.path().join("file.txt")) {
|
|
|
|
panic!("Could not create file.txt")
|
|
|
|
}
|
|
|
|
if let Err(_) = fs::File::create(&tmp.path().join("file.md")) {
|
|
|
|
panic!("Could not create file.md")
|
|
|
|
}
|
|
|
|
if let Err(_) = fs::File::create(&tmp.path().join("file.png")) {
|
|
|
|
panic!("Could not create file.png")
|
|
|
|
}
|
|
|
|
if let Err(_) = fs::create_dir(&tmp.path().join("sub_dir")) {
|
|
|
|
panic!("Could not create sub_dir")
|
|
|
|
}
|
|
|
|
if let Err(_) = fs::File::create(&tmp.path().join("sub_dir/file.png")) {
|
|
|
|
panic!("Could not create sub_dir/file.png")
|
|
|
|
}
|
|
|
|
if let Err(_) = fs::create_dir(&tmp.path().join("sub_dir_exists")) {
|
|
|
|
panic!("Could not create sub_dir_exists")
|
|
|
|
}
|
|
|
|
if let Err(_) = fs::File::create(&tmp.path().join("sub_dir_exists/file.txt")) {
|
|
|
|
panic!("Could not create sub_dir_exists/file.txt")
|
|
|
|
}
|
2015-09-17 05:35:16 +08:00
|
|
|
|
|
|
|
// Create output dir
|
2016-03-18 05:31:28 +08:00
|
|
|
if let Err(_) = fs::create_dir(&tmp.path().join("output")) {
|
|
|
|
panic!("Could not create output")
|
|
|
|
}
|
|
|
|
if let Err(_) = fs::create_dir(&tmp.path().join("output/sub_dir_exists")) {
|
|
|
|
panic!("Could not create output/sub_dir_exists")
|
|
|
|
}
|
2015-09-17 05:35:16 +08:00
|
|
|
|
|
|
|
match copy_files_except_ext(&tmp.path(), &tmp.path().join("output"), true, &["md"]) {
|
|
|
|
Err(e) => panic!("Error while executing the function:\n{:?}", e),
|
|
|
|
Ok(_) => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the correct files where created
|
2016-03-18 05:31:28 +08:00
|
|
|
if !(&tmp.path().join("output/file.txt")).exists() {
|
|
|
|
panic!("output/file.txt should exist")
|
|
|
|
}
|
|
|
|
if (&tmp.path().join("output/file.md")).exists() {
|
|
|
|
panic!("output/file.md should not exist")
|
|
|
|
}
|
|
|
|
if !(&tmp.path().join("output/file.png")).exists() {
|
|
|
|
panic!("output/file.png should exist")
|
|
|
|
}
|
|
|
|
if !(&tmp.path().join("output/sub_dir/file.png")).exists() {
|
|
|
|
panic!("output/sub_dir/file.png should exist")
|
|
|
|
}
|
|
|
|
if !(&tmp.path().join("output/sub_dir_exists/file.txt")).exists() {
|
|
|
|
panic!("output/sub_dir/file.png should exist")
|
|
|
|
}
|
2015-09-17 05:35:16 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|