repos / pgit

static site generator for git
git clone https://github.com/picosh/pgit.git

commit
e51bb05
parent
d19c0db
author
Eric Bower
date
2023-08-05 18:53:20 +0000 UTC
content edits
8 files changed,  +71, -48
M config.yaml
+2, -0
1@@ -3,6 +3,8 @@ repos:
2   - path: /home/erock/dev/pico/pico
3     refs:
4       - main
5+    desc: pico services - prose.sh, lists.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
6   - path: /home/erock/dev/pico/pico-ops
7     refs:
8       - main
9+    desc: infra for pico services
M html/base.layout.tmpl
+1, -3
 1@@ -13,8 +13,6 @@
 2         <link rel="stylesheet" href="/main.css" />
 3         <link rel="stylesheet" href="/syntax.css" />
 4     </head>
 5-    <body>
 6-      {{template "content" .}}
 7-    </body>
 8+    <body>{{template "content" .}}</body>
 9 </html>
10 {{end}}
M html/commit.page.tmpl
+1, -1
1@@ -22,7 +22,7 @@
2 
3   <div class="box mono">
4     <div>
5-      <strong>{{.Data.Diff.NumFiles}}</strong> files changed,&nbps;
6+      <strong>{{.Data.Diff.NumFiles}}</strong> files changed,&nbsp;
7       <span class="color-green">+{{.Data.Diff.TotalAdditions}}</span>,
8       <span class="color-red">-{{.Data.Diff.TotalDeletions}}</span>
9     </div>
M html/header.partial.tmpl
+10, -7
 1@@ -1,17 +1,20 @@
 2 {{define "header"}}
 3-<h1>
 4+<h1 class="text-xl">
 5   <a href="/">repos</a>
 6   <span>&nbsp;/&nbsp;{{.RepoName}}</span>
 7 </h1>
 8 
 9-<div>
10-    <a href="{{.Repo.SummaryURL}}">summary</a> |
11-    <a href="{{.Repo.TreeURL}}">tree</a> |
12-    <a href="{{.Repo.LogURL}}">log</a> |
13-    <a href="{{.Repo.RefsURL}}">refs</a>
14+<div class="text-lg">
15+  <a href="{{.Repo.SummaryURL}}">summary</a> |
16+  <a href="{{.Repo.TreeURL}}">tree</a> |
17+  <a href="{{.Repo.LogURL}}">log</a> |
18+  <a href="{{.Repo.RefsURL}}">refs</a>
19 </div>
20 
21-<p>git clone {{.Repo.CloneURL}}</p>
22+<div class="my">
23+  <div class="text-lg">{{.Repo.Desc}}</div>
24+  <pre style="margin: 0;">git clone {{.Repo.CloneURL}}</pre>
25+</div>
26 
27 <hr />
28 {{end}}
M html/summary.page.tmpl
+2, -1
1@@ -4,5 +4,6 @@
2 
3 {{define "content"}}
4   {{template "header" .}}
5-  <p>{{.Data.Readme}}</p>
6+
7+  {{.Data.Readme}}
8 {{end}}
M html/tree.page.tmpl
+9, -3
 1@@ -5,10 +5,16 @@
 2 {{define "content"}}
 3   {{template "header" .}}
 4 
 5+  <div>
 6   {{range .Data.Tree}}
 7-    <div>
 8-      <a href="{{.URL}}">{{.Path}}</a>
 9-      <span> (lines: {{.NumLines}})</span>
10+    <div class="flex justify-between mt">
11+      <div class="text-md">
12+        <a href="{{.URL}}">{{.Path}}</a>
13+      </div>
14+      <div class="mono">
15+        <span>lines: {{.NumLines}}</span>
16+      </div>
17     </div>
18   {{end}}
19+  </div>
20 {{end}}
M main.go
+37, -32
  1@@ -26,6 +26,7 @@ type IndexPage struct {
  2 
  3 type RepoData struct {
  4 	Name       string
  5+	Desc       string
  6 	SummaryURL string
  7 	TreeURL    string
  8 	LogURL     string
  9@@ -51,7 +52,7 @@ type PageData struct {
 10 	Branches []*git.Reference
 11 	Tags     []*git.Reference
 12 	Tree     []*TreeItem
 13-	Readme   string
 14+	Readme   template.HTML
 15 	Rev      *git.Reference
 16 	RevName  string
 17 }
 18@@ -219,16 +220,7 @@ func writeRootSummary(data *PageData) {
 19 		Repo:     data.Repo,
 20 	})
 21 }
 22-func writeSummary(data *PageData) {
 23-	writeHtml(&WriteData{
 24-		Name:     "index.html",
 25-		Subdir:   filepath.Join("tree", data.RevName),
 26-		Template: "./html/summary.page.tmpl",
 27-		Data:     data,
 28-		RepoName: data.Repo.Name,
 29-		Repo:     data.Repo,
 30-	})
 31-}
 32+
 33 func writeTree(data *PageData) {
 34 	writeHtml(&WriteData{
 35 		Name:     "index.html",
 36@@ -263,7 +255,8 @@ type FileData struct {
 37 	Contents template.HTML
 38 }
 39 
 40-func writeHTMLTreeFiles(data *PageData) {
 41+func writeHTMLTreeFiles(data *PageData) string {
 42+	readme := ""
 43 	for _, file := range data.Tree {
 44 		b, err := file.Entry.Blob().Bytes()
 45 		bail(err)
 46@@ -271,9 +264,13 @@ func writeHTMLTreeFiles(data *PageData) {
 47 
 48 		d := filepath.Dir(file.Path)
 49 		contents, err := pastes.ParseText(file.Entry.Name(), string(b))
 50-		if err != nil {
 51-			panic(err)
 52+		bail(err)
 53+
 54+		nameLower := strings.ToLower(file.Entry.Name())
 55+		if nameLower == "readme.md" {
 56+			readme = contents
 57 		}
 58+
 59 		writeHtml(&WriteData{
 60 			Name:     fmt.Sprintf("%s.html", file.Entry.Name()),
 61 			Template: "./html/file.page.tmpl",
 62@@ -283,6 +280,7 @@ func writeHTMLTreeFiles(data *PageData) {
 63 			Repo:     data.Repo,
 64 		})
 65 	}
 66+	return readme
 67 }
 68 
 69 func writeLogDiffs(project string, repo *git.Repository, data *PageData, cache map[string]bool) {
 70@@ -340,9 +338,8 @@ func writeLogDiffs(project string, repo *git.Repository, data *PageData, cache m
 71 				}
 72 			}
 73 			finContent, err := pastes.ParseText("commit.diff", content)
 74-			if err != nil {
 75-				panic(err)
 76-			}
 77+			bail(err)
 78+
 79 			fl.Content = template.HTML(finContent)
 80 			fls = append(fls, fl)
 81 		}
 82@@ -373,6 +370,7 @@ func writeRepo(config *RepoConfig) {
 83 	bail(err)
 84 
 85 	name := repoName(config.Path)
 86+	desc := config.Desc
 87 
 88 	heads, err := repo.ShowRef(git.ShowRefOptions{Heads: true, Tags: false})
 89 	bail(err)
 90@@ -385,6 +383,7 @@ func writeRepo(config *RepoConfig) {
 91 
 92 	repoData := &RepoData{
 93 		Name:       name,
 94+		Desc:       desc,
 95 		SummaryURL: fmt.Sprintf("/%s/index.html", name),
 96 		TreeURL:    fmt.Sprintf("/%s/tree/%s/index.html", name, revName),
 97 		LogURL:     fmt.Sprintf("/%s/logs/%s/index.html", name, revName),
 98@@ -396,17 +395,7 @@ func writeRepo(config *RepoConfig) {
 99 
100 	cache := make(map[string]bool)
101 
102-	data := &PageData{
103-		Branches: heads,
104-		Tags:     tags,
105-		Rev:      rev,
106-		RevName:  revName,
107-		Repo:     repoData,
108-		Readme:   "",
109-	}
110-	writeRootSummary(data)
111-	writeRefs(data)
112-
113+	readme := ""
114 	for _, revn := range config.Refs {
115 		for _, head := range heads {
116 			_, headName := filepath.Split(head.Refspec)
117@@ -419,15 +408,28 @@ func writeRepo(config *RepoConfig) {
118 				Rev:      head,
119 				RevName:  headName,
120 				Repo:     repoData,
121-				Readme:   "",
122 			}
123 
124-			writeBranch(repo, data, cache)
125+			branchReadme := writeBranch(repo, data, cache)
126+			if readme == "" {
127+				readme = branchReadme
128+			}
129 		}
130 	}
131+
132+	data := &PageData{
133+		Branches: heads,
134+		Tags:     tags,
135+		Rev:      rev,
136+		RevName:  revName,
137+		Repo:     repoData,
138+		Readme:   template.HTML(readme),
139+	}
140+	writeRefs(data)
141+	writeRootSummary(data)
142 }
143 
144-func writeBranch(repo *git.Repository, pageData *PageData, cache map[string]bool) {
145+func writeBranch(repo *git.Repository, pageData *PageData, cache map[string]bool) string {
146 	commits, err := repo.CommitsByPage(pageData.Rev.ID, 0, 100)
147 	bail(err)
148 
149@@ -459,7 +461,7 @@ func writeBranch(repo *git.Repository, pageData *PageData, cache map[string]bool
150 	pageData.Tree = treeEntries
151 
152 	writeLog(pageData)
153-	writeHTMLTreeFiles(pageData)
154+	readme := writeHTMLTreeFiles(pageData)
155 	writeLogDiffs(pageData.Repo.Name, repo, pageData, cache)
156 
157 	for _, def := range defaultBranches {
158@@ -467,11 +469,14 @@ func writeBranch(repo *git.Repository, pageData *PageData, cache map[string]bool
159 			writeTree(pageData)
160 		}
161 	}
162+
163+	return readme
164 }
165 
166 type RepoConfig struct {
167 	Path string   `mapstructure:"path"`
168 	Refs []string `mapstructure:"refs"`
169+	Desc string   `mapstructure:"desc"`
170 }
171 type Config struct {
172 	Repos []*RepoConfig `mapstructure:"repos"`
M static/main.css
+9, -1
 1@@ -67,7 +67,7 @@ html {
 2 
 3 body {
 4   margin: 0 auto;
 5-  max-width: 720px;
 6+  max-width: 900px;
 7 }
 8 
 9 img {
10@@ -115,6 +115,14 @@ pre {
11   background-color: var(--pre) !important;
12 }
13 
14+.my {
15+  margin: 1rem 0;
16+}
17+
18+.mt {
19+  margin-top: 1rem;
20+}
21+
22 .mono {
23   font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", Menlo,
24     monospace;