mygit

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

commit e427dfba6fca56609463c5b7850bceb3f18fdd03
parent 156ce3e61dafa5e5582975b2559c04709e1e7a3f
Author: Johann150 <johann@qwertqwefsday.eu>
Date:   Wed, 17 Mar 2021 22:44:20 +0100

handle repos without readme

Diffstat:
Msrc/main.rs | 77++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mtemplates/repo.html | 6+++++-
2 files changed, 45 insertions(+), 38 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -147,44 +147,47 @@ async fn repo_home(req: Request<()>) -> tide::Result { return Ok(RepoEmptyTemplate { repo }.into()); } - let readme_text = { - let mut format = ReadmeFormat::Plaintext; - let readme = repo - .revparse_single("HEAD:README") - .or_else(|_| repo.revparse_single("HEAD:README.txt")) - .or_else(|_| { - format = ReadmeFormat::Markdown; - repo.revparse_single("HEAD:README.md") - }) - .or_else(|_| repo.revparse_single("HEAD:README.mdown")) - .or_else(|_| repo.revparse_single("HEAD:README.markdown")) - .or_else(|_| { - format = ReadmeFormat::Html; - repo.revparse_single("HEAD:README.html") - }) - .or_else(|_| repo.revparse_single("HEAD:README.htm"))?; - let readme_text = str::from_utf8(readme.as_blob().unwrap().content())?; - - // render the file contents to HTML - match format { - // render plaintext as preformatted text - ReadmeFormat::Plaintext => { - let mut output = "<pre>".to_string(); - escape_html(&mut output, readme_text)?; - output.push_str("</pre>"); - output - } - // already is HTML - ReadmeFormat::Html => readme_text.to_string(), - // render Markdown to HTML - ReadmeFormat::Markdown => { - let mut output = String::new(); - let parser = Parser::new_ext(readme_text, Options::empty()); - push_html(&mut output, parser); - output + let mut format = ReadmeFormat::Plaintext; + let readme_text = repo + .revparse_single("HEAD:README") + .or_else(|_| repo.revparse_single("HEAD:README.txt")) + .or_else(|_| { + format = ReadmeFormat::Markdown; + repo.revparse_single("HEAD:README.md") + }) + .or_else(|_| repo.revparse_single("HEAD:README.mdown")) + .or_else(|_| repo.revparse_single("HEAD:README.markdown")) + .or_else(|_| { + format = ReadmeFormat::Html; + repo.revparse_single("HEAD:README.html") + }) + .or_else(|_| repo.revparse_single("HEAD:README.htm")) + .ok() + .and_then(|readme| readme.into_blob().ok()) + .map(|blob| { + let text = str::from_utf8(blob.content()).unwrap_or_default(); + + // render the file contents to HTML + match format { + // render plaintext as preformatted text + ReadmeFormat::Plaintext => { + let mut output = "<pre>".to_string(); + escape_html(&mut output, text).unwrap(); + output.push_str("</pre>"); + output + } + // already is HTML + ReadmeFormat::Html => text.to_string(), + // render Markdown to HTML + ReadmeFormat::Markdown => { + let mut output = String::new(); + let parser = Parser::new_ext(text, Options::empty()); + push_html(&mut output, parser); + output + } } - } - }; + }) + .unwrap_or_default(); Ok(RepoHomeTemplate { repo, readme_text }.into()) } diff --git a/templates/repo.html b/templates/repo.html @@ -2,5 +2,9 @@ {% block content %} {% include "repo-navbar.html" %} - {{ readme_text|safe }} + {% if !readme_text.is_empty() -%} + {{ readme_text|safe }} + {% else -%} + <em>(no README)</em> + {%- endif %} {% endblock %}