1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/**
* AltGit - altgit
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Paolo Lulli <kevwe.com>
* @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=<f>&hash=<h>&branch=<b>
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(
`<a href="/">repos</a> <span class="sep">/</span> <a href="/%s/">%s</a>`+
` <span class="sep">/</span> <a href="/%s/history?path=%s&branch=%s">%s</a>`+
` <span class="sep">/</span> <span style="color:var(--gold)">%s</span>`,
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(
`<a class="back-link" href="/%s/history?path=%s&branch=%s">← history for %s</a>`,
url.PathEscape(repoName), url.QueryEscape(path), url.QueryEscape(pd.Branch),
template.HTMLEscapeString(path),
))
if err != nil {
body.WriteString(fmt.Sprintf(
`<div class="empty">⚠ Could not retrieve file at %s: %s</div>`,
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(
`<div class="code-meta">`+
`<span class="ref-hash">%s</span>`+
`<strong>%s</strong>`+
`<span style="flex:1"></span>`+
`<span class="muted-text">%d lines</span>`+
`<a class="raw-btn" href="%s" target="_blank">⬡ raw</a>`+
`<a class="raw-btn dl" href="%s" download="%s">↓ download</a>`+
`</div>`+
`<div class="code-wrap chroma-wrap">`,
template.HTMLEscapeString(short),
template.HTMLEscapeString(path),
len(lines), rawURL, dlURL,
template.HTMLEscapeString(filename),
))
body.WriteString(rows)
body.WriteString("</div>")
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(`<div class="readme-box">`)
body.WriteString(`<div class="readme-header">`)
body.WriteString(`<span class="file-icon">📄</span> README.md`)
body.WriteString(`</div>`)
body.WriteString(`<div class="readme-body">`)
body.WriteString(rendering.RenderMarkdown([]byte(content)))
body.WriteString(`</div></div>`)
}
}
pd.Title = fmt.Sprintf("%s @ %s — %s", path, short, repoName)
pd.GitCmd = cmd
pd.Body = template.HTML(body.String())
rendering.RenderPage(cfg, c, pd)
}
}
|