Skip to content

Commit 38b2dee

Browse files
committed
Continue #29 Check that the rust file exists and read to string
1 parent 0cb234d commit 38b2dee

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ impl Renderer for HtmlHandlebars {
7171
debug!("[*]: Reading file");
7272
try!(f.read_to_string(&mut content));
7373

74+
// Parse for playpen links
75+
if let Some(p) = path.parent() {
76+
helpers::playpen::render_playpen(&mut content, p);
77+
}
78+
7479
// Render markdown using the pulldown-cmark crate
75-
helpers::playpen::render_playpen(&mut content);
7680
content = utils::render_markdown(&content);
7781
print_content.push_str(&content);
7882

src/renderer/html_handlebars/helpers/playpen.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
use std::path::{Path, PathBuf};
2+
use std::fs::File;
3+
use std::io::Read;
24

3-
pub fn render_playpen(s: &mut str) {
5+
pub fn render_playpen(s: &mut str, path: &Path) {
6+
// When replacing one thing in a string by something with a different length, the indices
7+
// after that will not correspond, we therefore have to store the difference to correct this
8+
let difference_index = 0;
49

5-
for playpen in find_playpens(s) {
6-
println!("Playpen{{ {}, {}, {:?}, {} }}", playpen.start_index, playpen.end_index, playpen.rust_file, playpen.editable);
10+
for playpen in find_playpens(s, path) {
11+
12+
// Check if the file exists
13+
if !playpen.rust_file.exists() || !playpen.rust_file.is_file() {
14+
output!("[-] No file exists for {{{{#playpen }}}}\n {}", playpen.rust_file.to_str().unwrap());
15+
continue
16+
}
17+
18+
// Open file & read file
19+
let mut file = if let Ok(f) = File::open(&playpen.rust_file) { f } else { continue };
20+
let mut file_content = String::new();
21+
if let Err(_) = file.read_to_string(&mut file_content) { continue };
22+
23+
//println!("Playpen{{ {}, {}, {:?}, {} }}", playpen.start_index, playpen.end_index, playpen.rust_file, playpen.editable);
724
}
825

926
}
@@ -16,21 +33,21 @@ struct Playpen{
1633
editable: bool
1734
}
1835

19-
fn find_playpens(s: &str) -> Vec<Playpen> {
36+
fn find_playpens(s: &str, base_path: &Path) -> Vec<Playpen> {
2037
let mut playpens = vec![];
2138
for (i, _) in s.match_indices("{{#playpen") {
22-
println!("[*]: find_playpen");
39+
debug!("[*]: find_playpen");
2340

2441
// DON'T forget the "+ i" else you have an index out of bounds error !!
2542
let end_i = if let Some(n) = s[i..].find("}}") { n } else { continue } + i + 2;
2643

27-
println!("s[{}..{}] = {}", i, end_i, s[i..end_i].to_string());
44+
debug!("s[{}..{}] = {}", i, end_i, s[i..end_i].to_string());
2845

2946
// If there is nothing between "{{#playpen" and "}}" skip
3047
if end_i-2 - (i+10) < 1 { continue }
3148
if s[i+10..end_i-2].trim().len() == 0 { continue }
3249

33-
println!("{}", s[i+10..end_i-2].to_string());
50+
debug!("{}", s[i+10..end_i-2].to_string());
3451

3552
// Split on whitespaces
3653
let params: Vec<&str> = s[i+10..end_i-2].split_whitespace().collect();
@@ -44,7 +61,7 @@ fn find_playpens(s: &str) -> Vec<Playpen> {
4461
Playpen{
4562
start_index: i as u32,
4663
end_index: end_i as u32,
47-
rust_file: PathBuf::from(params[0]),
64+
rust_file: base_path.join(PathBuf::from(params[0])),
4865
editable: editable,
4966
}
5067
)
@@ -64,28 +81,28 @@ fn find_playpens(s: &str) -> Vec<Playpen> {
6481
#[test]
6582
fn test_find_playpens_no_playpen() {
6683
let s = "Some random text without playpen...";
67-
assert!(find_playpens(s) == vec![]);
84+
assert!(find_playpens(s, Path::new("")) == vec![]);
6885
}
6986

7087
#[test]
7188
fn test_find_playpens_partial_playpen() {
7289
let s = "Some random text with {{#playpen...";
73-
assert!(find_playpens(s) == vec![]);
90+
assert!(find_playpens(s, Path::new("")) == vec![]);
7491
}
7592

7693
#[test]
7794
fn test_find_playpens_empty_playpen() {
7895
let s = "Some random text with {{#playpen}} and {{#playpen }}...";
79-
assert!(find_playpens(s) == vec![]);
96+
assert!(find_playpens(s, Path::new("")) == vec![]);
8097
}
8198

8299
#[test]
83100
fn test_find_playpens_simple_playpen() {
84101
let s = "Some random text with {{#playpen file.rs}} and {{#playpen test.rs }}...";
85102

86-
println!("\nOUTPUT: {:?}\n", find_playpens(s));
103+
println!("\nOUTPUT: {:?}\n", find_playpens(s, Path::new("")));
87104

88-
assert!(find_playpens(s) == vec![
105+
assert!(find_playpens(s, Path::new("")) == vec![
89106
Playpen{start_index: 22, end_index: 42, rust_file: PathBuf::from("file.rs"), editable: false},
90107
Playpen{start_index: 47, end_index: 68, rust_file: PathBuf::from("test.rs"), editable: false}
91108
]);
@@ -95,10 +112,10 @@ fn test_find_playpens_simple_playpen() {
95112
fn test_find_playpens_complex_playpen() {
96113
let s = "Some random text with {{#playpen file.rs editable}} and {{#playpen test.rs editable }}...";
97114

98-
println!("\nOUTPUT: {:?}\n", find_playpens(s));
115+
println!("\nOUTPUT: {:?}\n", find_playpens(s, Path::new("dir")));
99116

100-
assert!(find_playpens(s) == vec![
101-
Playpen{start_index: 22, end_index: 51, rust_file: PathBuf::from("file.rs"), editable: true},
102-
Playpen{start_index: 56, end_index: 86, rust_file: PathBuf::from("test.rs"), editable: true}
117+
assert!(find_playpens(s, Path::new("dir")) == vec![
118+
Playpen{start_index: 22, end_index: 51, rust_file: PathBuf::from("dir/file.rs"), editable: true},
119+
Playpen{start_index: 56, end_index: 86, rust_file: PathBuf::from("dir/test.rs"), editable: true}
103120
]);
104121
}

0 commit comments

Comments
 (0)