]> Sergey Matveev's repositories - sgblog.git/blob - common.go
More compact Base64 ETag
[sgblog.git] / common.go
1 // SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
2 // Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU Affero General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 // SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
17 package sgblog
18
19 import (
20         "bytes"
21         "fmt"
22         "io"
23         "sort"
24         "text/scanner"
25
26         "github.com/go-git/go-git/v5"
27         "github.com/go-git/go-git/v5/plumbing"
28         "github.com/go-git/go-git/v5/plumbing/object"
29         "go.cypherpunks.ru/recfile"
30 )
31
32 const (
33         Version = "0.34.0"
34         WhenFmt = "2006-01-02 15:04:05Z07:00"
35 )
36
37 func ParseComments(data []byte) []string {
38         comments := []string{}
39         r := recfile.NewReader(bytes.NewReader(data))
40         for {
41                 fields, err := r.Next()
42                 if err != nil {
43                         break
44                 }
45                 if len(fields) != 3 ||
46                         fields[0].Name != "From" ||
47                         fields[1].Name != "Date" ||
48                         fields[2].Name != "Body" {
49                         continue
50                 }
51                 comments = append(comments, fmt.Sprintf(
52                         "%s: %s\n%s: %s\n%s",
53                         fields[0].Name, fields[0].Value,
54                         fields[1].Name, fields[1].Value,
55                         fields[2].Value,
56                 ))
57         }
58         return comments
59 }
60
61 func ParseTopics(data []byte) []string {
62         var s scanner.Scanner
63         s.Init(bytes.NewBuffer(data))
64         topics := []string{}
65         for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
66                 topics = append(topics, s.TokenText())
67         }
68         sort.Strings(topics)
69         return topics
70 }
71
72 func GetNote(repo *git.Repository, tree *object.Tree, what plumbing.Hash) []byte {
73         if tree == nil {
74                 return nil
75         }
76         var entry *object.TreeEntry
77         var err error
78         paths := make([]string, 3)
79         paths[0] = what.String()
80         paths[1] = paths[0][:2] + "/" + paths[0][2:]
81         paths[2] = paths[1][:4+1] + "/" + paths[1][4+1:]
82         for _, p := range paths {
83                 entry, err = tree.FindEntry(p)
84                 if err == nil {
85                         break
86                 }
87         }
88         if entry == nil {
89                 return nil
90         }
91         blob, err := repo.BlobObject(entry.Hash)
92         if err != nil {
93                 return nil
94         }
95         r, err := blob.Reader()
96         if err != nil {
97                 return nil
98         }
99         data, err := io.ReadAll(r)
100         if err != nil {
101                 return nil
102         }
103         return bytes.TrimSuffix(data, []byte{'\n'})
104 }