From e2ca53029e0c4c28c0796d3d55bfecf28c132d2e Mon Sep 17 00:00:00 2001 From: Adam Strzelecki Date: Mon, 7 Dec 2015 00:18:12 +0100 Subject: [PATCH] Render commit msg as header + verbatim description Most commit in Git are expected to follow standard of single header line, followed by description paragraphs, separated by empty line from previous block. Previously Gogs were treating everything as single header. Now we are trying to render only first line as header, but following lines (description chunks) as a verbatim. --- modules/template/template.go | 22 +++++++++++++++++----- public/css/gogs.css | 17 +++++++++++++---- public/less/_base.less | 18 +++++++++++++----- templates/repo/commits_table.tmpl | 2 +- templates/repo/diff.tmpl | 14 ++++++-------- templates/repo/view_list.tmpl | 4 ++-- 6 files changed, 52 insertions(+), 25 deletions(-) diff --git a/modules/template/template.go b/modules/template/template.go index d0d77b017..9d63452dd 100644 --- a/modules/template/template.go +++ b/modules/template/template.go @@ -183,15 +183,27 @@ func ReplaceLeft(s, old, new string) string { } // RenderCommitMessage renders commit message with XSS-safe and special links. -func RenderCommitMessage(msg, urlPrefix string, metas map[string]string) template.HTML { +func RenderCommitMessage(full bool, msg, urlPrefix string, metas map[string]string) template.HTML { cleanMsg := template.HTMLEscapeString(msg) fullMessage := string(base.RenderIssueIndexPattern([]byte(cleanMsg), urlPrefix, metas)) msgLines := strings.Split(strings.TrimSpace(fullMessage), "\n") - for i := range msgLines { - msgLines[i] = ReplaceLeft(msgLines[i], " ", " ") + numLines := len(msgLines) + if numLines == 0 { + return template.HTML("") + } else if !full { + return template.HTML(msgLines[0]) + } else if numLines == 1 || (numLines >= 2 && len(msgLines[1]) == 0) { + // First line is a header, standalone or followed by empty line + header := fmt.Sprintf("

%s

", msgLines[0]) + if numLines >= 2 { + fullMessage = header + fmt.Sprintf("\n
%s
", strings.Join(msgLines[2:], "\n")) + } else { + fullMessage = header + } + } else { + // Non-standard git message, there is no header line + fullMessage = fmt.Sprintf("

%s

", strings.Join(msgLines, "
")) } - - fullMessage = strings.Join(msgLines, "
") return template.HTML(fullMessage) } diff --git a/public/css/gogs.css b/public/css/gogs.css index 583297afe..56a64cfd8 100755 --- a/public/css/gogs.css +++ b/public/css/gogs.css @@ -895,13 +895,22 @@ pre.raw { .ui .warning.segment { border-color: #F0C36D; } -.ui .info.header { - background-color: #d9edf7 !important; - border-color: #85c5e5; -} .ui .info.segment { border-color: #85c5e5; } +.ui .info.segment.top { + background-color: #d9edf7 !important; +} +.ui .info.segment.top h3, +.ui .info.segment.top h4 { + margin-top: 0; +} +.ui .info.segment.top h3:last-child { + margin-top: 4px; +} +.ui .info.segment.top > :last-child { + margin-bottom: 0; +} .ui .normal.header { font-weight: normal; } diff --git a/public/less/_base.less b/public/less/_base.less index bd51f2303..e35936342 100644 --- a/public/less/_base.less +++ b/public/less/_base.less @@ -196,12 +196,20 @@ pre { } } .info { - &.header { - background-color: #d9edf7 !important; - border-color: #85c5e5; - } &.segment { - border-color: #85c5e5; + border-color: #85c5e5; + &.top { + background-color: #d9edf7 !important; + h3, h4 { + margin-top: 0; + } + h3:last-child { + margin-top: 4px; + } + > :last-child { + margin-bottom: 0; + } + } } } diff --git a/templates/repo/commits_table.tmpl b/templates/repo/commits_table.tmpl index 2adc1770f..8bab2be00 100644 --- a/templates/repo/commits_table.tmpl +++ b/templates/repo/commits_table.tmpl @@ -37,7 +37,7 @@ {{ShortSha .ID.String}} - {{RenderCommitMessage .Summary $.RepoLink $.Repository.ComposeMetas}} + {{RenderCommitMessage false .Summary $.RepoLink $.Repository.ComposeMetas}} {{TimeSince .Author.When $.Lang}} diff --git a/templates/repo/diff.tmpl b/templates/repo/diff.tmpl index ba1322e43..18de976eb 100644 --- a/templates/repo/diff.tmpl +++ b/templates/repo/diff.tmpl @@ -5,14 +5,12 @@ {{if .IsDiffCompare }} {{template "repo/commits_table" .}} {{else}} -

- - {{RenderCommitMessage .Commit.Message $.RepoLink $.Repository.ComposeMetas}} -

+
+ + {{.i18n.Tr "repo.diff.browse_source"}} + + {{RenderCommitMessage true .Commit.Message $.RepoLink $.Repository.ComposeMetas}} +
{{if .Author}} diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index 9fb71908a..887ce8e25 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -10,7 +10,7 @@ {{.LastCommit.Author.Name}} {{end}} {{ShortSha .LastCommit.ID.String}} - {{RenderCommitMessage .LastCommit.Summary .RepoLink $.Repository.ComposeMetas}} + {{RenderCommitMessage false .LastCommit.Summary .RepoLink $.Repository.ComposeMetas}} @@ -44,7 +44,7 @@ {{end}} {{ShortSha $commit.ID.String}} - {{RenderCommitMessage $commit.Summary $.RepoLink $.Repository.ComposeMetas}} + {{RenderCommitMessage false $commit.Summary $.RepoLink $.Repository.ComposeMetas}} {{TimeSince $commit.Committer.When $.Lang}}