/** * AltGit - altgit * * This file is licensed under the Affero General Public License version 3 or * later. See the COPYING file. * * @author Paolo Lulli * @copyright Paolo Lulli 2026 */ package handler import ( "altgit/internal/config" "altgit/internal/gitrepo" "altgit/internal/rendering" "fmt" "html/template" "net/http" "net/url" "strings" "github.com/gin-gonic/gin" ) // HandleView GET /:repo/view?path=&hash=&branch= func HandleView(cfg config.AltGitConfiguration) gin.HandlerFunc { return func(c *gin.Context) { repoName, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo")) if !ok { return } path := c.Query("path") hash := c.Query("hash") if path == "" || hash == "" { c.Redirect(http.StatusFound, "/"+repoName+"/") return } short := hash if len(short) > 8 { short = short[:8] } pd := rendering.BasePageData(c, repoName, rp, "files", template.HTML(fmt.Sprintf( `repos / %s`+ ` / %s`+ ` / %s`, url.PathEscape(repoName), template.HTMLEscapeString(repoName), url.PathEscape(repoName), url.QueryEscape(path), url.QueryEscape(rendering.ActiveBranch(c, "")), template.HTMLEscapeString(path), template.HTMLEscapeString(short), ))) content, cmd, err := gitrepo.FileAtCommit(rp, hash, path) var body strings.Builder body.WriteString(fmt.Sprintf( `← history for %s`, url.PathEscape(repoName), url.QueryEscape(path), url.QueryEscape(pd.Branch), template.HTMLEscapeString(path), )) if err != nil { body.WriteString(fmt.Sprintf( `
⚠ Could not retrieve file at %s: %s
`, template.HTMLEscapeString(short), template.HTMLEscapeString(err.Error()), )) } else { lines := strings.Split(content, "\n") rawURL := fmt.Sprintf("/%s/raw?path=%s&hash=%s", url.PathEscape(repoName), url.QueryEscape(path), url.QueryEscape(hash)) dlURL := rawURL + "&dl=1" filename := path[strings.LastIndex(path, "/")+1:] is_markdown := strings.HasSuffix(filename, ".md") if !is_markdown { codeStyle := "onedark" if cfg.CodeStyle != "" { codeStyle = cfg.CodeStyle } rows, chromaCSS := rendering.HighlightCode(content, filename, codeStyle) body.WriteString(fmt.Sprintf( `
`+ `%s`+ `%s`+ ``+ `%d lines`+ `⬡ raw`+ `↓ download`+ `
`+ `
`, template.HTMLEscapeString(short), template.HTMLEscapeString(path), len(lines), rawURL, dlURL, template.HTMLEscapeString(filename), )) body.WriteString(rows) body.WriteString("
") pd.ChromaCSS = template.CSS(chromaCSS) } else { _, rp, ok := rendering.ResolveRepo(cfg, c.Param("repo")) if !ok { return } content, _, _ := gitrepo.FileAtCommit(rp, hash, path) fmt.Println("Rendering markdown!!!") body.WriteString(`
`) body.WriteString(`
`) body.WriteString(`📄 README.md`) body.WriteString(`
`) body.WriteString(`
`) body.WriteString(rendering.RenderMarkdown([]byte(content))) body.WriteString(`
`) } } pd.Title = fmt.Sprintf("%s @ %s — %s", path, short, repoName) pd.GitCmd = cmd pd.Body = template.HTML(body.String()) rendering.RenderPage(cfg, c, pd) } }