2 SGBlog -- Git-backed CGI/inetd blogging/phlogging engine
3 Copyright (C) 2020-2021 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/>.
34 "github.com/go-git/go-git/v5"
35 "github.com/go-git/go-git/v5/plumbing"
36 "github.com/go-git/go-git/v5/plumbing/object"
37 "github.com/hjson/hjson-go"
38 "go.stargrave.org/sgblog"
42 TmplGopherMenu = `{{$CR := printf "\r"}}{{$CRLF := printf "\r\n" -}}
43 {{- define "domainPort" }} {{.GopherDomain}} 70{{end}}{{$Cfg := .Cfg -}}
44 i{{.Cfg.Title}} {{if .Topic}}(topic: {{.Topic}}) {{end}}({{.Offset}}-{{.OffsetNext}}) err{{template "domainPort" .Cfg}}{{$CRLF -}}
45 {{- if .Cfg.AboutURL}}hAbout URL:{{.Cfg.AboutURL}}{{template "domainPort" .Cfg}}{{$CRLF}}{{end -}}
46 {{- if .Offset}}1Prev {{if .Topic}}{{.Topic}}/{{end}}offset/{{.OffsetPrev}}{{template "domainPort" .Cfg}}{{$CRLF}}{{end -}}
47 {{- if not .LogEnded}}1Next {{if .Topic}}{{.Topic}}/{{end}}offset/{{.OffsetNext}}{{template "domainPort" .Cfg}}{{$CRLF}}{{end -}}
48 {{- $datePrev := "0001-01-01" -}}
49 {{- range .Entries -}}
50 {{- $dateCur := .Commit.Author.When.Format "2006-01-02" -}}
51 {{- if ne $dateCur $datePrev}}{{$datePrev = $dateCur}}
52 i{{$dateCur}} err{{template "domainPort" $Cfg}}{{$CR}}{{end}}
53 0[{{.Commit.Author.When.Format "15:04"}}] {{.Title}} ({{.LinesNum}}L){{with .CommentsNum}} ({{.}}C){{end}}{{if .Topics}}{{range .Topics}} {{.}}{{end}}{{end}} /{{.Commit.Hash.String}}{{template "domainPort" $Cfg}}{{$CR}}{{end}}
55 1Topic: {{.}} {{.}}/offset/0{{template "domainPort" $Cfg}}{{$CR}}{{end}}
56 iGenerated by: SGBlog {{.Version}} err{{template "domainPort" .Cfg}}{{$CR}}
58 TmplGopherEntry = `What: {{.Commit.Hash.String}}
60 ------------------------------------------------------------------------
61 {{if .Topics}}Topics:{{range .Topics}} {{.}}{{end}}{{end}}
62 ------------------------------------------------------------------------
65 ------------------------------------------------------------------------
68 {{- if .Cfg.CommentsEmail}}
69 ------------------------------------------------------------------------
70 leave comment: mailto:{{.Cfg.CommentsEmail}}?subject={{.TitleEscaped}}
71 {{end}}{{range $idx, $comment := .Comments}}
72 ------------------------------------------------------------------------
76 ------------------------------------------------------------------------
77 Generated by: SGBlog {{.Version}}
81 type TableMenuEntry struct {
91 cfgRaw, err := ioutil.ReadFile(cfgPath)
95 var cfgGeneral map[string]interface{}
96 if err = hjson.Unmarshal(cfgRaw, &cfgGeneral); err != nil {
99 cfgRaw, err = json.Marshal(cfgGeneral)
104 if err = json.Unmarshal(cfgRaw, &cfg); err != nil {
107 if cfg.GopherDomain == "" {
108 log.Fatalln("GopherDomain is not configured")
111 headHash, err := initRepo(cfg)
116 scanner := bufio.NewScanner(io.LimitReader(os.Stdin, 1<<8))
118 log.Fatalln(errors.New("no CRLF found"))
120 selector := scanner.Text()
122 selector = "offset/0"
124 selectorParts := strings.Split(selector, "/")
125 if strings.HasPrefix(selector, "URL:") {
126 selector = selector[len("URL:"):]
129 <meta http-equiv="Refresh" content="1; url=%s" />
130 <title>Redirect to non-gopher URL</title>
133 Redirecting to <a href="%s">%s</a>...
136 `, selector, selector, selector)
137 } else if sha1DigestRe.MatchString(selector) {
138 commit, err := repo.CommitObject(plumbing.NewHash(selector[1:]))
142 tmpl := template.Must(template.New("entry").Parse(TmplGopherEntry))
143 err = tmpl.Execute(os.Stdout, struct {
144 Commit *object.Commit
154 When: commit.Author.When.Format(sgblog.WhenFmt),
156 Note: string(getNote(notesTree, commit.Hash)),
157 Comments: parseComments(getNote(commentsTree, commit.Hash)),
158 Topics: parseTopics(getNote(topicsTree, commit.Hash)),
159 Version: sgblog.Version,
160 TitleEscaped: url.PathEscape(fmt.Sprintf(
161 "Re: %s (%s)", msgSplit(commit.Message)[0], commit.Hash,
167 } else if selectorParts[len(selectorParts)-2] == "offset" {
168 offset, err := strconv.Atoi(selectorParts[len(selectorParts)-1])
172 repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
176 topicsCache, err := getTopicsCache(cfg, repoLog)
180 repoLog, err = repo.Log(&git.LogOptions{From: *headHash})
186 if len(selectorParts) == 3 {
187 topic = selectorParts[0]
189 var commits CommitIterNext
191 for i := 0; i < offset; i++ {
192 if _, err = repoLog.Next(); err != nil {
198 hashes := topicsCache[topic]
200 log.Fatalln(errors.New("no posts with that topic"))
202 if len(hashes) > offset {
203 hashes = hashes[offset:]
205 commits = &HashesIter{hashes}
209 entries := make([]TableMenuEntry, 0, PageEntries)
210 for i := 0; i < PageEntries; i++ {
211 commit, err := commits.Next()
216 lines := msgSplit(commit.Message)
217 entries = append(entries, TableMenuEntry{
220 LinesNum: len(lines) - 2,
221 CommentsNum: len(parseComments(getNote(commentsTree, commit.Hash))),
222 Topics: parseTopics(getNote(topicsTree, commit.Hash)),
225 tmpl := template.Must(template.New("menu").Parse(TmplGopherMenu))
226 offsetPrev := offset - PageEntries
230 err = tmpl.Execute(os.Stdout, struct {
237 Entries []TableMenuEntry
244 OffsetPrev: offsetPrev,
245 OffsetNext: offset + PageEntries,
248 Topics: topicsCache.Topics(),
249 Version: sgblog.Version,
255 log.Fatalln(errors.New("unknown selector"))