Skip to content

Commit

Permalink
Continue #29, Rust files can now be loaded with {{#playpen file.rs}},…
Browse files Browse the repository at this point in the history
… they will be displayed as other code snippets included with markdown backticks except they have a playpen css class
  • Loading branch information
azerupi committed Dec 31, 2015
1 parent 38b2dee commit 0ac0301
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Renderer for HtmlHandlebars {

// Parse for playpen links
if let Some(p) = path.parent() {
helpers::playpen::render_playpen(&mut content, p);
content = helpers::playpen::render_playpen(&content, p);
}

// Render markdown using the pulldown-cmark crate
Expand Down
24 changes: 18 additions & 6 deletions src/renderer/html_handlebars/helpers/playpen.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
extern crate handlebars;

use std::path::{Path, PathBuf};
use std::fs::File;
use std::io::Read;

pub fn render_playpen(s: &mut str, path: &Path) {

pub fn render_playpen(s: &str, path: &Path) -> String {
// 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;
let mut previous_end_index = 0;
let mut replaced = String::new();

for playpen in find_playpens(s, path) {

Expand All @@ -20,15 +24,23 @@ pub fn render_playpen(s: &mut str, path: &Path) {
let mut file_content = String::new();
if let Err(_) = file.read_to_string(&mut file_content) { continue };

let replacement = String::new() + "<pre class=\"playpen\"><code class=\"language-rust\">" + &file_content + "</code></pre>";

replaced.push_str(&s[previous_end_index..playpen.start_index]);
replaced.push_str(&replacement);
previous_end_index = playpen.end_index;
//println!("Playpen{{ {}, {}, {:?}, {} }}", playpen.start_index, playpen.end_index, playpen.rust_file, playpen.editable);
}

replaced.push_str(&s[previous_end_index..]);

replaced
}

#[derive(PartialOrd, PartialEq, Debug)]
struct Playpen{
start_index: u32,
end_index: u32,
start_index: usize,
end_index: usize,
rust_file: PathBuf,
editable: bool
}
Expand Down Expand Up @@ -59,8 +71,8 @@ fn find_playpens(s: &str, base_path: &Path) -> Vec<Playpen> {

playpens.push(
Playpen{
start_index: i as u32,
end_index: end_i as u32,
start_index: i,
end_index: end_i,
rust_file: base_path.join(PathBuf::from(params[0])),
editable: editable,
}
Expand Down

0 comments on commit 0ac0301

Please sign in to comment.