]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/gopher.go
sgblog-topics helper command
[sgblog.git] / cmd / sgblog / gopher.go
index 86fb5692d81ea055c8b16cf98aac7979007c267e..f682c41debd460c7185948587ef26ce33ebbdd08 100644 (file)
@@ -1,6 +1,6 @@
 /*
 SGBlog -- Git-backed CGI/inetd blogging/phlogging engine
-Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2020-2021 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
@@ -19,12 +19,11 @@ package main
 
 import (
        "bufio"
-       "encoding/json"
        "errors"
        "fmt"
        "io"
-       "io/ioutil"
        "log"
+       "net/url"
        "os"
        "strconv"
        "strings"
@@ -33,7 +32,6 @@ import (
        "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/hjson/hjson-go"
        "go.stargrave.org/sgblog"
 )
 
@@ -66,7 +64,7 @@ Note:
 {{.Note}}{{end -}}
 {{- if .Cfg.CommentsEmail}}
 ------------------------------------------------------------------------
-leave comment: mailto:{{.Cfg.CommentsEmail}}?subject={{.Commit.Hash.String}}
+leave comment: mailto:{{.Cfg.CommentsEmail}}?subject={{.TitleEscaped}}
 {{end}}{{range $idx, $comment := .Comments}}
 ------------------------------------------------------------------------
 comment {{$idx}}:
@@ -85,24 +83,11 @@ type TableMenuEntry struct {
        Topics      []string
 }
 
-func serveGopher() {
-       cfgPath := os.Args[2]
-       cfgRaw, err := ioutil.ReadFile(cfgPath)
+func serveGopher(cfgPath string) {
+       cfg, err := readCfg(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)
-       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")
        }
@@ -140,21 +125,25 @@ Redirecting to <a href="%s">%s</a>...
                }
                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
-                       Topics   []string
-                       Version  string
+                       Commit       *object.Commit
+                       When         string
+                       Cfg          *Cfg
+                       Note         string
+                       Comments     []string
+                       Topics       []string
+                       Version      string
+                       TitleEscaped string
                }{
                        Commit:   commit,
                        When:     commit.Author.When.Format(sgblog.WhenFmt),
                        Cfg:      cfg,
-                       Note:     string(getNote(notesTree, commit.Hash)),
-                       Comments: parseComments(getNote(commentsTree, commit.Hash)),
-                       Topics:   parseTopics(getNote(topicsTree, commit.Hash)),
+                       Note:     string(sgblog.GetNote(repo, notesTree, 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)
@@ -213,8 +202,8 @@ Redirecting to <a href="%s">%s</a>...
                                Commit:      commit,
                                Title:       lines[0],
                                LinesNum:    len(lines) - 2,
-                               CommentsNum: len(parseComments(getNote(commentsTree, commit.Hash))),
-                               Topics:      parseTopics(getNote(topicsTree, 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))