]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/main.go
Use external recfiles parser library
[sgblog.git] / cmd / sgblog / main.go
index 088d6646ed1f13a7ae6fc5702a821b9b42d780b8..f5b45823510e36a62f025335e741e22d0ecc8339 100644 (file)
@@ -32,6 +32,7 @@ import (
        "github.com/go-git/go-git/v5"
        "github.com/go-git/go-git/v5/plumbing"
        "github.com/go-git/go-git/v5/plumbing/object"
+       "go.cypherpunks.ru/recfile"
 )
 
 const (
@@ -118,36 +119,24 @@ func getNote(tree *object.Tree, what plumbing.Hash) []byte {
 
 func parseComments(data []byte) []string {
        comments := []string{}
-       isBody := false
-       comment := make([]string, 0, 4)
-       lines := strings.Split(strings.TrimSuffix(string(data), "\n"), "\n")
-       if len(lines) == 1 {
-               return comments
-       }
-       for _, s := range lines {
-               if s == "" {
-                       comments = append(comments, strings.Join(comment, "\n"))
-                       comment = make([]string, 0, 4)
-                       isBody = false
-                       continue
-               }
-               if s == "Body:" {
-                       isBody = true
-                       comment = append(comment, "")
-                       continue
+       r := recfile.NewReader(bytes.NewReader(data))
+       for {
+               fields, err := r.Next()
+               if err != nil {
+                       break
                }
-               if isBody {
-                       if s == "+" {
-                               comment = append(comment, "")
-                       } else {
-                               comment = append(comment, strings.TrimPrefix(s, "+ "))
-                       }
+               if len(fields) != 3 ||
+                       fields[0].Name != "From" ||
+                       fields[1].Name != "Date" ||
+                       fields[2].Name != "Body" {
                        continue
                }
-               comment = append(comment, s)
-       }
-       if len(comment) > 1 {
-               comments = append(comments, strings.Join(comment, "\n"))
+               comments = append(comments, fmt.Sprintf(
+                       "%s: %s\n%s: %s\n%s",
+                       fields[0].Name, fields[0].Value,
+                       fields[1].Name, fields[1].Value,
+                       fields[2].Value,
+               ))
        }
        return comments
 }