]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/gopher.go
Unify copyright comment format
[sgblog.git] / cmd / sgblog / gopher.go
index 59e2607644ec8d8c9da44175283fb2e54c592ffb..0eb5e3e58ff69276e91bcc7812f030cf77473176 100644 (file)
-/*
-SGBlog -- Git-backed CGI/inetd blogging/phlogging engine
-Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU Affero General Public License as
-published by the Free Software Foundation, version 3 of the License.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU Affero General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
-*/
+// SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
+// Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, version 3 of the License.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 package main
 
 import (
        "bufio"
-       "encoding/json"
+       _ "embed"
        "errors"
        "fmt"
        "io"
-       "io/ioutil"
        "log"
+       "net/url"
        "os"
        "strconv"
        "strings"
        "text/template"
 
-       "github.com/hjson/hjson-go"
+       "github.com/go-git/go-git/v5"
+       "github.com/go-git/go-git/v5/plumbing"
+       "github.com/go-git/go-git/v5/plumbing/object"
+       "github.com/vorlif/spreak"
        "go.stargrave.org/sgblog"
-       "gopkg.in/src-d/go-git.v4"
-       "gopkg.in/src-d/go-git.v4/plumbing"
-       "gopkg.in/src-d/go-git.v4/plumbing/object"
 )
 
-const (
-       TmplGopherMenu = `{{$CR := printf "\r"}}{{$CRLF := printf "\r\n" -}}
-{{- define "domainPort" }}     {{.GopherDomain}}       70{{end}}{{$Cfg := .Cfg -}}
-i{{.Cfg.Title}} ({{.Offset}}-{{.OffsetNext}})  err{{template "domainPort" .Cfg}}{{$CRLF -}}
-{{- if .Cfg.AboutURL}}hAbout   URL:{{.Cfg.AboutURL}}{{template "domainPort" .Cfg}}{{$CRLF}}{{end -}}
-{{- if .Offset}}1Prev  offset/{{.OffsetPrev}}{{template "domainPort" .Cfg}}{{$CRLF}}{{end -}}
-{{- if not .LogEnded}}1Next    offset/{{.OffsetNext}}{{template "domainPort" .Cfg}}{{$CRLF}}{{end -}}
-{{- $datePrev := "0001-01-01" -}}
-{{- range .Entries -}}
-{{- $dateCur := .Commit.Author.When.Format "2006-01-02" -}}
-{{- if ne $dateCur $datePrev}}{{$datePrev = $dateCur}}
-i{{$dateCur}}  err{{template "domainPort" $Cfg}}{{$CR}}{{end}}
-0[{{.Commit.Author.When.Format "15:04"}}] {{.Title}} ({{.LinesNum}}L){{with .CommentsNum}} ({{.}}C){{end}}     /{{.Commit.Hash.String}}{{template "domainPort" $Cfg}}{{$CR}}{{end}}
-iGenerated by: SGBlog {{.Version}}     err{{template "domainPort" .Cfg}}{{$CR}}
-.{{$CRLF}}`
-       TmplGopherEntry = `What: {{.Commit.Hash.String}}
-When: {{.When}}
-------------------------------------------------------------------------
-{{.Commit.Message -}}
-{{- if .Note}}
-------------------------------------------------------------------------
-Note:
-{{.Note}}{{end -}}
-{{- if .Cfg.CommentsEmail}}
-------------------------------------------------------------------------
-leave comment: mailto:{{.Cfg.CommentsEmail}}?subject={{.Commit.Hash.String}}
-{{end}}{{range $idx, $comment := .Comments}}
-------------------------------------------------------------------------
-comment {{$idx}}:
-{{$comment}}
-{{end}}
-------------------------------------------------------------------------
-Generated by: SGBlog {{.Version}}
-`
+var (
+       //go:embed gopher-menu.tmpl
+       TmplGopherMenuRaw string
+       //go:embed gopher-entry.tmpl
+       TmplGopherEntryRaw string
+
+       TmplGopherMenu  = template.Must(template.New("gopher-menu").Parse(TmplGopherMenuRaw))
+       TmplGopherEntry = template.Must(template.New("gopher-entry").Parse(TmplGopherEntryRaw))
 )
 
 type TableMenuEntry struct {
        Commit      *object.Commit
        Title       string
        LinesNum    int
+       ImagesNum   int
        CommentsNum int
+       Topics      []string
 }
 
-func serveGopher() {
-       cfgPath := os.Args[2]
-       cfgRaw, err := ioutil.ReadFile(cfgPath)
-       if err != nil {
-               log.Fatalln(err)
-       }
-       var cfgGeneral map[string]interface{}
-       if err = hjson.Unmarshal(cfgRaw, &cfgGeneral); err != nil {
-               log.Fatalln(err)
-       }
-       cfgRaw, err = json.Marshal(cfgGeneral)
+func serveGopher(cfgPath string) {
+       cfg, err := readCfg(cfgPath)
        if err != nil {
                log.Fatalln(err)
        }
-       var cfg *Cfg
-       if err = json.Unmarshal(cfgRaw, &cfg); err != nil {
-               log.Fatalln(err)
-       }
        if cfg.GopherDomain == "" {
                log.Fatalln("GopherDomain is not configured")
        }
+       initLocalizer(cfg.Lang)
 
        headHash, err := initRepo(cfg)
        if err != nil {
@@ -115,8 +77,56 @@ func serveGopher() {
        if selector == "" {
                selector = "offset/0"
        }
-       if strings.HasPrefix(selector, "offset/") {
-               offset, err := strconv.Atoi(selector[len("offset/"):])
+       selectorParts := strings.Split(selector, "/")
+       if strings.HasPrefix(selector, "URL:") {
+               selector = selector[len("URL:"):]
+               fmt.Printf(`<!DOCTYPE html>
+<html>
+<head>
+       <meta http-equiv="Refresh" content="1; url=%s" />
+       <title>Redirect to non-gopher URL</title>
+</head>
+<body>
+Redirecting to <a href="%s">%s</a>...
+</body>
+</html>
+`, selector, selector, selector)
+       } else if sha1DigestRe.MatchString(selector) {
+               commit, err := repo.CommitObject(plumbing.NewHash(selector[1:]))
+               if err != nil {
+                       log.Fatalln(err)
+               }
+               err = TmplGopherEntry.Execute(os.Stdout, struct {
+                       T            *spreak.Localizer
+                       Commit       *object.Commit
+                       When         string
+                       Cfg          *Cfg
+                       Note         string
+                       Images       []Img
+                       Comments     []string
+                       Topics       []string
+                       Version      string
+                       TitleEscaped string
+               }{
+                       T:        localizer,
+                       Commit:   commit,
+                       When:     commit.Author.When.Format(sgblog.WhenFmt),
+                       Cfg:      cfg,
+                       Note:     string(sgblog.GetNote(repo, notesTree, commit.Hash)),
+                       Images:   listImgs(cfg, commit.Hash),
+                       Comments: sgblog.ParseComments(sgblog.GetNote(repo, commentsTree, commit.Hash)),
+                       Topics:   sgblog.ParseTopics(sgblog.GetNote(repo, topicsTree, commit.Hash)),
+                       Version:  sgblog.Version,
+                       TitleEscaped: url.PathEscape(fmt.Sprintf(
+                               "Re: %s (%s)", msgSplit(commit.Message)[0], commit.Hash,
+                       )),
+               })
+               if err != nil {
+                       log.Fatalln(err)
+               }
+       } else if len(selectorParts) > 1 &&
+               selectorParts[len(selectorParts)-2] == "offset" {
+               offset, err := strconv.Atoi(selectorParts[len(selectorParts)-1])
                if err != nil {
                        log.Fatalln(err)
                }
@@ -124,84 +134,90 @@ func serveGopher() {
                if err != nil {
                        log.Fatalln(err)
                }
-               for i := 0; i < offset; i++ {
-                       if _, err = repoLog.Next(); err != nil {
-                               break
+               topicsCache, err := getTopicsCache(cfg, repoLog)
+               if err != nil {
+                       log.Fatalln(err)
+               }
+               repoLog, err = repo.Log(&git.LogOptions{From: *headHash})
+               if err != nil {
+                       log.Fatalln(err)
+               }
+
+               var topic string
+               if len(selectorParts) == 3 {
+                       topic = selectorParts[0]
+               }
+               var commits CommitIterNext
+               if topic == "" {
+                       for i := 0; i < offset; i++ {
+                               if _, err = repoLog.Next(); err != nil {
+                                       break
+                               }
+                       }
+                       commits = repoLog
+               } else {
+                       hashes := topicsCache[topic]
+                       if hashes == nil {
+                               log.Fatalln(errors.New("no posts with that topic"))
                        }
+                       if len(hashes) > offset {
+                               hashes = hashes[offset:]
+                       }
+                       commits = &HashesIter{hashes}
                }
+
                logEnded := false
                entries := make([]TableMenuEntry, 0, PageEntries)
                for i := 0; i < PageEntries; i++ {
-                       commit, err := repoLog.Next()
+                       commit, err := commits.Next()
                        if err != nil {
                                logEnded = true
                                break
                        }
                        lines := msgSplit(commit.Message)
                        entries = append(entries, TableMenuEntry{
-                               Commit:      commit,
-                               Title:       lines[0],
-                               LinesNum:    len(lines) - 2,
-                               CommentsNum: len(parseComments(getNote(commentsTree, commit.Hash))),
+                               Commit:    commit,
+                               Title:     lines[0],
+                               LinesNum:  len(lines) - 2,
+                               ImagesNum: len(listImgs(cfg, commit.Hash)),
+                               CommentsNum: len(sgblog.ParseComments(sgblog.GetNote(
+                                       repo, commentsTree, commit.Hash,
+                               ))),
+                               Topics: sgblog.ParseTopics(sgblog.GetNote(
+                                       repo, topicsTree, commit.Hash,
+                               )),
                        })
                }
-               tmpl := template.Must(template.New("menu").Parse(TmplGopherMenu))
-               err = tmpl.Execute(os.Stdout, struct {
+               offsetPrev := offset - PageEntries
+               if offsetPrev < 0 {
+                       offsetPrev = 0
+               }
+               err = TmplGopherMenu.Execute(os.Stdout, struct {
+                       T          *spreak.Localizer
                        Cfg        *Cfg
+                       Topic      string
                        Offset     int
                        OffsetPrev int
                        OffsetNext int
                        LogEnded   bool
                        Entries    []TableMenuEntry
+                       Topics     []string
                        Version    string
                }{
+                       T:          localizer,
                        Cfg:        cfg,
+                       Topic:      topic,
                        Offset:     offset,
-                       OffsetPrev: offset - PageEntries,
+                       OffsetPrev: offsetPrev,
                        OffsetNext: offset + PageEntries,
                        LogEnded:   logEnded,
                        Entries:    entries,
+                       Topics:     topicsCache.Topics(),
                        Version:    sgblog.Version,
                })
                if err != nil {
                        log.Fatalln(err)
                }
-       } else if strings.HasPrefix(selector, "URL:") {
-               selector = selector[len("URL:"):]
-               fmt.Printf(`<html>
-<head>
-       <meta http-equiv="Refresh" content="1; url=%s" />
-       <title>Redirect to non-gopher URL</title>
-</head>
-<body>
-Redirecting to <a href="%s">%s</a>...
-</body>
-</html>
-`, selector, selector, selector)
-       } else if sha1DigestRe.MatchString(selector) {
-               commit, err := repo.CommitObject(plumbing.NewHash(selector[1:]))
-               if err != nil {
-                       log.Fatalln(err)
-               }
-               tmpl := template.Must(template.New("entry").Parse(TmplGopherEntry))
-               err = tmpl.Execute(os.Stdout, struct {
-                       Commit   *object.Commit
-                       When     string
-                       Cfg      *Cfg
-                       Note     string
-                       Comments []string
-                       Version  string
-               }{
-                       Commit:   commit,
-                       When:     commit.Author.When.Format(sgblog.WhenFmt),
-                       Cfg:      cfg,
-                       Note:     string(getNote(notesTree, commit.Hash)),
-                       Comments: parseComments(getNote(commentsTree, commit.Hash)),
-                       Version:  sgblog.Version,
-               })
-               if err != nil {
-                       log.Fatalln(err)
-               }
        } else {
                log.Fatalln(errors.New("unknown selector"))
        }