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/>.
32 "github.com/go-git/go-git/v5"
33 "github.com/go-git/go-git/v5/plumbing"
34 "github.com/go-git/go-git/v5/plumbing/object"
35 "github.com/vorlif/spreak"
36 "go.stargrave.org/sgblog"
40 //go:embed gemini-menu.tmpl
42 //go:embed gemini-entry.tmpl
43 TmplGemEntryRaw string
45 TmplGemMenu = template.Must(template.New("menu").Parse(TmplGemMenuRaw))
46 TmplGemEntry = template.Must(template.New("entry").Parse(TmplGemEntryRaw))
49 func makeGemErr(err error) {
50 fmt.Print("59 " + err.Error() + "\r\n")
54 func serveGemini(cfgPath string) {
55 cfg, err := readCfg(cfgPath)
59 initLocalizer(cfg.Lang)
61 headHash, err := initRepo(cfg)
66 scanner := bufio.NewScanner(io.LimitReader(os.Stdin, 2+1<<10))
68 makeGemErr(errors.New("no CRLF found"))
70 urlRaw := scanner.Text()
71 u, err := url.Parse(urlRaw)
75 if u.Scheme != "gemini" {
76 makeGemErr(errors.New("only gemini:// is supported" + u.String()))
81 if offsetRaw, exists := u.Query()["offset"]; exists {
82 offset, err = strconv.Atoi(offsetRaw[0])
87 repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
91 topicsCache, err := getTopicsCache(cfg, repoLog)
95 repoLog, err = repo.Log(&git.LogOptions{From: *headHash})
101 var commits CommitIterNext
103 if t, exists := u.Query()["topic"]; exists {
105 hashes := topicsCache[topic]
107 makeGemErr(errors.New("no posts with that topic"))
109 if len(hashes) > offset {
110 hashes = hashes[offset:]
113 commits = &HashesIter{hashes}
115 for i := 0; i < offset; i++ {
116 if _, err = repoLog.Next(); err != nil {
125 entries := make([]TableMenuEntry, 0, PageEntries)
126 for i := 0; i < PageEntries; i++ {
127 commit, err := commits.Next()
132 lines := msgSplit(commit.Message)
133 entries = append(entries, TableMenuEntry{
136 LinesNum: len(lines) - 2,
137 CommentsNum: len(sgblog.ParseComments(sgblog.GetNote(
138 repo, commentsTree, commit.Hash,
140 Topics: sgblog.ParseTopics(sgblog.GetNote(
141 repo, topicsTree, commit.Hash,
145 offsetPrev := offset - PageEntries
149 err = TmplGemMenu.Execute(os.Stdout, struct {
157 Entries []TableMenuEntry
165 OffsetPrev: offsetPrev,
166 OffsetNext: offset + PageEntries,
169 Topics: topicsCache.Topics(),
170 Version: sgblog.Version,
175 } else if sha1DigestRe.MatchString(u.Path[1:]) {
176 commit, err := repo.CommitObject(plumbing.NewHash(u.Path[1:]))
180 title := msgSplit(commit.Message)[0]
181 err = TmplGemEntry.Execute(os.Stdout, struct {
184 Commit *object.Commit
196 When: commit.Author.When.Format(sgblog.WhenFmt),
198 Note: string(sgblog.GetNote(repo, notesTree, commit.Hash)),
199 Comments: sgblog.ParseComments(sgblog.GetNote(repo, commentsTree, commit.Hash)),
200 Topics: sgblog.ParseTopics(sgblog.GetNote(repo, topicsTree, commit.Hash)),
201 Version: sgblog.Version,
202 TitleEscaped: url.PathEscape(fmt.Sprintf(
203 "Re: %s (%s)", title, commit.Hash,
210 makeGemErr(errors.New("unknown URL action"))