Clean up: Removed uneeded function because the same functionality is present in std

This commit is contained in:
Mathieu David 2015-07-30 13:49:49 +02:00
parent 18c3ed4540
commit c63ca25e43
1 changed files with 22 additions and 34 deletions

View File

@ -42,7 +42,7 @@ impl Renderer for HtmlHandlebars {
if item.path != PathBuf::new() { if item.path != PathBuf::new() {
let path = config.src().join(&item.path); let path = config.src().join(&item.path);
println!("Open file: {:?}", path);
let mut f = try!(File::open(&path)); let mut f = try!(File::open(&path));
let mut content: String = String::new(); let mut content: String = String::new();
@ -68,10 +68,16 @@ impl Renderer for HtmlHandlebars {
// Create an index.html from the first element in SUMMARY.md // Create an index.html from the first element in SUMMARY.md
if index { if index {
print!(
"Creating index.html from {:?}",
config.dest().join(&item.path.with_extension("html"))
);
try!(fs::copy( try!(fs::copy(
config.dest().join(path_to_link(&item.path).expect("Failed at creation of index.html")), config.dest().join(&item.path.with_extension("html")),
config.dest().join("index.html") config.dest().join("index.html")
)); ));
println!("");
index = false; index = false;
} }
} }
@ -106,8 +112,6 @@ impl HtmlHandlebars {
fn create_file(working_directory: &Path, path: &Path) -> Result<File, Box<Error>> { fn create_file(working_directory: &Path, path: &Path) -> Result<File, Box<Error>> {
println!("create_file:\n\t{:?}\n\t{:?}", working_directory, path);
// Extract filename // Extract filename
let mut file_name; let mut file_name;
if let Some(name) = path.file_stem() { if let Some(name) = path.file_stem() {
@ -134,8 +138,6 @@ fn create_file(working_directory: &Path, path: &Path) -> Result<File, Box<Error>
constructed_path.push(&dir); constructed_path.push(&dir);
println!("constructed path= {:?}\ndir= {:?}", constructed_path, dir.as_os_str());
// Check if path exists // Check if path exists
match metadata(&constructed_path) { match metadata(&constructed_path) {
// Any way to combine the Err and first Ok branch ?? // Any way to combine the Err and first Ok branch ??
@ -146,7 +148,6 @@ fn create_file(working_directory: &Path, path: &Path) -> Result<File, Box<Error>
if !f.is_dir() { if !f.is_dir() {
try!(fs::create_dir(&constructed_path)) try!(fs::create_dir(&constructed_path))
} else { } else {
println!("Exists ??");
continue continue
} }
}, },
@ -154,9 +155,11 @@ fn create_file(working_directory: &Path, path: &Path) -> Result<File, Box<Error>
} }
print!("Create file: {:?}", constructed_path.join(&file_name));
let file = try!(File::create( let file = try!(File::create(
constructed_path.join(file_name) constructed_path.join(&file_name)
)); ));
println!("");
Ok(file) Ok(file)
} }
@ -210,25 +213,6 @@ fn render_html(text: &str) -> String {
s s
} }
fn path_to_link(path: &Path) -> Option<PathBuf> {
// Extract filename
let mut file_name;
if let Some(name) = path.file_stem() {
file_name = String::from(name.to_str().unwrap());
}
else { return None }
file_name.push_str(".html");
// Change file name to .html
let mut path = path.to_path_buf();
path.set_file_name(file_name);
// Clean paths with './'
Some(path)
}
fn path_to_root(path: &Path) -> String { fn path_to_root(path: &Path) -> String {
// Remove filename and add "../" for every directory // Remove filename and add "../" for every directory
@ -281,13 +265,17 @@ impl HelperDef for RenderToc {
if path.len() > 0 { if path.len() > 0 {
try!(rc.writer.write("<a href=\"".as_bytes())); try!(rc.writer.write("<a href=\"".as_bytes()));
if let Some(link) = path_to_link(Path::new(item.get("path").expect("Error: path should be Some(_)"))) { // Prefix with path to root
try!(rc.writer.write(path_to_root.as_bytes())); try!(rc.writer.write(path_to_root.as_bytes()));
println!("Path to root: {}", path_to_root);
try!(rc.writer.write(link.to_str().unwrap().as_bytes())); // Add link
} else { try!(rc.writer.write(
try!(rc.writer.write("#".as_bytes())); Path::new(
} item.get("path")
.expect("Error: path should be Some(_)")
).with_extension("html")
.to_str().unwrap().as_bytes()
));
try!(rc.writer.write("\">".as_bytes())); try!(rc.writer.write("\">".as_bytes()));
true true