89109d2
use axum::extract::{Path, State};
use axum::response::Response;
use maud::html;
use crate::handler::html::{HtmlRepoPath, archived_class, format_size, resolve, response_html};
use crate::service::GitService;
use crate::share::access::BasicAuth;
#[utoipa::path(
get,
path = "/repos/{space_name}/{repo_name}/html/badge",
tag = "html",
description = "渲染仓库摘要徽章 HTML 片段。",
operation_id = "render_badge",
params(HtmlRepoPath),
responses(
(status = 200, description = "repo badge html fragment", content_type = "text/html"),
(status = 404, description = "repo not found")
)
)]
pub async fn render_badge(
State(service): State<GitService>,
Path(path): Path<HtmlRepoPath>,
auth: BasicAuth,
) -> Response {
let ctx = match resolve(service, path, auth).await {
Ok(c) => c,
Err(resp) => return resp,
};
let base = "git-badge inline-flex items-center gap-2 rounded-md bg-zinc-100 px-2 py-0.5 text-xs text-zinc-700";
let cls = archived_class(&ctx.dto, base);
let size_str = format_size(ctx.dto.size.max(0) as u64);
let markup = html! {
div class=(cls)
data-component="git-badge"
data-space=(ctx.space_name)
data-repo=(ctx.repo_name)
data-default-branch=(ctx.dto.default_branch)
data-commits=(ctx.dto.commits_count)
data-size=(ctx.dto.size)
data-archived=(if ctx.dto.is_archived { "true" } else { "false" }) {
span class="rounded bg-white px-1.5 py-0.5 font-mono text-[10px]" {
(ctx.dto.default_branch)
}
span class="flex items-center gap-1" {
svg class="h-3 w-3" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" {
path d="M11.93 8.5a4.002 4.002 0 01-7.86 0H.75a.75.75 0 010-1.5h3.32a4.002 4.002 0 017.86 0h3.32a.75.75 0 010 1.5zm-1.43-.75a2.5 2.5 0 10-5 0 2.5 2.5 0 005 0z" {}
}
span class="font-mono" { (ctx.dto.commits_count) }
span { "commits" }
}
span class="flex items-center gap-1" {
svg class="h-3 w-3" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" {
path d="M2 2.5A2.5 2.5 0 014.5 0h8.75a.75.75 0 01.75.75v12.5a.75.75 0 01-.75.75h-2.5a.75.75 0 110-1.5h1.75v-2h-8a1 1 0 00-.714 1.7.75.75 0 01-1.072 1.05A2.495 2.495 0 012 11.5zm10.5-1V9h-8c-.356 0-.694.074-1 .208V2.5a1 1 0 011-1h8zM5 12.25v3.25a.25.25 0 00.4.2l1.45-1.087a.25.25 0 01.3 0L8.6 15.7a.25.25 0 00.4-.2v-3.25a.25.25 0 00-.25-.25h-3.5a.25.25 0 00-.25.25z" {}
}
span class="font-mono" { (size_str) }
}
}
};
response_html(markup, 30)
}