Continue #29 Check that the rust file exists and read to string

This commit is contained in:
Mathieu David 2015-12-31 14:04:11 +01:00
parent 0cb234de5d
commit 38b2dee17e
2 changed files with 39 additions and 18 deletions

View File

@ -71,8 +71,12 @@ impl Renderer for HtmlHandlebars {
debug!("[*]: Reading file"); debug!("[*]: Reading file");
try!(f.read_to_string(&mut content)); try!(f.read_to_string(&mut content));
// Parse for playpen links
if let Some(p) = path.parent() {
helpers::playpen::render_playpen(&mut content, p);
}
// Render markdown using the pulldown-cmark crate // Render markdown using the pulldown-cmark crate
helpers::playpen::render_playpen(&mut content);
content = utils::render_markdown(&content); content = utils::render_markdown(&content);
print_content.push_str(&content); print_content.push_str(&content);

View File

@ -1,9 +1,26 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::fs::File;
use std::io::Read;
pub fn render_playpen(s: &mut str) { pub fn render_playpen(s: &mut str, path: &Path) {
// When replacing one thing in a string by something with a different length, the indices
// after that will not correspond, we therefore have to store the difference to correct this
let difference_index = 0;
for playpen in find_playpens(s) { for playpen in find_playpens(s, path) {
println!("Playpen{{ {}, {}, {:?}, {} }}", playpen.start_index, playpen.end_index, playpen.rust_file, playpen.editable);
// Check if the file exists
if !playpen.rust_file.exists() || !playpen.rust_file.is_file() {
output!("[-] No file exists for {{{{#playpen }}}}\n {}", playpen.rust_file.to_str().unwrap());
continue
}
// Open file & read file
let mut file = if let Ok(f) = File::open(&playpen.rust_file) { f } else { continue };
let mut file_content = String::new();
if let Err(_) = file.read_to_string(&mut file_content) { continue };
//println!("Playpen{{ {}, {}, {:?}, {} }}", playpen.start_index, playpen.end_index, playpen.rust_file, playpen.editable);
} }
} }
@ -16,21 +33,21 @@ struct Playpen{
editable: bool editable: bool
} }
fn find_playpens(s: &str) -> Vec<Playpen> { fn find_playpens(s: &str, base_path: &Path) -> Vec<Playpen> {
let mut playpens = vec![]; let mut playpens = vec![];
for (i, _) in s.match_indices("{{#playpen") { for (i, _) in s.match_indices("{{#playpen") {
println!("[*]: find_playpen"); debug!("[*]: find_playpen");
// DON'T forget the "+ i" else you have an index out of bounds error !! // DON'T forget the "+ i" else you have an index out of bounds error !!
let end_i = if let Some(n) = s[i..].find("}}") { n } else { continue } + i + 2; let end_i = if let Some(n) = s[i..].find("}}") { n } else { continue } + i + 2;
println!("s[{}..{}] = {}", i, end_i, s[i..end_i].to_string()); debug!("s[{}..{}] = {}", i, end_i, s[i..end_i].to_string());
// If there is nothing between "{{#playpen" and "}}" skip // If there is nothing between "{{#playpen" and "}}" skip
if end_i-2 - (i+10) < 1 { continue } if end_i-2 - (i+10) < 1 { continue }
if s[i+10..end_i-2].trim().len() == 0 { continue } if s[i+10..end_i-2].trim().len() == 0 { continue }
println!("{}", s[i+10..end_i-2].to_string()); debug!("{}", s[i+10..end_i-2].to_string());
// Split on whitespaces // Split on whitespaces
let params: Vec<&str> = s[i+10..end_i-2].split_whitespace().collect(); let params: Vec<&str> = s[i+10..end_i-2].split_whitespace().collect();
@ -44,7 +61,7 @@ fn find_playpens(s: &str) -> Vec<Playpen> {
Playpen{ Playpen{
start_index: i as u32, start_index: i as u32,
end_index: end_i as u32, end_index: end_i as u32,
rust_file: PathBuf::from(params[0]), rust_file: base_path.join(PathBuf::from(params[0])),
editable: editable, editable: editable,
} }
) )
@ -64,28 +81,28 @@ fn find_playpens(s: &str) -> Vec<Playpen> {
#[test] #[test]
fn test_find_playpens_no_playpen() { fn test_find_playpens_no_playpen() {
let s = "Some random text without playpen..."; let s = "Some random text without playpen...";
assert!(find_playpens(s) == vec![]); assert!(find_playpens(s, Path::new("")) == vec![]);
} }
#[test] #[test]
fn test_find_playpens_partial_playpen() { fn test_find_playpens_partial_playpen() {
let s = "Some random text with {{#playpen..."; let s = "Some random text with {{#playpen...";
assert!(find_playpens(s) == vec![]); assert!(find_playpens(s, Path::new("")) == vec![]);
} }
#[test] #[test]
fn test_find_playpens_empty_playpen() { fn test_find_playpens_empty_playpen() {
let s = "Some random text with {{#playpen}} and {{#playpen }}..."; let s = "Some random text with {{#playpen}} and {{#playpen }}...";
assert!(find_playpens(s) == vec![]); assert!(find_playpens(s, Path::new("")) == vec![]);
} }
#[test] #[test]
fn test_find_playpens_simple_playpen() { fn test_find_playpens_simple_playpen() {
let s = "Some random text with {{#playpen file.rs}} and {{#playpen test.rs }}..."; let s = "Some random text with {{#playpen file.rs}} and {{#playpen test.rs }}...";
println!("\nOUTPUT: {:?}\n", find_playpens(s)); println!("\nOUTPUT: {:?}\n", find_playpens(s, Path::new("")));
assert!(find_playpens(s) == vec![ assert!(find_playpens(s, Path::new("")) == vec![
Playpen{start_index: 22, end_index: 42, rust_file: PathBuf::from("file.rs"), editable: false}, Playpen{start_index: 22, end_index: 42, rust_file: PathBuf::from("file.rs"), editable: false},
Playpen{start_index: 47, end_index: 68, rust_file: PathBuf::from("test.rs"), editable: false} Playpen{start_index: 47, end_index: 68, rust_file: PathBuf::from("test.rs"), editable: false}
]); ]);
@ -95,10 +112,10 @@ fn test_find_playpens_simple_playpen() {
fn test_find_playpens_complex_playpen() { fn test_find_playpens_complex_playpen() {
let s = "Some random text with {{#playpen file.rs editable}} and {{#playpen test.rs editable }}..."; let s = "Some random text with {{#playpen file.rs editable}} and {{#playpen test.rs editable }}...";
println!("\nOUTPUT: {:?}\n", find_playpens(s)); println!("\nOUTPUT: {:?}\n", find_playpens(s, Path::new("dir")));
assert!(find_playpens(s) == vec![ assert!(find_playpens(s, Path::new("dir")) == vec![
Playpen{start_index: 22, end_index: 51, rust_file: PathBuf::from("file.rs"), editable: true}, Playpen{start_index: 22, end_index: 51, rust_file: PathBuf::from("dir/file.rs"), editable: true},
Playpen{start_index: 56, end_index: 86, rust_file: PathBuf::from("test.rs"), editable: true} Playpen{start_index: 56, end_index: 86, rust_file: PathBuf::from("dir/test.rs"), editable: true}
]); ]);
} }