support categories at index

This commit is contained in:
cel 🌸 2024-02-19 22:23:39 +00:00
parent bdfc973207
commit 3c1b8007b6
4 changed files with 122 additions and 36 deletions

View File

@ -1,5 +1,5 @@
repo: repo:
scanPath: /var/www/git scanPath: ./repositories
readme: readme:
- readme - readme
- README - README

View File

@ -1,6 +1,7 @@
package routes package routes
import ( import (
"errors"
"fmt" "fmt"
"html/template" "html/template"
"log" "log"
@ -8,12 +9,14 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
"strings"
"time" "time"
"git.icyphox.sh/legit/config" "git.icyphox.sh/legit/config"
"git.icyphox.sh/legit/git" "git.icyphox.sh/legit/git"
"github.com/alexedwards/flow" "github.com/alexedwards/flow"
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
gogit "github.com/go-git/go-git/v5"
"github.com/microcosm-cc/bluemonday" "github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2" "github.com/russross/blackfriday/v2"
) )
@ -22,6 +25,11 @@ type deps struct {
c *config.Config c *config.Config
} }
type info struct {
Name, Path, Desc, Idle string
d time.Time
}
func (d *deps) Index(w http.ResponseWriter, r *http.Request) { func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
dirs, err := os.ReadDir(d.c.Repo.ScanPath) dirs, err := os.ReadDir(d.c.Repo.ScanPath)
if err != nil { if err != nil {
@ -30,12 +38,7 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
return return
} }
type info struct { categories := make(map[string][]info)
Name, Desc, Idle string
d time.Time
}
infos := []info{}
for _, dir := range dirs { for _, dir := range dirs {
if d.isIgnored(dir.Name()) { if d.isIgnored(dir.Name()) {
@ -44,7 +47,17 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
path := filepath.Join(d.c.Repo.ScanPath, dir.Name()) path := filepath.Join(d.c.Repo.ScanPath, dir.Name())
gr, err := git.Open(path, "") gr, err := git.Open(path, "")
if err != nil { if errors.Is(err, gogit.ErrRepositoryNotExists) {
log.Printf("reading category: %s", dir.Name())
infos, err := d.IndexCategory(path, dir.Name())
if err != nil {
log.Printf("reading category: %s", err)
}
if len(infos) > 0 {
categories[dir.Name()] = infos
}
continue
} else if err != nil {
log.Println(err) log.Println(err)
continue continue
} }
@ -57,25 +70,29 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
} }
desc := getDescription(path) desc := getDescription(path)
name := strings.TrimSuffix(dir.Name(), ".git")
infos = append(infos, info{ categories[""] = append(categories[""], info{
Name: dir.Name(), Name: name,
Path: name,
Desc: desc, Desc: desc,
Idle: humanize.Time(c.Author.When), Idle: humanize.Time(c.Author.When),
d: c.Author.When, d: c.Author.When,
}) })
} }
sort.Slice(infos, func(i, j int) bool { for _, infos := range categories {
return infos[j].d.Before(infos[i].d) sort.Slice(infos, func(i, j int) bool {
}) return infos[j].d.Before(infos[i].d)
})
}
tpath := filepath.Join(d.c.Dirs.Templates, "*") tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath)) t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{}) data := make(map[string]interface{})
data["meta"] = d.c.Meta data["meta"] = d.c.Meta
data["info"] = infos data["categories"] = categories
if err := t.ExecuteTemplate(w, "index", data); err != nil { if err := t.ExecuteTemplate(w, "index", data); err != nil {
log.Println(err) log.Println(err)
@ -83,6 +100,58 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
} }
} }
func (d *deps) IndexCategory(scanpath string, category string) ([]info, error) {
dirs, err := os.ReadDir(scanpath)
if err != nil {
return nil, fmt.Errorf("reading scan path: %s", err)
}
infos := []info{}
for _, dir := range dirs {
if d.isIgnored(dir.Name()) {
continue
}
path := filepath.Join(scanpath, dir.Name())
gr, err := git.Open(path, "")
if errors.Is(err, gogit.ErrRepositoryNotExists) {
log.Println(path)
folder_infos, err := d.IndexCategory(path, category)
if err != nil {
log.Println(err)
continue
}
infos = append(infos, folder_infos...)
continue
} else if err != nil {
log.Println(err)
continue
}
c, err := gr.LastCommit()
if err != nil {
return nil, err
}
desc := getDescription(path)
repodir := filepath.Join(scanpath, dir.Name())
repopath := strings.Split(repodir, category)
name := strings.TrimSuffix(repopath[len(repopath)-1], ".git")
name = strings.TrimPrefix(name, "/")
infos = append(infos, info{
Name: name,
Path: category + "/" + name,
Desc: desc,
Idle: humanize.Time(c.Author.When),
d: c.Author.When,
})
}
return infos, nil
}
func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) { func (d *deps) RepoIndex(w http.ResponseWriter, r *http.Request) {
name := flow.Param(r.Context(), "name") name := flow.Param(r.Context(), "name")
if d.isIgnored(name) { if d.isIgnored(name) {

View File

@ -39,13 +39,17 @@ body {
margin: 40px auto; margin: 40px auto;
} }
main, footer { main,
footer {
font-size: 1rem; font-size: 1rem;
padding: 0; padding: 0;
line-height: 160%; line-height: 160%;
} }
main h1, h2, h3, strong { main h1,
h2,
h3,
strong {
font-family: var(--display-font); font-family: var(--display-font);
font-weight: 500; font-weight: 500;
} }
@ -63,7 +67,8 @@ main h2 {
font-size: 18px; font-size: 18px;
} }
main h2, h3 { main h2,
h3 {
padding: 20px 0 15px 0; padding: 20px 0 15px 0;
} }
@ -101,13 +106,17 @@ a:hover {
} }
.index { .index {
padding-top: 2em;
display: grid; display: grid;
grid-template-columns: 6em 1fr minmax(0, 7em); grid-template-columns: 6em 1fr minmax(0, 7em);
grid-row-gap: 0.5em; grid-row-gap: 0.5em;
min-width: 0; min-width: 0;
} }
.index-category {
margin-top: 2em;
color: var(--gray);
}
.clone-url { .clone-url {
padding-top: 2rem; padding-top: 2rem;
} }
@ -145,9 +154,11 @@ a:hover {
white-space: pre-wrap; white-space: pre-wrap;
} }
.mode, .size { .mode,
.size {
font-family: var(--mono-font); font-family: var(--mono-font);
} }
.size { .size {
text-align: right; text-align: right;
} }
@ -183,7 +194,8 @@ a:hover {
padding: 1rem 0 1rem 0; padding: 1rem 0 1rem 0;
} }
.commit-hash, .commit-email { .commit-hash,
.commit-email {
font-family: var(--mono-font); font-family: var(--mono-font);
} }
@ -303,4 +315,4 @@ a:hover {
pre { pre {
font-size: 0.8rem; font-size: 0.8rem;
} }
} }

View File

@ -2,20 +2,25 @@
<html> <html>
{{ template "head" . }} {{ template "head" . }}
<header> <header>
<h1>{{ .meta.Title }}</h1> <h1>{{ .meta.Title }}</h1>
<h2>{{ .meta.Description }}</h2> <h2>{{ .meta.Description }}</h2>
</header> </header>
<body>
<main> <body>
<div class="index"> <main>
{{ range .info }} {{ range $key, $value := .categories }}
<div class="index-name"><a href="/{{ .Name }}">{{ .Name }}</a></div> <div class="index-category">{{ $key }}</div>
<div class="desc">{{ .Desc }}</div> <div class="index">
<div>{{ .Idle }}</div> {{ range $value }}
<div class="index-name"><a href="/{{ .Path }}">{{ .Name }}</a></div>
<div class="desc">{{ .Desc }}</div>
<div>{{ .Idle }}</div>
{{ end }} {{ end }}
</div> </div>
</main> {{ end }}
</body> </main>
</body>
</html> </html>
{{ end }} {{ end }}