gshr
git static host repo -- generates static html for repos
git clone https://git.vogt.world/gshr.git
Log | Files | README.md | LICENSE
← Commit log
commit
message
Files list page working
author
Ben Vogt <[email protected]>
date
2023-04-03 16:42:34
stats
4 file(s) changed, 118 insertions(+), 33 deletions(-)
files
files.template.html
index.template.html
main.go
styles.css
  1diff --git a/files.template.html b/files.template.html
  2new file mode 100644
  3index 0000000..94b3986
  4--- /dev/null
  5+++ b/files.template.html
  6@@ -0,0 +1,44 @@
  7+<!DOCTYPE html>
  8+<html lang="en">
  9+
 10+<head>
 11+  <meta charset="utf-8">
 12+  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 13+  <meta name="viewport" content="width=device-width, initial-scale=1">
 14+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
 15+  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
 16+  <title>gshr</title>
 17+  <link rel="stylesheet" href="/styles.css" type="text/css">
 18+  <meta name="description"
 19+        content="git static host repo">
 20+  <meta property="og:site_name" content="gshr">
 21+  <meta property="og:type" content="article">
 22+  <meta property="article:author" content="gshr">
 23+</head>
 24+
 25+<body>
 26+  <div class="content">
 27+    <div class="commits">
 28+      <table id="log">
 29+        <thead>
 30+          <tr>
 31+            <td><b>Mode</b></td>
 32+            <td><b>Name</b></td>
 33+            <td><b>Size</b></td>
 34+          </tr>
 35+        </thead>
 36+        <tbody>
 37+          {{ range .Files }}
 38+          <tr>
 39+            <td>{{ .Mode }}</td>
 40+            <td><a href="files/{{ .Name }}/index.html">{{ .Name }}</a></td>
 41+            <td>{{ .Size }}</td>
 42+          </tr>
 43+          {{ end }}
 44+        </tbody>
 45+      </table>
 46+    </div>
 47+  </div>
 48+</body>
 49+
 50+</html>
 51\ No newline at end of file
 52diff --git a/index.template.html b/index.template.html
 53index b6bdaf2..5bad6d7 100644
 54--- a/index.template.html
 55+++ b/index.template.html
 56@@ -41,30 +41,6 @@
 57             <td align="right">-0</td>
 58           </tr>
 59           {{ end }}
 60-          <!-- <tr>
 61-            <td>2020-03-04 11:03</td>
 62-            <td><a href="commit/commit_hash.html">update TODO</a></td>
 63-            <td>benjvogt</td>
 64-            <td align="right">1</td>
 65-            <td align="right">+2</td>
 66-            <td align="right">-0</td>
 67-          </tr>
 68-          <tr>
 69-            <td>2020-03-04 10:59</td>
 70-            <td><a href="commit/commit_hash.html">README.md update for clarity.</a></td>
 71-            <td>benjvogt</td>
 72-            <td align="right">3</td>
 73-            <td align="right">+83</td>
 74-            <td align="right">-3</td>
 75-          </tr>
 76-          <tr>
 77-            <td>2020-01-22 10:04</td>
 78-            <td><a href="commit/commit_hash.html">Working through the bugs.</a></td>
 79-            <td>benjvogt</td>
 80-            <td align="right">1</td>
 81-            <td align="right">+2</td>
 82-            <td align="right">-3</td>
 83-          </tr> -->
 84         </tbody>
 85       </table>
 86     </div>
 87diff --git a/main.go b/main.go
 88index 06ba68a..e2da262 100644
 89--- a/main.go
 90+++ b/main.go
 91@@ -29,18 +29,28 @@ var (
 92 	cloningDir = ""
 93 )
 94 
 95+type TrackedFileMetaData struct {
 96+	Mode   string
 97+	Name   string
 98+	Size   string
 99+	Origin string
100+}
101+
102 type TrackedFile struct {
103-	origin      string
104-	destination string
105+	Mode        string
106+	Name        string
107+	Size        string
108+	Origin      string
109+	Destination string
110 }
111 
112 func (tf *TrackedFile) SaveTemplate(t *template.Template) {
113-	fileBytes, err := os.ReadFile(tf.origin)
114+	fileBytes, err := os.ReadFile(tf.Origin)
115 	checkErr(err)
116 	fileStr := string(fileBytes)
117-	err = os.MkdirAll(filepath.Dir(tf.destination), 0775)
118+	err = os.MkdirAll(filepath.Dir(tf.Destination), 0775)
119 	checkErr(err)
120-	output, err := os.Create(tf.destination)
121+	output, err := os.Create(tf.Destination)
122 	checkErr(err)
123 	err = t.Execute(output, fileStr)
124 	checkErr(err)
125@@ -64,6 +74,19 @@ func (mi *MainIndex) SaveTemplate(t *template.Template) {
126 	checkErr(err)
127 }
128 
129+type FilesIndex struct {
130+	Files []TrackedFileMetaData
131+}
132+
133+func (fi *FilesIndex) SaveTemplate(t *template.Template) {
134+	err := os.MkdirAll(path.Join(outputDir, "files"), 0775)
135+	checkErr(err)
136+	output, err := os.Create(path.Join(outputDir, "files", "index.html"))
137+	checkErr(err)
138+	err = t.Execute(output, fi)
139+	checkErr(err)
140+}
141+
142 func main() {
143 	outputDir = os.Getenv("OUTPUT_DIR")
144 	cloningDir = os.Getenv("CLONING_DIR")
145@@ -71,6 +94,7 @@ func main() {
146 	line("CLONING_DIR = %v", cloningDir)
147 	r := CloneAndInfo()
148 	BuildMainIndex(r)
149+	BuildFilesIndex()
150 	BuildTrackedFiles()
151 }
152 
153@@ -106,7 +130,39 @@ func BuildMainIndex(r *git.Repository) {
154 		Commits: commits,
155 	}
156 	m.SaveTemplate(t)
157+}
158 
159+func BuildFilesIndex() {
160+	t, err := template.ParseFiles("files.template.html")
161+	trackedFiles := make([]TrackedFileMetaData, 0)
162+	err = filepath.Walk(cloningDir, func(filename string, info fs.FileInfo, err error) error {
163+		if info.IsDir() && info.Name() == ".git" {
164+			return filepath.SkipDir
165+		}
166+
167+		if !info.IsDir() {
168+			ext := filepath.Ext(filename)
169+			if _, ok := allowedExtensions[ext]; ok {
170+				info, err := os.Stat(filename)
171+				checkErr(err)
172+				Name, _ := strings.CutPrefix(filename, cloningDir)
173+				Name, _ = strings.CutPrefix(Name, "/")
174+				tf := TrackedFileMetaData{
175+					Origin: filename,
176+					Name:   Name,
177+					Mode:   info.Mode().String(),
178+					Size:   fmt.Sprintf("%v bytes", info.Size()),
179+				}
180+				trackedFiles = append(trackedFiles, tf)
181+			}
182+		}
183+		return nil
184+	})
185+	checkErr(err)
186+	index := FilesIndex{
187+		Files: trackedFiles,
188+	}
189+	index.SaveTemplate(t)
190 }
191 
192 func BuildTrackedFiles() {
193@@ -126,8 +182,8 @@ func BuildTrackedFiles() {
194 				line("READING: %v", filename)
195 				line("WRITING: %v", outputName)
196 				tf := TrackedFile{
197-					origin:      filename,
198-					destination: outputName,
199+					Origin:      filename,
200+					Destination: outputName,
201 				}
202 				tf.SaveTemplate(t)
203 			}
204diff --git a/styles.css b/styles.css
205index 872e062..93e940f 100644
206--- a/styles.css
207+++ b/styles.css
208@@ -411,6 +411,10 @@ div.content div.commits table {
209   width: 100%;
210 }
211 
212+div.content div.commits table td {
213+  padding: 0 0.4em;
214+}
215+
216 div.content div.commits table tr:hover {
217   background: #efefef;
218 }