From 7b966626c0af21e3ed2b7c26d4c7a733b130e872 Mon Sep 17 00:00:00 2001 From: Dennis Smith Date: Thu, 27 Nov 2014 00:13:18 +0100 Subject: [PATCH] linking issues and commits --- modules/base/markdown.go | 27 ++++++++++++++++++++++----- routers/repo/commit.go | 23 ++++++++++++++++++++++- routers/repo/view.go | 1 + templates/repo/commits_table.tmpl | 2 +- templates/repo/diff.tmpl | 8 ++++---- templates/repo/view_list.tmpl | 13 ++++++++----- 6 files changed, 58 insertions(+), 16 deletions(-) diff --git a/modules/base/markdown.go b/modules/base/markdown.go index 7b371dbd9..57676ed70 100644 --- a/modules/base/markdown.go +++ b/modules/base/markdown.go @@ -100,10 +100,11 @@ func (options *CustomRender) Image(out *bytes.Buffer, link []byte, title []byte, } var ( - MentionPattern = regexp.MustCompile(`@[0-9a-zA-Z_]{1,}`) - commitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`) - issueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`) - issueIndexPattern = regexp.MustCompile(`#[0-9]+`) + MentionPattern = regexp.MustCompile(`@[0-9a-zA-Z_]{1,}`) + commitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`) + issueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`) + issueIndexPattern = regexp.MustCompile(`#[0-9]+`) + sha1CurrentPattern = regexp.MustCompile(`\b[0-9a-f]{5,40}\b`) ) func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte { @@ -153,7 +154,23 @@ func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte { rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf( ` #%s`, m, ShortSha(string(m[i+7:j])))), -1) } - ms = issueIndexPattern.FindAll(rawBytes, -1) + rawBytes = RenderissueIndexPattern(rawBytes, urlPrefix) + rawBytes = RenderSha1CurrentPattern(rawBytes, urlPrefix) + + return rawBytes +} + +func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte { + ms := sha1CurrentPattern.FindAll(rawBytes, -1) + for _, m := range ms { + rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf( + `%s`, urlPrefix, m, ShortSha(string(m)))), -1) + } + return rawBytes +} + +func RenderissueIndexPattern(rawBytes []byte, urlPrefix string) []byte { + ms := issueIndexPattern.FindAll(rawBytes, -1) for _, m := range ms { rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf( `%s`, urlPrefix, m[1:], m)), -1) diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 1174e6266..2190fc82b 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -5,12 +5,14 @@ package repo import ( + "container/list" "path" "github.com/Unknwon/com" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/base" + "github.com/gogits/gogs/modules/git" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/setting" ) @@ -72,6 +74,8 @@ func Commits(ctx *middleware.Context) { ctx.Handle(500, "CommitsByRange", err) return } + + commits = RenderMarkDOwnCommitMessage(commits, ctx.Repo.RepoLink) commits = models.ValidateCommitsWithEmails(commits) ctx.Data["Commits"] = commits @@ -83,6 +87,21 @@ func Commits(ctx *middleware.Context) { ctx.HTML(200, COMMITS) } +func RenderMarkDOwnCommitMessage(oldCommits *list.List, repoLink string) *list.List { + newCommits := list.New() + + for e := oldCommits.Front(); e != nil; e = e.Next() { + + c := e.Value.(*git.Commit) + + c.CommitMessage = string(base.RenderMarkdown([]byte(c.CommitMessage), repoLink)) + + newCommits.PushBack(c) + + } + return newCommits +} + func SearchCommits(ctx *middleware.Context) { ctx.Data["IsSearchPage"] = true ctx.Data["IsRepoToolbarCommits"] = true @@ -110,6 +129,7 @@ func SearchCommits(ctx *middleware.Context) { ctx.Handle(500, "SearchCommits", err) return } + commits = RenderMarkDOwnCommitMessage(commits, ctx.Repo.RepoLink) commits = models.ValidateCommitsWithEmails(commits) ctx.Data["Keyword"] = keyword @@ -171,6 +191,7 @@ func FileHistory(ctx *middleware.Context) { ctx.Handle(500, "repo.FileHistory(CommitsByRange)", err) return } + commits = RenderMarkDOwnCommitMessage(commits, ctx.Repo.RepoLink) commits = models.ValidateCommitsWithEmails(commits) ctx.Data["Commits"] = commits @@ -191,7 +212,7 @@ func Diff(ctx *middleware.Context) { commitId := ctx.Repo.CommitId commit := ctx.Repo.Commit - + commit.CommitMessage = string(base.RenderMarkdown([]byte(commit.CommitMessage), ctx.Repo.RepoLink)) diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName), commitId, setting.MaxGitDiffLines) if err != nil { diff --git a/routers/repo/view.go b/routers/repo/view.go index 162279acd..a60c6748c 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -199,6 +199,7 @@ func Home(ctx *middleware.Context) { } lastCommit := ctx.Repo.Commit + lastCommit.CommitMessage = string(base.RenderMarkdown([]byte(lastCommit.CommitMessage), ctx.Repo.RepoLink)) if len(treePath) > 0 { c, err := ctx.Repo.Commit.GetCommitOfRelPath(treePath) if err != nil { diff --git a/templates/repo/commits_table.tmpl b/templates/repo/commits_table.tmpl index eb819e387..fe4288734 100644 --- a/templates/repo/commits_table.tmpl +++ b/templates/repo/commits_table.tmpl @@ -32,7 +32,7 @@ {{end}} {{SubStr .Id.String 0 10}} - {{.Summary}} + {{str2html .Summary}} {{TimeSince .Author.When $.Lang}} {{end}} diff --git a/templates/repo/diff.tmpl b/templates/repo/diff.tmpl index 8e6b1b079..ed7a081c5 100644 --- a/templates/repo/diff.tmpl +++ b/templates/repo/diff.tmpl @@ -17,7 +17,7 @@
{{.i18n.Tr "repo.diff.browse_source"}} -

{{.Commit.Message}}

+

{{Str2html .Commit.Message}}

@@ -37,7 +37,7 @@ {{.Commit.Author.Name}} {{end}} - {{TimeSince .Commit.Author.When $.Lang}} + {{TimeSince .Commit.Author.When $.Lang}}

@@ -110,7 +110,7 @@ {{if .RightIdx}}{{.RightIdx}}{{end}} - +
{{ToUtf8 .Content}}
@@ -122,7 +122,7 @@ {{end}} -
+
{{end}} {{end}} diff --git a/templates/repo/view_list.tmpl b/templates/repo/view_list.tmpl index d516eac94..7e6fc965c 100644 --- a/templates/repo/view_list.tmpl +++ b/templates/repo/view_list.tmpl @@ -14,7 +14,7 @@ {{ShortSha .LastCommit.Id.String}} - {{.LastCommit.Summary}} + {{str2html .LastCommit.Summary}} {{TimeSince .LastCommit.Author.When $.Lang}} @@ -25,6 +25,7 @@ .. + SHA1 @@ -48,9 +49,11 @@ {{$entry.Name}} {{end}} - - {{$commit.Summary}} - + {{SubStr $commit.Id.String 0 10}} + {{str2html $commit.Summary}} + + + {{TimeSince $commit.Committer.When $.Lang}} {{end}} @@ -58,4 +61,4 @@ {{if .ReadmeExist}} {{template "repo/view_file" .}} -{{end}} \ No newline at end of file +{{end}}