1 // SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
2 // Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
16 // SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
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"
34 WhenFmt = "2006-01-02 15:04:05Z07:00"
37 func ParseComments(data []byte) []string {
38 comments := []string{}
39 r := recfile.NewReader(bytes.NewReader(data))
41 fields, err := r.Next()
45 if len(fields) != 3 ||
46 fields[0].Name != "From" ||
47 fields[1].Name != "Date" ||
48 fields[2].Name != "Body" {
51 comments = append(comments, fmt.Sprintf(
53 fields[0].Name, fields[0].Value,
54 fields[1].Name, fields[1].Value,
61 func ParseTopics(data []byte) []string {
63 s.Init(bytes.NewBuffer(data))
65 for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
66 topics = append(topics, s.TokenText())
72 func GetNote(repo *git.Repository, tree *object.Tree, what plumbing.Hash) []byte {
76 var entry *object.TreeEntry
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)
91 blob, err := repo.BlobObject(entry.Hash)
95 r, err := blob.Reader()
99 data, err := io.ReadAll(r)
103 return bytes.TrimSuffix(data, []byte{'\n'})