aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMelody Horn <melody@boringcactus.com>2021-04-22 05:08:11 -0600
committerMelody Horn <melody@boringcactus.com>2021-04-22 05:08:11 -0600
commit1fc46e3c0bd0075f6ca9eeb50eecad10dbf767b8 (patch)
tree9680e67b5d592fe803981784f9dbc8da3f469717
parent7c0daee1843e36c4c204c653a2ce010e4e0f98b2 (diff)
downloadgityeet-1fc46e3c0bd0075f6ca9eeb50eecad10dbf767b8.tar.gz
gityeet-1fc46e3c0bd0075f6ca9eeb50eecad10dbf767b8.zip
actually render the readme
-rw-r--r--Cargo.lock31
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs20
-rw-r--r--src/templates.rs35
-rw-r--r--templates/about.html13
5 files changed, 85 insertions, 15 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 434044d..5cb5769 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -817,6 +817,15 @@ dependencies = [
]
[[package]]
+name = "getopts"
+version = "0.2.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5"
+dependencies = [
+ "unicode-width",
+]
+
+[[package]]
name = "getrandom"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -873,6 +882,7 @@ dependencies = [
"clap",
"eyre",
"git2",
+ "pulldown-cmark",
"tide",
]
@@ -1308,6 +1318,18 @@ dependencies = [
]
[[package]]
+name = "pulldown-cmark"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ffade02495f22453cd593159ea2f59827aae7f53fa8323f756799b670881dcf8"
+dependencies = [
+ "bitflags",
+ "getopts",
+ "memchr",
+ "unicase",
+]
+
+[[package]]
name = "quote"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1794,6 +1816,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06"
[[package]]
+name = "unicase"
+version = "2.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
+dependencies = [
+ "version_check",
+]
+
+[[package]]
name = "unicode-bidi"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index a0607dd..f9645e9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,4 +13,5 @@ async-std = { version = "1.8.0", features = ["attributes"] }
clap = "2.33.3"
eyre = "0.6.5"
git2 = "0.13.18"
+pulldown-cmark = "0.8.0"
tide = "0.16.0"
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<Arc<Mutex<State>>>;
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<String> {
+ 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)
+ }
+}
diff --git a/templates/about.html b/templates/about.html
new file mode 100644
index 0000000..977d916
--- /dev/null
+++ b/templates/about.html
@@ -0,0 +1,13 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <meta name="viewport"
+ content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
+ <meta http-equiv="X-UA-Compatible" content="ie=edge">
+ <title>{{ path }}</title>
+</head>
+<body>
+{{ readme|markdown|safe }}
+</body>
+</html>