2 SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
3 Copyright (C) 2020-2022 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as
7 published by the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU Affero General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 "github.com/go-git/go-git/v5"
34 "github.com/go-git/go-git/v5/plumbing"
35 "github.com/go-git/go-git/v5/plumbing/object"
36 "github.com/vorlif/spreak"
37 "go.stargrave.org/sgblog"
41 //go:embed gopher-menu.tmpl
42 TmplGopherMenuRaw string
43 //go:embed gopher-entry.tmpl
44 TmplGopherEntryRaw string
46 TmplGopherMenu = template.Must(template.New("gopher-menu").Parse(TmplGopherMenuRaw))
47 TmplGopherEntry = template.Must(template.New("gopher-entry").Parse(TmplGopherEntryRaw))
50 type TableMenuEntry struct {
58 func serveGopher(cfgPath string) {
59 cfg, err := readCfg(cfgPath)
63 if cfg.GopherDomain == "" {
64 log.Fatalln("GopherDomain is not configured")
66 initLocalizer(cfg.Lang)
68 headHash, err := initRepo(cfg)
73 scanner := bufio.NewScanner(io.LimitReader(os.Stdin, 1<<8))
75 log.Fatalln(errors.New("no CRLF found"))
77 selector := scanner.Text()
81 selectorParts := strings.Split(selector, "/")
82 if strings.HasPrefix(selector, "URL:") {
83 selector = selector[len("URL:"):]
84 fmt.Printf(`<!DOCTYPE html>
87 <meta http-equiv="Refresh" content="1; url=%s" />
88 <title>Redirect to non-gopher URL</title>
91 Redirecting to <a href="%s">%s</a>...
94 `, selector, selector, selector)
95 } else if sha1DigestRe.MatchString(selector) {
96 commit, err := repo.CommitObject(plumbing.NewHash(selector[1:]))
100 err = TmplGopherEntry.Execute(os.Stdout, struct {
102 Commit *object.Commit
113 When: commit.Author.When.Format(sgblog.WhenFmt),
115 Note: string(sgblog.GetNote(repo, notesTree, commit.Hash)),
116 Comments: sgblog.ParseComments(sgblog.GetNote(repo, commentsTree, commit.Hash)),
117 Topics: sgblog.ParseTopics(sgblog.GetNote(repo, topicsTree, commit.Hash)),
118 Version: sgblog.Version,
119 TitleEscaped: url.PathEscape(fmt.Sprintf(
120 "Re: %s (%s)", msgSplit(commit.Message)[0], commit.Hash,
126 } else if len(selectorParts) > 1 &&
127 selectorParts[len(selectorParts)-2] == "offset" {
128 offset, err := strconv.Atoi(selectorParts[len(selectorParts)-1])
132 repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
136 topicsCache, err := getTopicsCache(cfg, repoLog)
140 repoLog, err = repo.Log(&git.LogOptions{From: *headHash})
146 if len(selectorParts) == 3 {
147 topic = selectorParts[0]
149 var commits CommitIterNext
151 for i := 0; i < offset; i++ {
152 if _, err = repoLog.Next(); err != nil {
158 hashes := topicsCache[topic]
160 log.Fatalln(errors.New("no posts with that topic"))
162 if len(hashes) > offset {
163 hashes = hashes[offset:]
165 commits = &HashesIter{hashes}
169 entries := make([]TableMenuEntry, 0, PageEntries)
170 for i := 0; i < PageEntries; i++ {
171 commit, err := commits.Next()
176 lines := msgSplit(commit.Message)
177 entries = append(entries, TableMenuEntry{
180 LinesNum: len(lines) - 2,
181 CommentsNum: len(sgblog.ParseComments(sgblog.GetNote(repo, commentsTree, commit.Hash))),
182 Topics: sgblog.ParseTopics(sgblog.GetNote(repo, topicsTree, commit.Hash)),
185 offsetPrev := offset - PageEntries
189 err = TmplGopherMenu.Execute(os.Stdout, struct {
197 Entries []TableMenuEntry
205 OffsetPrev: offsetPrev,
206 OffsetNext: offset + PageEntries,
209 Topics: topicsCache.Topics(),
210 Version: sgblog.Version,
216 log.Fatalln(errors.New("unknown selector"))