Skip to content

Instantly share code, notes, and snippets.

@icefoxen
Created November 7, 2016 17:03
Show Gist options
  • Save icefoxen/0b62209f5979dcdee3decaa64c5e4aa7 to your computer and use it in GitHub Desktop.
Save icefoxen/0b62209f5979dcdee3decaa64c5e4aa7 to your computer and use it in GitHub Desktop.
fn get_page(req: &mut Request) -> IronResult<Response> {
let ref pagename = req.extensions
.get::<Router>()
.unwrap()
.find("page")
.unwrap_or("no query");
let mut pagepath = REPO_PATH.to_owned();
pagepath += pagename;
pagepath += ".md";
match fs::File::open(pagepath) {
Ok(file) => {
let md = hoedown::Markdown::read_from(file);
let mut html = hoedown::Html::new(hoedown::renderer::html::Flags::empty(), 0);
let buffer = html.render(&md);
let stringggggg = buffer.to_str().unwrap();
let pageinfo = PageInfo::new(stringggggg);
let json = rustc_serialize::json::encode(&pageinfo).unwrap();
// Prints out:
// JSON is: {"body":"<h1>Hello world!</h1>\n\n<p>This is a test markdown page</p>\n"}
println!("JSON is: {}", json);
// Ok(Response::with((status::Ok, stringggggg)));
let t = handlebars_iron::Template::new("page", json);
Ok(Response::with((status::Ok, t)))
}
Err(e) => {
let status = match e.kind() {
io::ErrorKind::NotFound => status::NotFound,
io::ErrorKind::PermissionDenied => status::Forbidden,
_ => status::InternalServerError,
};
Err(IronError::new(e, status))
}
}
}
// My template is:
// This is a template!
//
// Page body:
//
// {{ body }}
//
// The server returns:
// $ curl http://localhost:8080/start
// This is a template!
//
// Page body:
//
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment