mygit

[UNMAINTAINED] A cgit/webgit alternative, written in Rust
Log | Files | Refs | README | LICENSE

commit 7bf69cae74567bf2ab79270bcd75c7023fc6d39b
parent e05b03713e6b5c9a39421e136c90f0e3b1350462
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat, 13 Mar 2021 12:21:37 -0800

Add README parsing

Diffstat:
Msrc/main.rs | 15+++++++++++----
Mtemplates/repo-navbar.html | 1+
Mtemplates/repo.html | 2+-
3 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -2,10 +2,11 @@ use askama::Template; use git2::Repository; use once_cell::sync::OnceCell; use pico_args; +use pulldown_cmark::{html, Options, Parser}; use serde::{Deserialize, Serialize}; use std::fs; use std::path::Path; -use std::time::Instant; +use std::str; use tide::prelude::*; use tide::Request; @@ -50,7 +51,7 @@ async fn index(req: Request<()>) -> tide::Result { #[derive(Template)] #[template(path = "repo.html")] // using the template in this path, relative struct RepoHomeTemplate<'a> { - repo: Repository, + repo: &'a Repository, readme_text: &'a str, config: &'a Config, } @@ -60,9 +61,15 @@ async fn repo_home(req: Request<()>) -> tide::Result { let repo_path = Path::new(&config.repo_directory).join(req.param("repo_name")?); // TODO CLEAN PATH! VERY IMPORTANT! DONT FORGET! let repo = Repository::open(repo_path)?; + let readme = &repo.revparse_single("HEAD:README.md")?; // TODO allow more incl plaintext + let markdown_input = str::from_utf8(readme.as_blob().unwrap().content())?; + let mut options = Options::empty(); + let parser = Parser::new_ext(markdown_input, options); + let mut html_output = String::new(); + html::push_html(&mut html_output, parser); let tmpl = RepoHomeTemplate { - repo, - readme_text: "Hello world", + repo: &repo, + readme_text: &html_output, config, }; Ok(tmpl.into()) diff --git a/templates/repo-navbar.html b/templates/repo-navbar.html @@ -2,3 +2,4 @@ <div class="repo-description">My cool repo</div> <div class="clone-url">git clone git.alexwennerberg.com/repo </div> <div class="navbar"><a href="/{{name}}">README</a> | <a href="/{{name}}/tree">tree</a> | <a href="/{{name}}/log">log</a> | <a href="/{{name}}/refs">refs</a></div> +<hr class='thin'> diff --git a/templates/repo.html b/templates/repo.html @@ -3,5 +3,5 @@ {% block content %} {% let name = repo.workdir().unwrap().file_name().unwrap().to_str().unwrap() %} {% include "repo-navbar.html" %} - Readme text goes here + {{ readme_text|safe }} {% endblock %}