From: Sergey Matveev Date: Wed, 15 Jan 2020 07:56:26 +0000 (+0300) Subject: Render URLs in comments too X-Git-Tag: v0.2.0~3 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=bbd16dce5ecdc4158c611d9ceac13fabc9053299;p=sgblog.git Render URLs in comments too --- 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("\n
\n")) if len(notesRaw) > 0 { @@ -565,9 +567,17 @@ func main() { for i, comment := range parseComments(commentsRaw) { out.Write([]byte(fmt.Sprintf( "
comment %d:"+ - "
\n
\n%s\n
\n", - i, i, i, html.EscapeString(comment), + "\n
\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("
\n")) } out.Write([]byte("\n")) } else {