]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/gopher.go
Raised copyright years
[sgblog.git] / cmd / sgblog / gopher.go
index b12a2b893e1e1c02e180f70f8368f742d54d157b..966d94a19284106848d89e4a480c425230c102af 100644 (file)
@@ -1,6 +1,6 @@
 /*
-SGBlog -- Git-backed CGI/inetd blogging/phlogging engine
-Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
+SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
+Copyright (C) 2020-2022 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,6 +19,7 @@ package main
 
 import (
        "bufio"
+       _ "embed"
        "errors"
        "fmt"
        "io"
@@ -35,44 +36,14 @@ import (
        "go.stargrave.org/sgblog"
 )
 
-const (
-       TmplGopherMenu = `{{$CR := printf "\r"}}{{$CRLF := printf "\r\n" -}}
-{{- define "domainPort" }}     {{.GopherDomain}}       70{{end}}{{$Cfg := .Cfg -}}
-i{{.Cfg.Title}} {{if .Topic}}(topic: {{.Topic}}) {{end}}({{.Offset}}-{{.OffsetNext}})  err{{template "domainPort" .Cfg}}{{$CRLF -}}
-{{- if .Cfg.AboutURL}}hAbout   URL:{{.Cfg.AboutURL}}{{template "domainPort" .Cfg}}{{$CRLF}}{{end -}}
-{{- if .Offset}}1Prev  {{if .Topic}}{{.Topic}}/{{end}}offset/{{.OffsetPrev}}{{template "domainPort" .Cfg}}{{$CRLF}}{{end -}}
-{{- if not .LogEnded}}1Next    {{if .Topic}}{{.Topic}}/{{end}}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}}{{if .Topics}}{{range .Topics}} {{.}}{{end}}{{end}}  /{{.Commit.Hash.String}}{{template "domainPort" $Cfg}}{{$CR}}{{end}}
-{{range .Topics}}
-1Topic: {{.}}  {{.}}/offset/0{{template "domainPort" $Cfg}}{{$CR}}{{end}}
-iGenerated by: SGBlog {{.Version}}     err{{template "domainPort" .Cfg}}{{$CR}}
-.{{$CRLF}}`
-       TmplGopherEntry = `What: {{.Commit.Hash.String}}
-When: {{.When}}
-------------------------------------------------------------------------
-{{if .Topics}}Topics:{{range .Topics}} {{.}}{{end}}{{end}}
-------------------------------------------------------------------------
-{{.Commit.Message -}}
-{{- if .Note}}
-------------------------------------------------------------------------
-Note:
-{{.Note}}{{end -}}
-{{- if .Cfg.CommentsEmail}}
-------------------------------------------------------------------------
-leave comment: mailto:{{.Cfg.CommentsEmail}}?subject={{.TitleEscaped}}
-{{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 {
@@ -108,7 +79,8 @@ func serveGopher(cfgPath string) {
        selectorParts := strings.Split(selector, "/")
        if strings.HasPrefix(selector, "URL:") {
                selector = selector[len("URL:"):]
-               fmt.Printf(`<html>
+               fmt.Printf(`<!DOCTYPE html>
+<html>
 <head>
        <meta http-equiv="Refresh" content="1; url=%s" />
        <title>Redirect to non-gopher URL</title>
@@ -123,8 +95,7 @@ Redirecting to <a href="%s">%s</a>...
                if err != nil {
                        log.Fatalln(err)
                }
-               tmpl := template.Must(template.New("entry").Parse(TmplGopherEntry))
-               err = tmpl.Execute(os.Stdout, struct {
+               err = TmplGopherEntry.Execute(os.Stdout, struct {
                        Commit       *object.Commit
                        When         string
                        Cfg          *Cfg
@@ -137,9 +108,9 @@ Redirecting to <a href="%s">%s</a>...
                        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,
@@ -148,7 +119,8 @@ Redirecting to <a href="%s">%s</a>...
                if err != nil {
                        log.Fatalln(err)
                }
-       } else if selectorParts[len(selectorParts)-2] == "offset" {
+       } else if len(selectorParts) > 1 &&
+               selectorParts[len(selectorParts)-2] == "offset" {
                offset, err := strconv.Atoi(selectorParts[len(selectorParts)-1])
                if err != nil {
                        log.Fatalln(err)
@@ -202,16 +174,15 @@ 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))
                offsetPrev := offset - PageEntries
                if offsetPrev < 0 {
                        offsetPrev = 0
                }
-               err = tmpl.Execute(os.Stdout, struct {
+               err = TmplGopherMenu.Execute(os.Stdout, struct {
                        Cfg        *Cfg
                        Topic      string
                        Offset     int