From 458f87066f39f381498cbe9df533bf0b8de91998 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Thu, 13 Jul 2023 04:59:01 +0000 Subject: [PATCH] working quite well --- .gitignore | 2 ++ main.go | 24 +++++++++++++++++++ templates/index.tmpl | 56 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 .gitignore create mode 100644 templates/index.tmpl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cba3a61 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +blackout +*.swp diff --git a/main.go b/main.go index 8096748..8cb2282 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,8 @@ import ( "log" "math/big" "net/http" + "regexp" + "strings" "crypto/rand" "database/sql" @@ -29,6 +31,7 @@ func connectDB() (*sql.DB, error) { type chunk struct { Chunk string + Tokens []string Name string Author string } @@ -39,6 +42,8 @@ func main() { randMax := big.NewInt(maxID) + spaceRE := regexp.MustCompile(`[\t\v\f\r ]+`) + r.GET("/", func(c *gin.Context) { db, err := connectDB() if err != nil { @@ -69,6 +74,25 @@ func main() { c.String(http.StatusInternalServerError, "oh no.") } + dest.Tokens = []string{} + for _, t := range spaceRE.Split(dest.Chunk, -1) { + if t == "" { + continue + } + if strings.Contains(t, "\n") { + sp := strings.Split(t, "\n") + for x, s := range sp { + nl := "\n" + if x == len(sp)-1 { + nl = "" + } + dest.Tokens = append(dest.Tokens, s+nl) + } + } else { + dest.Tokens = append(dest.Tokens, t) + } + } + c.HTML(http.StatusOK, "index.tmpl", dest) }) r.Run() // 8080 diff --git a/templates/index.tmpl b/templates/index.tmpl new file mode 100644 index 0000000..2736571 --- /dev/null +++ b/templates/index.tmpl @@ -0,0 +1,56 @@ + + + + blackout engine + + + +
+
+ {{- range .Tokens -}} + {{.}} + {{- end -}} +
+ +
+
{{.Chunk}}
+
{{.Name}}
+
{{.Author}}
+ +