diff options
author | 无闻 <u@gogs.io> | 2015-09-24 17:23:53 -0400 |
---|---|---|
committer | 无闻 <u@gogs.io> | 2015-09-24 17:23:53 -0400 |
commit | befed9c20c99840acbb91995b32103311e07b267 (patch) | |
tree | 72b839504fd84c1003ee49169f45d9a343e70abb /modules | |
parent | e787e73e2f8adc76cee471549fb04d7bbfd76b7d (diff) | |
parent | 7acbcf9ddd5058fa131d5d0eeee4ce346988ad80 (diff) | |
download | gitea-befed9c20c99840acbb91995b32103311e07b267.tar.gz gitea-befed9c20c99840acbb91995b32103311e07b267.zip |
Merge pull request #1662 from SergioBenitez/develop
Commit messages now rendered with line breaks and prefix spacing.
Diffstat (limited to 'modules')
-rw-r--r-- | modules/base/template.go | 35 |
1 files changed, 34 insertions, 1 deletions
diff --git a/modules/base/template.go b/modules/base/template.go index 2571e7d244..f5f567aded 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -96,9 +96,42 @@ func ToUtf8(content string) string { return res } +// Replaces all prefixes 'old' in 's' with 'new'. +func ReplaceLeft(s, old, new string) string { + old_len, new_len, i, n := len(old), len(new), 0, 0 + for ; i < len(s) && strings.HasPrefix(s[i:], old); n += 1 { + i += old_len + } + + // simple optimization + if n == 0 { + return s + } + + // allocating space for the new string + newLen := n*new_len + len(s[i:]) + replacement := make([]byte, newLen, newLen) + + j := 0 + for ; j < n*new_len; j += new_len { + copy(replacement[j:j+new_len], new) + } + + copy(replacement[j:], s[i:]) + return string(replacement) +} + // RenderCommitMessage renders commit message with XSS-safe and special links. func RenderCommitMessage(msg, urlPrefix string) template.HTML { - return template.HTML(string(RenderIssueIndexPattern([]byte(template.HTMLEscapeString(msg)), urlPrefix))) + cleanMsg := template.HTMLEscapeString(msg) + fullMessage := string(RenderIssueIndexPattern([]byte(cleanMsg), urlPrefix)) + msgLines := strings.Split(strings.TrimSpace(fullMessage), "\n") + for i := range msgLines { + msgLines[i] = ReplaceLeft(msgLines[i], " ", " ") + } + + fullMessage = strings.Join(msgLines, "<br>") + return template.HTML(fullMessage) } var TemplateFuncs template.FuncMap = map[string]interface{}{ |