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:
scanPath: /var/www/git
scanPath: ./repositories
readme:
- readme
- README

View File

@ -1,6 +1,7 @@
package routes
import (
"errors"
"fmt"
"html/template"
"log"
@ -8,12 +9,14 @@ import (
"os"
"path/filepath"
"sort"
"strings"
"time"
"git.icyphox.sh/legit/config"
"git.icyphox.sh/legit/git"
"github.com/alexedwards/flow"
"github.com/dustin/go-humanize"
gogit "github.com/go-git/go-git/v5"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
)
@ -22,6 +25,11 @@ type deps struct {
c *config.Config
}
type info struct {
Name, Path, Desc, Idle string
d time.Time
}
func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
dirs, err := os.ReadDir(d.c.Repo.ScanPath)
if err != nil {
@ -30,12 +38,7 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
return
}
type info struct {
Name, Desc, Idle string
d time.Time
}
infos := []info{}
categories := make(map[string][]info)
for _, dir := range dirs {
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())
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)
continue
}
@ -57,25 +70,29 @@ func (d *deps) Index(w http.ResponseWriter, r *http.Request) {
}
desc := getDescription(path)
name := strings.TrimSuffix(dir.Name(), ".git")
infos = append(infos, info{
Name: dir.Name(),
categories[""] = append(categories[""], info{
Name: name,
Path: name,
Desc: desc,
Idle: humanize.Time(c.Author.When),
d: c.Author.When,
})
}
sort.Slice(infos, func(i, j int) bool {
return infos[j].d.Before(infos[i].d)
})
for _, infos := range categories {
sort.Slice(infos, func(i, j int) bool {
return infos[j].d.Before(infos[i].d)
})
}
tpath := filepath.Join(d.c.Dirs.Templates, "*")
t := template.Must(template.ParseGlob(tpath))
data := make(map[string]interface{})
data["meta"] = d.c.Meta
data["info"] = infos
data["categories"] = categories
if err := t.ExecuteTemplate(w, "index", data); err != nil {
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) {
name := flow.Param(r.Context(), "name")
if d.isIgnored(name) {

View File

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

View File

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