From bbd16dce5ecdc4158c611d9ceac13fabc9053299 Mon Sep 17 00:00:00 2001
From: Sergey Matveev <stargrave@stargrave.org>
Date: Wed, 15 Jan 2020 10:56:26 +0300
Subject: [PATCH] Render URLs in comments too

---
 cmd/sgblog/main.go | 40 +++++++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 15 deletions(-)

diff --git a/cmd/sgblog/main.go b/cmd/sgblog/main.go
index 3611893..4dc9fb2 100644
--- a/cmd/sgblog/main.go
+++ b/cmd/sgblog/main.go
@@ -119,6 +119,20 @@ func msgSplit(msg string) []string {
 	return lines
 }
 
+func lineURLize(urlPrefix, line string) string {
+	cols := strings.Split(html.EscapeString(line), " ")
+	for i, col := range cols {
+		if u := urlParse(col); u != nil {
+			cols[i] = makeA(col, col)
+			continue
+		}
+		cols[i] = sha1DigestRe.ReplaceAllString(col, makeA(
+			urlPrefix+"/$1", "$1",
+		))
+	}
+	return strings.Join(cols, " ")
+}
+
 func getNote(tree *object.Tree, what plumbing.Hash) []byte {
 	if tree == nil {
 		return nil
@@ -537,19 +551,7 @@ func main() {
 			when, commit.Hash.String(), title,
 		)))
 		for _, line := range lines[2:] {
-			line = html.EscapeString(line)
-			cols := strings.Split(line, " ")
-			for i, col := range cols {
-				if u := urlParse(col); u != nil {
-					cols[i] = makeA(col, col)
-					continue
-				}
-				cols[i] = sha1DigestRe.ReplaceAllString(col, makeA(
-					cfg.URLPrefix+"/$1", "$1",
-				))
-			}
-			line = strings.Join(cols, " ")
-			out.Write([]byte(line + "\n"))
+			out.Write([]byte(lineURLize(cfg.URLPrefix, line) + "\n"))
 		}
 		out.Write([]byte("</pre>\n<hr/>\n"))
 		if len(notesRaw) > 0 {
@@ -565,9 +567,17 @@ func main() {
 		for i, comment := range parseComments(commentsRaw) {
 			out.Write([]byte(fmt.Sprintf(
 				"<dt><a name=\"comment%d\"><a href=\"#comment%d\">comment %d</a>:"+
-					"</dt>\n<dd><pre>\n%s\n</pre></dd>\n",
-				i, i, i, html.EscapeString(comment),
+					"</dt>\n<dd><pre>\n",
+				i, i, i,
 			)))
+			lines = strings.Split(comment, "\n")
+			for _, line := range lines[:3] {
+				out.Write([]byte(line + "\n"))
+			}
+			for _, line := range lines[3:] {
+				out.Write([]byte(lineURLize(cfg.URLPrefix, line) + "\n"))
+			}
+			out.Write([]byte("</pre></dd>\n"))
 		}
 		out.Write([]byte("</dl>\n"))
 	} else {
-- 
2.51.0