git.rs: cleaning; render.rs: refactor to fit new git.rs library; test_render: initial commit; assets/*: update templates

This commit is contained in:
2025-08-24 18:29:59 -06:00
parent 7265642b8b
commit 806a9aebca
6 changed files with 215 additions and 86 deletions

View File

@@ -16,56 +16,50 @@
*
* You should have received a copy of the GNU Affero General Public License
* along with Mintee. If not, see https://www.gnu.org/licenses/.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*/
use std::path::PathBuf;
use std::{
error::Error,
fmt::{ self, Display, Formatter },
path::Path,
};
use git2::Repository;
use tera::{ Context, Tera };
trait ToContext {
fn to_context(self) -> Result<Context, git2::Error>;
#[non_exhaustive]
pub enum PageKind {
Code,
Dashboard,
Tickets,
User,
}
// TODO: move this data into backend.rs
impl ToContext for Repository {
fn to_context(self) -> Result<Context, git2::Error> {
let repo = self.commondir();
let _index = self.index()?;
let head = self.head()?;
let branch = if head.is_branch() {
head.shorthand().unwrap()
} else { "detached" };
let head_commit = head.peel_to_commit()?;
let entries = get_entries(&self)?;
let committer = head_commit.committer().name().unwrap().to_owned();
let _author = head_commit.author().name().unwrap().to_owned();
impl PageKind {
pub fn render_page(&self, ctx: Context) -> Result<String, Box<dyn Error>> {
let page_dir = self.to_string();
let template = String::from_utf8(Path::new(&page_dir)
.to_path_buf()
.as_mut_os_string()
.as_encoded_bytes()
.to_vec()
)?;
let mut ctx = Context::new();
// stub until we have database
ctx.insert("user", "anon");
ctx.insert("site", "TiB.");
ctx.insert("notif_count", "");
ctx.insert("ticket_count", "(47)");
ctx.insert("owner", &committer);
ctx.insert("repo", repo);
ctx.insert("branch", &branch);
ctx.insert("directory", repo);
ctx.insert("entries", &entries);
ctx.insert("readme_content", "this is a readme");
Ok(ctx)
let tera = Tera::new(&page_dir)?;
Ok(tera.render(&template, &ctx)?)
}
}
fn render_path(path: PathBuf) -> tera::Result<String> {
let tera = Tera::new("./assets/templates/repo/*")?;
let repo = Repository::discover(path).unwrap();
let context = repo.to_context().unwrap();
impl Display for PageKind {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
use PageKind::*;
tera.render("code.html", &context)
let path = match self {
Code => "repo/code.html",
Dashboard => "dashboard.html",
Tickets => "repo/tickets.html",
User => "user.html",
};
write!(f, "./assets/templates/{}", path)
}
}