]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/http.go
Skip comments to possibly unexisting commits
[sgblog.git] / cmd / sgblog / http.go
index 4712a50f13584b98a81542a1d71cdd69c44307df..bad1cb8688e38a0342d6580c4cb30028330ef413 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
@@ -142,7 +142,7 @@ Topics: {{range .Topics}}[<tt><a href="{{$Cfg.URLPrefix}}?topic={{.}}">{{.}}</a>
 {{end}}</pre>
 <hr/>{{end}}
 
-{{if .Cfg.CommentsEmail}}[<a href="mailto:{{.Cfg.CommentsEmail}}?subject=Re:%20{{.TitleEscaped}}%20({{.Commit.Hash.String}})">leave comment</a>]{{end}}
+{{if .Cfg.CommentsEmail}}[<a href="mailto:{{.Cfg.CommentsEmail}}?subject={{.TitleEscaped}}">leave comment</a>]{{end}}
 
 <dl>{{range $idx, $comment := .Comments}}
 <dt><a name="comment{{$idx}}"><a href="#comment{{$idx}}">comment {{$idx}}</a>:</dt>
@@ -159,11 +159,11 @@ Topics: {{range .Topics}}[<tt><a href="{{$Cfg.URLPrefix}}?topic={{.}}">{{.}}</a>
 
 var (
        renderableSchemes = map[string]struct{}{
-               "ftp":    struct{}{},
-               "gopher": struct{}{},
-               "http":   struct{}{},
-               "https":  struct{}{},
-               "telnet": struct{}{},
+               "ftp":    {},
+               "gopher": {},
+               "http":   {},
+               "https":  {},
+               "telnet": {},
        }
 )
 
@@ -550,6 +550,12 @@ func serveHTTP() {
                        for _, topic := range parseTopics(getNote(topicsTree, commit.Hash)) {
                                categories = append(categories, atom.Category{Term: topic})
                        }
+                       htmlized := make([]string, 0, len(lines))
+                       htmlized = append(htmlized, "<pre>")
+                       for _, l := range lines[2:] {
+                               htmlized = append(htmlized, lineURLize(cfg.AtomBaseURL+cfg.URLPrefix, l))
+                       }
+                       htmlized = append(htmlized, "</pre>")
                        feed.Entry = append(feed.Entry, &atom.Entry{
                                Title: lines[0],
                                ID:    "urn:uuid:" + bytes2uuid(commit.Hash[:]),
@@ -561,8 +567,8 @@ func serveHTTP() {
                                Updated:   atom.Time(commit.Author.When),
                                Summary:   &atom.Text{Type: "text", Body: lines[0]},
                                Content: &atom.Text{
-                                       Type: "text",
-                                       Body: strings.Join(lines[2:], "\n"),
+                                       Type: "html",
+                                       Body: strings.Join(htmlized, "\n"),
                                },
                                Category: categories,
                        })
@@ -619,7 +625,7 @@ func serveHTTP() {
                        ))
                        commit, err = repo.CommitObject(commentedHash)
                        if err != nil {
-                               makeErr(err)
+                               continue
                        }
                        comments := parseComments(getNote(t, commentedHash))
                        if len(comments) == 0 {
@@ -629,6 +635,12 @@ func serveHTTP() {
                        lines := strings.Split(comments[len(comments)-1], "\n")
                        from := strings.TrimPrefix(lines[0], "From: ")
                        date := strings.TrimPrefix(lines[1], "Date: ")
+                       htmlized := make([]string, 0, len(lines))
+                       htmlized = append(htmlized, "<pre>")
+                       for _, l := range lines[2:] {
+                               htmlized = append(htmlized, lineURLize(cfg.AtomBaseURL+cfg.URLPrefix, l))
+                       }
+                       htmlized = append(htmlized, "</pre>")
                        idHasher.Reset()
                        idHasher.Write([]byte("COMMENT"))
                        idHasher.Write(commit.Hash[:])
@@ -650,8 +662,8 @@ func serveHTTP() {
                                Published: atom.TimeStr(date),
                                Updated:   atom.TimeStr(date),
                                Content: &atom.Text{
-                                       Type: "text",
-                                       Body: strings.Join(lines[2:], "\n"),
+                                       Type: "html",
+                                       Body: strings.Join(htmlized, "\n"),
                                },
                        })
                }
@@ -727,6 +739,15 @@ func serveHTTP() {
                                idHasher.Write([]byte("COMMENT"))
                                idHasher.Write(commit.Hash[:])
                                idHasher.Write([]byte(comment.n))
+                               htmlized := make([]string, 0, len(comment.body))
+                               htmlized = append(htmlized, "<pre>")
+                               for _, l := range comment.body {
+                                       htmlized = append(
+                                               htmlized,
+                                               lineURLize(cfg.AtomBaseURL+cfg.URLPrefix, l),
+                                       )
+                               }
+                               htmlized = append(htmlized, "</pre>")
                                feed.Entry = append(feed.Entry, &atom.Entry{
                                        Title:  fmt.Sprintf("Comment %s by %s", comment.n, comment.from),
                                        Author: &atom.Person{Name: comment.from},
@@ -743,8 +764,8 @@ func serveHTTP() {
                                        Published: atom.TimeStr(comment.date),
                                        Updated:   atom.TimeStr(comment.date),
                                        Content: &atom.Text{
-                                               Type: "text",
-                                               Body: strings.Join(comment.body, "\n"),
+                                               Type: "html",
+                                               Body: strings.Join(htmlized, "\n"),
                                        },
                                })
                        }
@@ -798,7 +819,7 @@ func serveHTTP() {
                        Version:         sgblog.Version,
                        Cfg:             cfg,
                        Title:           title,
-                       TitleEscaped:    strings.ReplaceAll(title, " ", "%20"),
+                       TitleEscaped:    url.PathEscape(fmt.Sprintf("Re: %s (%s)", title, commit.Hash)),
                        When:            when,
                        AtomCommentsURL: atomCommentsURL,
                        Parent:          parent,
@@ -822,7 +843,7 @@ func serveHTTP() {
        return
 
 AtomFinish:
-       os.Stdout.WriteString("Content-Type: text/xml; charset=UTF-8\n")
+       os.Stdout.WriteString("Content-Type: application/atom+xml; charset=UTF-8\n")
        os.Stdout.WriteString("ETag: " + etagString(etagHash) + "\n")
        if gzipWriter != nil {
                os.Stdout.WriteString("Content-Encoding: gzip\n")