Repositories

grarr

(mirrored on github)

Wim Looman <wim@nemo157.com>
68a1b9 Render .md files for pages requests
Wim Looman committed at 2016-03-11 13:20:14

Modified src/handler/pages.rs

@@ -1,11 +1,46 @@
use super::base::*;
use tree_entry;
use std::str;
use pulldown_cmark;
use git2;
#[derive(Clone)]
pub struct Pages;
fn get_response(context: &RepositoryContext, path: &str) -> Result<Response, ()> {
let entry = try!(tree_entry::get_tree_entry(&context, &path).map_err(|_| ())).entry;
match entry.kind() {
Some(git2::ObjectType::Blob) => {
let blob = entry.as_blob().unwrap();
Ok(Response::with((status::Ok, utils::blob_mime(blob, &path), blob.content())))
},
_ => Err(()),
}
}
fn get_markdown_response(context: &RepositoryContext, path: &str) -> Result<Response, ()> {
if !path.ends_with(".html") {
return Err(());
}
let md_path = path[..path.len()-5].to_owned() + ".md";
let entry = try!(tree_entry::get_tree_entry(&context, &md_path).map_err(|_| ())).entry;
match entry.kind() {
Some(git2::ObjectType::Blob) => {
let blob = entry.as_blob().unwrap();
let content = try!(str::from_utf8(blob.content()).map_err(|_| ()));
let mut buffer = String::with_capacity(content.len() * 3 / 2);
let parser = pulldown_cmark::Parser::new(&content);
pulldown_cmark::html::push_html(&mut buffer, parser);
Ok(Response::with((status::Ok, mime!(Text/Html), buffer)))
},
_ => Err(()),
}
}
impl Handler for Pages {
fn handle(&self, req: &mut Request) -> IronResult<Response> {
{
@@ -20,17 +55,9 @@ impl Handler for Pages {
path = path + "index.html";
}
let entry = try!(tree_entry::get_tree_entry(&context, &path)).entry;
match entry.kind() {
Some(git2::ObjectType::Blob) => {
let blob = entry.as_blob().unwrap();
Ok(Response::with((status::Ok, utils::blob_mime(blob, &path), blob.content())))
},
_ => {
Err(IronError::new(Error::from("Not found"), status::NotFound))
},
}
get_response(context, &*path)
.or_else(|_| get_markdown_response(context, &*path))
.or_else(|_| Err(IronError::new(Error::from("Not found"), status::NotFound)))
}
}