add raw file routes, link to raw files on file view, add relative link processing to readme code
This commit is contained in:
commit
239d5b2dfa
6 changed files with 47 additions and 14 deletions
|
@ -76,7 +76,7 @@ func (g *GitRepo) LastCommit() (*object.Commit, error) {
|
|||
return c, nil
|
||||
}
|
||||
|
||||
func (g *GitRepo) FileContent(path string) (string, error) {
|
||||
func (g *GitRepo) FileContent(path string, showBinary bool) (string, error) {
|
||||
c, err := g.r.CommitObject(g.h)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("commit object: %w", err)
|
||||
|
@ -94,7 +94,7 @@ func (g *GitRepo) FileContent(path string) (string, error) {
|
|||
|
||||
isbin, _ := file.IsBinary()
|
||||
|
||||
if !isbin {
|
||||
if showBinary || !isbin {
|
||||
return file.Contents()
|
||||
} else {
|
||||
return "Not displaying binary file", nil
|
||||
|
|
|
@ -41,6 +41,7 @@ func Handlers(c *config.Config) *flow.Mux {
|
|||
mux.HandleFunc("/static/:file", d.ServeStatic, "GET")
|
||||
mux.HandleFunc("/:name", d.Multiplex, "GET", "POST")
|
||||
mux.HandleFunc("/:name/tree/:ref/...", d.RepoTree, "GET")
|
||||
mux.HandleFunc("/:name/blob/raw/:ref/...", d.ServeStaticInRepo, "GET")
|
||||
mux.HandleFunc("/:name/blob/:ref/...", d.FileContent, "GET")
|
||||
mux.HandleFunc("/:name/log/:ref", d.Log, "GET")
|
||||
mux.HandleFunc("/:name/commit/:ref", d.Diff, "GET")
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
|
@ -97,6 +98,13 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
mainBranch, err := gr.FindMainBranch(d.c.Repo.MainBranch)
|
||||
if err != nil {
|
||||
d.Write500(w)
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
commits, err := gr.Commits()
|
||||
if err != nil {
|
||||
d.Write500(w)
|
||||
|
@ -107,7 +115,7 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
|
|||
var readmeContent template.HTML
|
||||
for _, readme := range d.c.Repo.Readme {
|
||||
ext := filepath.Ext(readme)
|
||||
content, _ := gr.FileContent(readme)
|
||||
content, _ := gr.FileContent(readme, false)
|
||||
if len(content) > 0 {
|
||||
switch ext {
|
||||
case ".md", ".mkd", ".markdown":
|
||||
|
@ -116,7 +124,7 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
|
|||
blackfriday.WithExtensions(blackfriday.CommonExtensions),
|
||||
)
|
||||
html := bluemonday.UGCPolicy().SanitizeBytes(unsafe)
|
||||
readmeContent = template.HTML(html)
|
||||
readmeContent = template.HTML(transformRelativeURLs(string(html), name, mainBranch))
|
||||
default:
|
||||
readmeContent = template.HTML(
|
||||
fmt.Sprintf(`<pre>%s</pre>`, content),
|
||||
|
@ -130,13 +138,6 @@ func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
|
|||
log.Printf("no readme found for %s", name)
|
||||
}
|
||||
|
||||
mainBranch, err := gr.FindMainBranch(d.c.Repo.MainBranch)
|
||||
if err != nil {
|
||||
d.Write500(w)
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
tpath := filepath.Join(d.c.Dirs.Templates, "*")
|
||||
t := template.Must(template.ParseGlob(tpath))
|
||||
|
||||
|
@ -212,12 +213,13 @@ func (d *deps) FileContent(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
contents, err := gr.FileContent(treePath)
|
||||
contents, err := gr.FileContent(treePath, false)
|
||||
data := make(map[string]any)
|
||||
data["name"] = name
|
||||
data["ref"] = ref
|
||||
data["desc"] = getDescription(path)
|
||||
data["path"] = treePath
|
||||
data["raw"] = fmt.Sprintf("/%s/blob/raw/%s/%s", name, ref, treePath)
|
||||
|
||||
d.showFile(contents, data, w)
|
||||
return
|
||||
|
@ -346,6 +348,26 @@ func (d *deps) Refs(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
func (d *deps) ServeStaticInRepo(w http.ResponseWriter, r *http.Request) {
|
||||
f := flow.Param(r.Context(), "...")
|
||||
p := flow.Param(r.Context(), "name")
|
||||
ref := flow.Param(r.Context(), "ref")
|
||||
|
||||
repoPath := filepath.Clean(filepath.Join(d.c.Repo.ScanPath, p))
|
||||
gr, err := git.Open(repoPath, ref)
|
||||
if err != nil {
|
||||
d.Write500(w)
|
||||
return
|
||||
}
|
||||
|
||||
contents, err := gr.FileContent(f, true)
|
||||
if err != nil {
|
||||
d.Write500(w)
|
||||
return
|
||||
}
|
||||
http.ServeContent(w, r, filepath.Base(f), time.Unix(0, 0), bytes.NewReader([]byte(contents)))
|
||||
}
|
||||
|
||||
func (d *deps) ServeStatic(w http.ResponseWriter, r *http.Request) {
|
||||
f := flow.Param(r.Context(), "file")
|
||||
f = filepath.Clean(filepath.Join(d.c.Dirs.Static, f))
|
||||
|
|
|
@ -1,14 +1,16 @@
|
|||
package routes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.icyphox.sh/legit/git"
|
||||
)
|
||||
|
||||
func isGoModule(gr *git.GitRepo) bool {
|
||||
_, err := gr.FileContent("go.mod")
|
||||
_, err := gr.FileContent("go.mod", false)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
|
@ -22,6 +24,13 @@ func getDescription(path string) (desc string) {
|
|||
return
|
||||
}
|
||||
|
||||
func transformRelativeURLs(html, repoName, mainBranch string) string {
|
||||
return strings.ReplaceAll(
|
||||
html,
|
||||
"=\"./",
|
||||
fmt.Sprintf("=\"/%s/blob/raw/%s/", repoName, mainBranch))
|
||||
}
|
||||
|
||||
func (d *deps) isIgnored(name string) bool {
|
||||
for _, i := range d.c.Repo.Ignore {
|
||||
if name == i {
|
||||
|
|
|
@ -253,6 +253,7 @@ a:hover {
|
|||
display: table;
|
||||
padding: 0.5rem
|
||||
width: 100%;
|
||||
margin-top: 0.5rem;
|
||||
}
|
||||
|
||||
.file-content {
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
<body>
|
||||
{{ template "nav" . }}
|
||||
<main>
|
||||
<p>{{ .path }}</p>
|
||||
<p>{{ .path }} (<a href="{{ .raw }}">raw</a>)</p>
|
||||
<table class="file-wrapper">
|
||||
<tbody><tr>
|
||||
<td class="line-numbers">
|
||||
|
|
Loading…
Reference in a new issue