]> Sergey Matveev's repositories - sgblog.git/commitdiff
recfiles instead of netstrings v0.9.0
authorSergey Matveev <stargrave@stargrave.org>
Thu, 15 Oct 2020 11:23:00 +0000 (14:23 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Thu, 15 Oct 2020 11:23:00 +0000 (14:23 +0300)
README.texi
cmd/sgblog-comment-add/main.go
cmd/sgblog/main.go
common.go
go.mod
go.sum

index a0309d81dd4dd38677b4b8b27845d510b8d1e2d1..9ede00801ae5d2377c181dd5e66dfb2a3700b58a 100644 (file)
@@ -82,11 +82,22 @@ Each comment is just a plaintext with @code{From} and @code{Date}
 headers. @code{From} is a name of email sender (with email address
 stripped off).
 
-Technically comments are stored in concatenated
-@url{https://en.wikipedia.org/wiki/Netstring, netstring}. Only
-@code{text/plain} or @code{multipart/signed+text/plain} email messages
-are accepted and only with UTF-8, US-ASCII, ISO-8859-1 character sets.
-Sane people won't send HTML email anyway, but this is just a precaution.
+Only @code{text/plain} or @code{multipart/signed+text/plain} email
+messages are accepted and only with UTF-8, US-ASCII, ISO-8859-1
+character sets. Sane people won't send HTML email anyway, but this is
+just a precaution.
+
+Comments are stored in @dfn{recfiles} --
+@url{https://www.gnu.org/software/recutils/, GNU recutils}
+human-editable plaintext database format. But they do not contain
+records description:
+
+@verbatim
+%rec: Comment
+%doc: SGBlog's comment
+%mandatory: From Date Body
+%type: Date date
+@end verbatim
 
 @node Topics
 @unnumbered Topics
index cb270afee87d1789f2da06218ee46d4108c8976c..2b977ae7feb14c0c886851f8871ca6e1d982b32c 100644 (file)
@@ -36,7 +36,6 @@ import (
        "syscall"
        "time"
 
-       "go.cypherpunks.ru/netstring/v2"
        "go.stargrave.org/sgblog"
 )
 
@@ -44,7 +43,7 @@ var hashFinder = regexp.MustCompile("([0-9a-f]{40})")
 
 // Remove various whitespaces and excess lines, because git-notes-add
 // will remove and we have to know exact bytes count
-func cleanupBody(body string) string {
+func cleanupBody(body string) []string {
        lines := strings.Split(string(body), "\n")
        for i, line := range lines {
                line = strings.ReplaceAll(line, "       ", "    ")
@@ -70,7 +69,7 @@ func cleanupBody(body string) string {
                }
                withoutDups = append(withoutDups, line)
        }
-       return strings.Join(withoutDups, "\n")
+       return withoutDups
 }
 
 func main() {
@@ -143,13 +142,16 @@ func main() {
        note = bytes.TrimRight(note, "\r\n")
 
        buf := bytes.NewBuffer(note)
-       w := netstring.NewWriter(buf)
-       w.WriteChunk([]byte(fmt.Sprintf(
-               "From: %s\nDate: %s\n\n%s",
+       buf.WriteString(fmt.Sprintf(
+               "\n\nFrom: %s\nDate: %s\nBody:\n",
                from,
                time.Now().UTC().Format(sgblog.WhenFmt),
-               cleanupBody(string(body)),
-       )))
+       ))
+       for _, s := range cleanupBody(string(body)) {
+               buf.WriteString("+ ")
+               buf.WriteString(s)
+               buf.WriteString("\n")
+       }
 
        if *dryRun {
                fmt.Print(buf.String())
index 2e4a483e27d1683c0e064481578c61bf57b4558f..088d6646ed1f13a7ae6fc5702a821b9b42d780b8 100644 (file)
@@ -32,7 +32,6 @@ 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/netstring/v2"
 )
 
 const (
@@ -119,14 +118,36 @@ 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
 }
index 6476388518d1e7b8e8ddb534fe984325a104e061..7fae06b6c1a2254883b610a49238f9683842e684 100644 (file)
--- a/common.go
+++ b/common.go
@@ -2,6 +2,6 @@
 package sgblog
 
 const (
-       Version = "0.8.0"
+       Version = "0.9.0"
        WhenFmt = "2006-01-02 15:04:05Z07:00"
 )
diff --git a/go.mod b/go.mod
index 8cc51fced5b9054862524d0a0134ac20f50c2fdf..25f4d41a8df1a93476c28d78c735bf43254f764c 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -6,7 +6,6 @@ require (
        github.com/go-git/go-git/v5 v5.1.0
        github.com/hjson/hjson-go v3.0.1+incompatible
        github.com/imdario/mergo v0.3.11 // indirect
-       go.cypherpunks.ru/netstring/v2 v2.0.0
        golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a
        golang.org/x/net v0.0.0-20200925080053-05aa5d4ee321 // indirect
        golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d // indirect
diff --git a/go.sum b/go.sum
index fafd1703b4c5d6fbcb6be42cac5ade604ea584e7..a1fa6911dfe1a5d1cd897054dcc1aeb40088df00 100644 (file)
--- a/go.sum
+++ b/go.sum
@@ -57,8 +57,6 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
 github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
 github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
-go.cypherpunks.ru/netstring/v2 v2.0.0 h1:or1LDZO3fSd6iITGR3jJUfUrjvRgeNlUpEYI13qaRBk=
-go.cypherpunks.ru/netstring/v2 v2.0.0/go.mod h1:6YDx4gW414SmHdvSBMKbHaB2/7w9WZ04NQb7XIUV/pA=
 golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=