From 3c1b8007b6d5992e6bffc701ad06e0adec717145 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Mon, 19 Feb 2024 22:23:39 +0000 Subject: support categories at index --- config.yaml | 2 +- routes/routes.go | 95 +++++++++++++++++++++++++++++++++++++++++++++------- static/style.css | 26 ++++++++++---- templates/index.html | 35 ++++++++++--------- 4 files changed, 122 insertions(+), 36 deletions(-) diff --git a/config.yaml b/config.yaml index 7333e95..dc7f9e7 100644 --- a/config.yaml +++ b/config.yaml @@ -1,5 +1,5 @@ repo: - scanPath: /var/www/git + scanPath: ./repositories readme: - readme - README diff --git a/routes/routes.go b/routes/routes.go index 0499a9e..ca2e729 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -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) { diff --git a/static/style.css b/static/style.css index 67b8e26..5f7087b 100644 --- a/static/style.css +++ b/static/style.css @@ -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; } -} +} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 80c9e1d..a820dec 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,20 +2,25 @@ {{ template "head" . }} -
-

{{ .meta.Title }}

-

{{ .meta.Description }}

-
- -
-
- {{ range .info }} - -
{{ .Desc }}
-
{{ .Idle }}
+
+

{{ .meta.Title }}

+

{{ .meta.Description }}

+
+ + +
+ {{ range $key, $value := .categories }} +
{{ $key }}
+
+ {{ range $value }} + +
{{ .Desc }}
+
{{ .Idle }}
{{ end }} -
-
- +
+ {{ end }} +
+ + -{{ end }} +{{ end }} \ No newline at end of file -- cgit