Skip to content

Commit bf114d7

Browse files
committed
Removed the sub module from utils and put the functions directly in the utils module + docs #30
1 parent b7214f9 commit bf114d7

File tree

4 files changed

+112
-85
lines changed

4 files changed

+112
-85
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
//! I have regrouped some useful functions in the [utils](utils/index.html) module, like the following function
6464
//!
6565
//! ```ignore
66-
//! utils::path::create_path(path: &Path)
66+
//! utils::create_path(path: &Path)
6767
//! ```
6868
//! This function creates all the directories in a given path if they do not exist
6969
//!

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Renderer for HtmlHandlebars {
4747

4848
// Check if dest directory exists
4949
debug!("[*]: Check if destination directory exists");
50-
match utils::path::create_path(config.dest()) {
50+
match utils::create_path(config.dest()) {
5151
Err(_) => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Unexcpected error when constructing destination path"))),
5252
_ => {},
5353
};
@@ -80,15 +80,15 @@ impl Renderer for HtmlHandlebars {
8080

8181
// Remove path to root from previous file and render content for this one
8282
data.remove("path_to_root");
83-
data.insert("path_to_root".to_string(), utils::path::path_to_root(&item.path).to_json());
83+
data.insert("path_to_root".to_string(), utils::path_to_root(&item.path).to_json());
8484

8585
// Rendere the handlebars template with the data
8686
debug!("[*]: Render template");
8787
let rendered = try!(handlebars.render("index", &data));
8888

8989
debug!("[*]: Create file {:?}", &config.dest().join(&item.path).with_extension("html"));
9090
// Write to file
91-
let mut file = try!(utils::path::create_file(&config.dest().join(&item.path).with_extension("html")));
91+
let mut file = try!(utils::create_file(&config.dest().join(&item.path).with_extension("html")));
9292
output!("[*] Creating {:?} ✓", &config.dest().join(&item.path).with_extension("html"));
9393

9494
try!(file.write_all(&rendered.into_bytes()));

src/utils/mod.rs

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,108 @@
1-
pub mod path;
1+
use std::path::{Path, PathBuf, Component};
2+
use std::error::Error;
3+
use std::fs::{self, metadata, File};
4+
5+
6+
/// Takes a path and returns a path containing just enough `../` to point to the root of the given path.
7+
///
8+
/// This is mostly interesting for a relative path to point back to the directory from where the
9+
/// path starts.
10+
///
11+
/// ```ignore
12+
/// let mut path = Path::new("some/relative/path");
13+
///
14+
/// println!("{}", path_to_root(&path));
15+
/// ```
16+
///
17+
/// **Outputs**
18+
///
19+
/// ```text
20+
/// "../../"
21+
/// ```
22+
///
23+
/// **note:** it's not very fool-proof, if you find a situation where it doesn't return the correct
24+
/// path. Consider [submitting a new issue](https://github.com/azerupi/mdBook/issues) or a
25+
/// [pull-request](https://github.com/azerupi/mdBook/pulls) to improve it.
26+
27+
pub fn path_to_root(path: &Path) -> String {
28+
debug!("[fn]: path_to_root");
29+
// Remove filename and add "../" for every directory
30+
31+
path.to_path_buf().parent().expect("")
32+
.components().fold(String::new(), |mut s, c| {
33+
match c {
34+
Component::Normal(_) => s.push_str("../"),
35+
_ => {
36+
debug!("[*]: Other path component... {:?}", c);
37+
}
38+
}
39+
s
40+
})
41+
}
42+
43+
/// This function checks for every component in a path if the directory exists,
44+
/// if it does not it is created.
45+
46+
pub fn create_path(path: &Path) -> Result<(), Box<Error>> {
47+
debug!("[fn]: create_path");
48+
49+
// Create directories if they do not exist
50+
let mut constructed_path = PathBuf::new();
51+
52+
for component in path.components() {
53+
54+
let mut dir;
55+
match component {
56+
Component::Normal(_) => { dir = PathBuf::from(component.as_os_str()); },
57+
Component::RootDir => {
58+
debug!("[*]: Root directory");
59+
// This doesn't look very compatible with Windows...
60+
constructed_path.push("/");
61+
continue
62+
},
63+
_ => continue,
64+
}
65+
66+
constructed_path.push(&dir);
67+
debug!("[*]: {:?}", constructed_path);
68+
69+
// Check if path exists
70+
match metadata(&constructed_path) {
71+
// Any way to combine the Err and first Ok branch ??
72+
Err(_) => {
73+
try!(fs::create_dir(&constructed_path));
74+
debug!("[*]: Directory created {:?}", constructed_path);
75+
},
76+
Ok(f) => {
77+
if !f.is_dir() {
78+
try!(fs::create_dir(&constructed_path));
79+
debug!("[*]: Directory created {:?}", constructed_path);
80+
} else {
81+
debug!("[*]: Directory exists {:?}", constructed_path);
82+
continue
83+
}
84+
},
85+
}
86+
}
87+
88+
debug!("[*]: Constructed path: {:?}", constructed_path);
89+
90+
Ok(())
91+
}
92+
93+
/// This function creates a file and returns it. But before creating the file it checks every
94+
/// directory in the path to see if it exists, and if it does not it will be created.
95+
96+
pub fn create_file(path: &Path) -> Result<File, Box<Error>> {
97+
debug!("[fn]: create_file");
98+
99+
// Construct path
100+
if let Some(p) = path.parent() {
101+
try!(create_path(p));
102+
}
103+
104+
debug!("[*]: Create file: {}", path);
105+
let f = try!(File::create(path));
106+
107+
Ok(f)
108+
}

src/utils/path.rs

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)