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
Render toggle for binary files
author
Ben Vogt <[email protected]>
date
2023-04-03 22:23:25
stats
3 file(s) changed, 48 insertions(+), 46 deletions(-)
files
file.template.html
main.go
styles.css
  1diff --git a/file.template.html b/file.template.html
  2index eeb873d..9c330ce 100644
  3--- a/file.template.html
  4+++ b/file.template.html
  5@@ -46,7 +46,11 @@
  6       </table>
  7     </div>
  8     <div class="file">
  9+      {{ if .CanRender }}
 10       {{ .Content }}
 11+      {{ else }}
 12+      Cannot preview file with extension <code>{{ .Extension }}</code>
 13+      {{ end }}
 14     </div>
 15   </div>
 16 </body>
 17diff --git a/main.go b/main.go
 18index 7babcba..5515dd4 100644
 19--- a/main.go
 20+++ b/main.go
 21@@ -112,6 +112,8 @@ type TrackedFile struct {
 22 	Name           string
 23 	Size           string
 24 	Origin         string
 25+	Extension      string
 26+	CanRender      bool
 27 	Destination    string
 28 	DestinationDir string
 29 	Content        template.HTML
 30@@ -128,24 +130,27 @@ func (f *TrackedFile) SaveTemplate(t *template.Template) {
 31 	}
 32 	err := os.MkdirAll(f.DestinationDir, 0775)
 33 	checkErr(err)
 34-	fileBytes, err := os.ReadFile(f.Origin)
 35-	checkErr(err)
 36-	fileStr := string(fileBytes)
 37-	iterator, err := lexer.Tokenise(nil, fileStr)
 38-	formatter := html.New(
 39-		html.WithClasses(true),
 40-		html.WithLineNumbers(true),
 41-		html.LinkableLineNumbers(true, ""),
 42-	)
 43-	s := ""
 44-	buf := bytes.NewBufferString(s)
 45-	err = formatter.Format(buf, style, iterator)
 46-	checkErr(err)
 47+	_, canRender := config.TextExtensions[f.Extension]
 48+	if canRender {
 49+		fileBytes, err := os.ReadFile(f.Origin)
 50+		checkErr(err)
 51+		fileStr := string(fileBytes)
 52+		iterator, err := lexer.Tokenise(nil, fileStr)
 53+		formatter := html.New(
 54+			html.WithClasses(true),
 55+			html.WithLineNumbers(true),
 56+			html.LinkableLineNumbers(true, ""),
 57+		)
 58+		s := ""
 59+		buf := bytes.NewBufferString(s)
 60+		err = formatter.Format(buf, style, iterator)
 61+		checkErr(err)
 62+		f.Content = template.HTML(buf.String())
 63+	}
 64 	err = os.MkdirAll(filepath.Dir(f.Destination), 0775)
 65 	checkErr(err)
 66 	output, err := os.Create(f.Destination)
 67 	checkErr(err)
 68-	f.Content = template.HTML(buf.String())
 69 	err = t.Execute(output, f)
 70 	checkErr(err)
 71 }
 72@@ -221,20 +226,17 @@ func BuildFilesPages() {
 73 		}
 74 
 75 		if !info.IsDir() {
 76-			ext := filepath.Ext(filename)
 77-			if _, ok := config.TextExtensions[ext]; ok {
 78-				info, err := os.Stat(filename)
 79-				checkErr(err)
 80-				Name, _ := strings.CutPrefix(filename, config.CloneDir)
 81-				Name, _ = strings.CutPrefix(Name, "/")
 82-				tf := TrackedFileMetaData{
 83-					Origin: filename,
 84-					Name:   Name,
 85-					Mode:   info.Mode().String(),
 86-					Size:   fmt.Sprintf("%v", info.Size()),
 87-				}
 88-				trackedFiles = append(trackedFiles, tf)
 89+			info, err := os.Stat(filename)
 90+			checkErr(err)
 91+			Name, _ := strings.CutPrefix(filename, config.CloneDir)
 92+			Name, _ = strings.CutPrefix(Name, "/")
 93+			tf := TrackedFileMetaData{
 94+				Origin: filename,
 95+				Name:   Name,
 96+				Mode:   info.Mode().String(),
 97+				Size:   fmt.Sprintf("%v", info.Size()),
 98 			}
 99+			trackedFiles = append(trackedFiles, tf)
100 		}
101 		return nil
102 	})
103@@ -256,17 +258,18 @@ func BuildSingleFilePages() {
104 
105 		if !info.IsDir() {
106 			ext := filepath.Ext(filename)
107-			if _, ok := config.TextExtensions[ext]; ok {
108-				partialPath, _ := strings.CutPrefix(filename, config.CloneDir)
109-				outputName := path.Join(config.OutputDir, "files", partialPath, "index.html")
110-				debug("reading = %v", partialPath)
111-				tf := TrackedFile{
112-					Origin:         filename,
113-					Destination:    outputName,
114-					DestinationDir: path.Join(config.OutputDir, "files", partialPath),
115-				}
116-				tf.SaveTemplate(t)
117+			_, canRender := config.TextExtensions[ext]
118+			partialPath, _ := strings.CutPrefix(filename, config.CloneDir)
119+			outputName := path.Join(config.OutputDir, "files", partialPath, "index.html")
120+			debug("reading = %v", partialPath)
121+			tf := TrackedFile{
122+				Extension:      ext,
123+				CanRender:      canRender,
124+				Origin:         filename,
125+				Destination:    outputName,
126+				DestinationDir: path.Join(config.OutputDir, "files", partialPath),
127 			}
128+			tf.SaveTemplate(t)
129 		}
130 		return nil
131 	})
132diff --git a/styles.css b/styles.css
133index 9f3d604..c2a7bfd 100644
134--- a/styles.css
135+++ b/styles.css
136@@ -344,16 +344,6 @@ div.content pre {
137   border-radius: 4px;
138 }
139 
140-div.content code {
141-  padding: 2px;
142-}
143-
144-div.content code::before,
145-div.content code::after {
146-  letter-spacing: -0.2em;
147-  content: "\00a0"
148-}
149-
150 div.content pre>code {
151   padding: 0;
152   background-color: transparent;