]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/main.go
recfiles instead of netstrings
[sgblog.git] / cmd / sgblog / main.go
index 68a3914e04c3240591c84e6ee74128f0f048dbfe..088d6646ed1f13a7ae6fc5702a821b9b42d780b8 100644 (file)
@@ -25,12 +25,13 @@ import (
        "io/ioutil"
        "os"
        "regexp"
+       "sort"
        "strings"
+       "text/scanner"
 
        "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/netstring/v2"
 )
 
 const (
@@ -43,6 +44,8 @@ var (
        notesTree    *object.Tree
        commentsRef  *plumbing.Reference
        commentsTree *object.Tree
+       topicsRef    *plumbing.Reference
+       topicsTree   *object.Tree
 )
 
 type Cfg struct {
@@ -64,6 +67,9 @@ type Cfg struct {
        CommentsNotesRef string
        CommentsEmail    string
 
+       TopicsNotesRef  string
+       TopicsCachePath string
+
        GopherDomain string
 }
 
@@ -112,18 +118,51 @@ func getNote(tree *object.Tree, what plumbing.Hash) []byte {
 
 func parseComments(data []byte) []string {
        comments := []string{}
-       nsr := netstring.NewReader(bytes.NewReader(data))
-       for {
-               if _, err := nsr.Next(); err != nil {
-                       break
+       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 comment, err := ioutil.ReadAll(nsr); err == nil {
-                       comments = append(comments, string(comment))
+               if s == "Body:" {
+                       isBody = true
+                       comment = append(comment, "")
+                       continue
                }
+               if isBody {
+                       if s == "+" {
+                               comment = append(comment, "")
+                       } else {
+                               comment = append(comment, strings.TrimPrefix(s, "+ "))
+                       }
+                       continue
+               }
+               comment = append(comment, s)
+       }
+       if len(comment) > 1 {
+               comments = append(comments, strings.Join(comment, "\n"))
        }
        return comments
 }
 
+func parseTopics(data []byte) []string {
+       var s scanner.Scanner
+       s.Init(bytes.NewBuffer(data))
+       topics := []string{}
+       for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
+               topics = append(topics, s.TokenText())
+       }
+       sort.Strings(topics)
+       return topics
+}
+
 func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
        var err error
        repo, err = git.PlainOpen(cfg.GitPath)
@@ -143,6 +182,8 @@ func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
                                notesRef = ref
                        case cfg.CommentsNotesRef:
                                commentsRef = ref
+                       case cfg.TopicsNotesRef:
+                               topicsRef = ref
                        }
                        return nil
                })
@@ -156,6 +197,11 @@ func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
                                commentsTree, _ = commentsCommit.Tree()
                        }
                }
+               if topicsRef != nil {
+                       if topicsCommit, err := repo.CommitObject(topicsRef.Hash()); err == nil {
+                               topicsTree, _ = topicsCommit.Tree()
+                       }
+               }
        }
        return &headHash, nil
 }