From 1fc46e3c0bd0075f6ca9eeb50eecad10dbf767b8 Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Thu, 22 Apr 2021 05:08:11 -0600 Subject: actually render the readme --- src/main.rs | 20 +++++--------------- src/templates.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 src/templates.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index dd5fea0..0710d40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,13 @@ +use std::ops::Deref; use std::path::Path; -use askama::Template; use async_std::sync::{Arc, Mutex}; use eyre::Result; use tide::prelude::*; mod state; +mod templates; use state::State; -use std::ops::Deref; - -#[derive(Template)] -#[template(path = "index.html")] -struct IndexTemplate<'a> { - state: &'a State, -} #[async_std::main] async fn main() -> Result<()> { @@ -52,7 +46,7 @@ type Request = tide::Request>>; async fn index(req: Request) -> tide::Result { let state = req.state(); let state = state.lock_arc().await; - Ok(IndexTemplate { state: state.deref() }.into()) + Ok(templates::Index { state: state.deref() }.into()) } async fn about(req: Request) -> tide::Result { @@ -96,10 +90,6 @@ async fn about(req: Request) -> tide::Result { Err(_) => return Ok("somehow that README wasn't a file".into()), }; - let body = if head_readme_blob.is_binary() { - tide::Body::from(head_readme_blob.content()) - } else { - tide::Body::from(String::from_utf8_lossy(head_readme_blob.content()).into_owned()) - }; - Ok(body.into()) + let readme = String::from_utf8_lossy(head_readme_blob.content()); + Ok(templates::About { path: repo_rel_path, readme }.into()) } diff --git a/src/templates.rs b/src/templates.rs new file mode 100644 index 0000000..bb285fa --- /dev/null +++ b/src/templates.rs @@ -0,0 +1,35 @@ +use std::borrow::Cow; + +use askama::Template; + +use crate::state::State; + +#[derive(Template)] +#[template(path = "index.html")] +pub struct Index<'a> { + pub state: &'a State, +} + +#[derive(Template)] +#[template(path = "about.html")] +pub struct About<'a> { + pub path: &'a str, + pub readme: Cow<'a, str>, +} + +mod filters { + pub fn markdown(s: &str) -> ::askama::Result { + use pulldown_cmark::{Parser, Options, html}; + + let mut options = Options::empty(); + options.insert(Options::ENABLE_STRIKETHROUGH); + options.insert(Options::ENABLE_SMART_PUNCTUATION); + let parser = Parser::new_ext(s, options); + + let mut result = String::new(); + html::push_html(&mut result, parser); + + // TODO mark as safe automatically + Ok(result) + } +} -- cgit v1.2.3