From 137a49e8342ab207c8570fc18b7c81b5fa9789ab Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 31 Jan 2016 14:16:29 -0200 Subject: go fmt models/git_diff_test.go --- models/git_diff_test.go | 100 ++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/models/git_diff_test.go b/models/git_diff_test.go index 4084814e..3a1312ca 100644 --- a/models/git_diff_test.go +++ b/models/git_diff_test.go @@ -1,70 +1,70 @@ package models import ( - dmp "github.com/sergi/go-diff/diffmatchpatch" - "html/template" - "testing" + dmp "github.com/sergi/go-diff/diffmatchpatch" + "html/template" + "testing" ) func assertEqual(t *testing.T, s1 string, s2 template.HTML) { - if s1 != string(s2) { - t.Errorf("%s should be equal %s", s2, s1) - } + if s1 != string(s2) { + t.Errorf("%s should be equal %s", s2, s1) + } } func assertLineEqual(t *testing.T, d1 *DiffLine, d2 *DiffLine) { - if d1 != d2 { - t.Errorf("%v should be equal %v", d1, d2) - } + if d1 != d2 { + t.Errorf("%v should be equal %v", d1, d2) + } } func TestDiffToHTML(t *testing.T) { - assertEqual(t, "foo bar biz", diffToHTML([]dmp.Diff{ - dmp.Diff{dmp.DiffEqual, "foo "}, - dmp.Diff{dmp.DiffInsert, "bar"}, - dmp.Diff{dmp.DiffDelete, " baz"}, - dmp.Diff{dmp.DiffEqual, " biz"}, - }, DIFF_LINE_ADD)) + assertEqual(t, "foo bar biz", diffToHTML([]dmp.Diff{ + dmp.Diff{dmp.DiffEqual, "foo "}, + dmp.Diff{dmp.DiffInsert, "bar"}, + dmp.Diff{dmp.DiffDelete, " baz"}, + dmp.Diff{dmp.DiffEqual, " biz"}, + }, DIFF_LINE_ADD)) - assertEqual(t, "foo bar biz", diffToHTML([]dmp.Diff{ - dmp.Diff{dmp.DiffEqual, "foo "}, - dmp.Diff{dmp.DiffDelete, "bar"}, - dmp.Diff{dmp.DiffInsert, " baz"}, - dmp.Diff{dmp.DiffEqual, " biz"}, - }, DIFF_LINE_DEL)) + assertEqual(t, "foo bar biz", diffToHTML([]dmp.Diff{ + dmp.Diff{dmp.DiffEqual, "foo "}, + dmp.Diff{dmp.DiffDelete, "bar"}, + dmp.Diff{dmp.DiffInsert, " baz"}, + dmp.Diff{dmp.DiffEqual, " biz"}, + }, DIFF_LINE_DEL)) } // test if GetLine is return the correct lines func TestGetLine(t *testing.T) { - ds := DiffSection{Lines: []*DiffLine{ - &DiffLine{LeftIdx: 28, RightIdx: 28, Type: DIFF_LINE_PLAIN}, - &DiffLine{LeftIdx: 29, RightIdx: 29, Type: DIFF_LINE_PLAIN}, - &DiffLine{LeftIdx: 30, RightIdx: 30, Type: DIFF_LINE_PLAIN}, - &DiffLine{LeftIdx: 31, RightIdx: 0, Type: DIFF_LINE_DEL}, - &DiffLine{LeftIdx: 0, RightIdx: 31, Type: DIFF_LINE_ADD}, - &DiffLine{LeftIdx: 0, RightIdx: 32, Type: DIFF_LINE_ADD}, - &DiffLine{LeftIdx: 32, RightIdx: 33, Type: DIFF_LINE_PLAIN}, - &DiffLine{LeftIdx: 33, RightIdx: 0, Type: DIFF_LINE_DEL}, - &DiffLine{LeftIdx: 34, RightIdx: 0, Type: DIFF_LINE_DEL}, - &DiffLine{LeftIdx: 35, RightIdx: 0, Type: DIFF_LINE_DEL}, - &DiffLine{LeftIdx: 36, RightIdx: 0, Type: DIFF_LINE_DEL}, - &DiffLine{LeftIdx: 0, RightIdx: 34, Type: DIFF_LINE_ADD}, - &DiffLine{LeftIdx: 0, RightIdx: 35, Type: DIFF_LINE_ADD}, - &DiffLine{LeftIdx: 0, RightIdx: 36, Type: DIFF_LINE_ADD}, - &DiffLine{LeftIdx: 0, RightIdx: 37, Type: DIFF_LINE_ADD}, - &DiffLine{LeftIdx: 37, RightIdx: 38, Type: DIFF_LINE_PLAIN}, - &DiffLine{LeftIdx: 38, RightIdx: 39, Type: DIFF_LINE_PLAIN}, - }} + ds := DiffSection{Lines: []*DiffLine{ + &DiffLine{LeftIdx: 28, RightIdx: 28, Type: DIFF_LINE_PLAIN}, + &DiffLine{LeftIdx: 29, RightIdx: 29, Type: DIFF_LINE_PLAIN}, + &DiffLine{LeftIdx: 30, RightIdx: 30, Type: DIFF_LINE_PLAIN}, + &DiffLine{LeftIdx: 31, RightIdx: 0, Type: DIFF_LINE_DEL}, + &DiffLine{LeftIdx: 0, RightIdx: 31, Type: DIFF_LINE_ADD}, + &DiffLine{LeftIdx: 0, RightIdx: 32, Type: DIFF_LINE_ADD}, + &DiffLine{LeftIdx: 32, RightIdx: 33, Type: DIFF_LINE_PLAIN}, + &DiffLine{LeftIdx: 33, RightIdx: 0, Type: DIFF_LINE_DEL}, + &DiffLine{LeftIdx: 34, RightIdx: 0, Type: DIFF_LINE_DEL}, + &DiffLine{LeftIdx: 35, RightIdx: 0, Type: DIFF_LINE_DEL}, + &DiffLine{LeftIdx: 36, RightIdx: 0, Type: DIFF_LINE_DEL}, + &DiffLine{LeftIdx: 0, RightIdx: 34, Type: DIFF_LINE_ADD}, + &DiffLine{LeftIdx: 0, RightIdx: 35, Type: DIFF_LINE_ADD}, + &DiffLine{LeftIdx: 0, RightIdx: 36, Type: DIFF_LINE_ADD}, + &DiffLine{LeftIdx: 0, RightIdx: 37, Type: DIFF_LINE_ADD}, + &DiffLine{LeftIdx: 37, RightIdx: 38, Type: DIFF_LINE_PLAIN}, + &DiffLine{LeftIdx: 38, RightIdx: 39, Type: DIFF_LINE_PLAIN}, + }} - assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 31), ds.Lines[4]) - assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 31), ds.Lines[3]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 31), ds.Lines[4]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 31), ds.Lines[3]) - assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 33), ds.Lines[11]) - assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 34), ds.Lines[12]) - assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 35), ds.Lines[13]) - assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 36), ds.Lines[14]) - assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 34), ds.Lines[7]) - assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 35), ds.Lines[8]) - assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 36), ds.Lines[9]) - assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 37), ds.Lines[10]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 33), ds.Lines[11]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 34), ds.Lines[12]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 35), ds.Lines[13]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 36), ds.Lines[14]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 34), ds.Lines[7]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 35), ds.Lines[8]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 36), ds.Lines[9]) + assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 37), ds.Lines[10]) } -- cgit v1.2.3 From 2bfb8bb5fdebb4ae02c83a271e8eb24bc68afec1 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sun, 31 Jan 2016 14:19:02 -0200 Subject: Enable sintax highlighting on diff view. Close #733 --- models/git_diff.go | 9 +++ modules/template/highlight.go | 97 --------------------------------- modules/template/highlight/highlight.go | 97 +++++++++++++++++++++++++++++++++ public/css/gogs.css | 14 ++++- public/less/_base.less | 8 ++- routers/install.go | 4 +- routers/repo/commit.go | 1 + routers/repo/pull.go | 2 + routers/repo/view.go | 3 +- templates/repo/diff_box.tmpl | 6 +- 10 files changed, 134 insertions(+), 107 deletions(-) delete mode 100644 modules/template/highlight.go create mode 100644 modules/template/highlight/highlight.go diff --git a/models/git_diff.go b/models/git_diff.go index e8bfe610..9796ef59 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -26,6 +26,7 @@ import ( "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/process" + "github.com/gogits/gogs/modules/template/highlight" ) type DiffLineType uint8 @@ -160,12 +161,20 @@ type DiffFile struct { IsBin bool IsRenamed bool Sections []*DiffSection + HighlightClass string } func (diffFile *DiffFile) GetType() int { return int(diffFile.Type) } +func (diffFile *DiffFile) GetHighlightClass() string { + if diffFile.HighlightClass == "" { + diffFile.HighlightClass = highlight.FileNameToHighlightClass(diffFile.Name) + } + return diffFile.HighlightClass +} + type Diff struct { TotalAddition, TotalDeletion int Files []*DiffFile diff --git a/modules/template/highlight.go b/modules/template/highlight.go deleted file mode 100644 index bd9813ee..00000000 --- a/modules/template/highlight.go +++ /dev/null @@ -1,97 +0,0 @@ -// Copyright 2015 The Gogs Authors. All rights reserved. -// Use of this source code is governed by a MIT-style -// license that can be found in the LICENSE file. - -package template - -import ( - "path" - "strings" - - "github.com/gogits/gogs/modules/setting" -) - -var ( - // File name should ignore highlight. - ignoreFileNames = map[string]bool{ - "license": true, - "copying": true, - } - - // File names that are representing highlight classes. - highlightFileNames = map[string]bool{ - "dockerfile": true, - "makefile": true, - } - - // Extensions that are same as highlight classes. - highlightExts = map[string]bool{ - ".arm": true, - ".as": true, - ".sh": true, - ".cs": true, - ".cpp": true, - ".c": true, - ".css": true, - ".cmake": true, - ".bat": true, - ".dart": true, - ".patch": true, - ".elixir": true, - ".erlang": true, - ".go": true, - ".html": true, - ".xml": true, - ".hs": true, - ".ini": true, - ".json": true, - ".java": true, - ".js": true, - ".less": true, - ".lua": true, - ".php": true, - ".py": true, - ".rb": true, - ".scss": true, - ".sql": true, - ".scala": true, - ".swift": true, - ".ts": true, - ".vb": true, - } - - // Extensions that are not same as highlight classes. - highlightMapping = map[string]string{} -) - -func NewContext() { - keys := setting.Cfg.Section("highlight.mapping").Keys() - for i := range keys { - highlightMapping[keys[i].Name()] = keys[i].Value() - } -} - -// FileNameToHighlightClass returns the best match for highlight class name -// based on the rule of highlight.js. -func FileNameToHighlightClass(fname string) string { - fname = strings.ToLower(fname) - if ignoreFileNames[fname] { - return "nohighlight" - } - - if highlightFileNames[fname] { - return fname - } - - ext := path.Ext(fname) - if highlightExts[ext] { - return ext[1:] - } - - name, ok := highlightMapping[ext] - if ok { - return name - } - - return "" -} diff --git a/modules/template/highlight/highlight.go b/modules/template/highlight/highlight.go new file mode 100644 index 00000000..bbf08e04 --- /dev/null +++ b/modules/template/highlight/highlight.go @@ -0,0 +1,97 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package highlight + +import ( + "path" + "strings" + + "github.com/gogits/gogs/modules/setting" +) + +var ( + // File name should ignore highlight. + ignoreFileNames = map[string]bool{ + "license": true, + "copying": true, + } + + // File names that are representing highlight classes. + highlightFileNames = map[string]bool{ + "dockerfile": true, + "makefile": true, + } + + // Extensions that are same as highlight classes. + highlightExts = map[string]bool{ + ".arm": true, + ".as": true, + ".sh": true, + ".cs": true, + ".cpp": true, + ".c": true, + ".css": true, + ".cmake": true, + ".bat": true, + ".dart": true, + ".patch": true, + ".elixir": true, + ".erlang": true, + ".go": true, + ".html": true, + ".xml": true, + ".hs": true, + ".ini": true, + ".json": true, + ".java": true, + ".js": true, + ".less": true, + ".lua": true, + ".php": true, + ".py": true, + ".rb": true, + ".scss": true, + ".sql": true, + ".scala": true, + ".swift": true, + ".ts": true, + ".vb": true, + } + + // Extensions that are not same as highlight classes. + highlightMapping = map[string]string{} +) + +func NewContext() { + keys := setting.Cfg.Section("highlight.mapping").Keys() + for i := range keys { + highlightMapping[keys[i].Name()] = keys[i].Value() + } +} + +// FileNameToHighlightClass returns the best match for highlight class name +// based on the rule of highlight.js. +func FileNameToHighlightClass(fname string) string { + fname = strings.ToLower(fname) + if ignoreFileNames[fname] { + return "nohighlight" + } + + if highlightFileNames[fname] { + return fname + } + + ext := path.Ext(fname) + if highlightExts[ext] { + return ext[1:] + } + + name, ok := highlightMapping[ext] + if ok { + return name + } + + return "" +} diff --git a/public/css/gogs.css b/public/css/gogs.css index b3498d4b..74a29e13 100755 --- a/public/css/gogs.css +++ b/public/css/gogs.css @@ -12,10 +12,12 @@ body { img { border-radius: 3px; } -pre { +pre, +code { font: 12px Consolas, "Liberation Mono", Menlo, Courier, monospace; } -pre.raw { +pre.raw, +code.raw { padding: 7px 12px; margin: 10px 0; background-color: #f8f8f8; @@ -25,7 +27,8 @@ pre.raw { line-height: 1.5; overflow: auto; } -pre.wrap { +pre.wrap, +code.wrap { white-space: pre-wrap; /* CSS 3 */ word-break: break-word; @@ -387,6 +390,11 @@ footer .container .links > *:first-child { width: 95%; } } +/* Overrides some styles of the Highlight.js plugin */ +.hljs { + background: inherit !important; + padding: 0 !important; +} .markdown { overflow: hidden; font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif; diff --git a/public/less/_base.less b/public/less/_base.less index 03229444..1911d3cb 100644 --- a/public/less/_base.less +++ b/public/less/_base.less @@ -8,7 +8,7 @@ body { img { border-radius: 3px; } -pre { +pre, code { font: 12px Consolas, "Liberation Mono", Menlo, Courier, monospace; &.raw { padding: 7px 12px; @@ -375,3 +375,9 @@ footer { width: 95%; } } + +/* Overrides some styles of the Highlight.js plugin */ +.hljs { + background: inherit !important; + padding: 0 !important; +} diff --git a/routers/install.go b/routers/install.go index 120aa468..02bc6fd6 100644 --- a/routers/install.go +++ b/routers/install.go @@ -28,7 +28,7 @@ import ( "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/ssh" - "github.com/gogits/gogs/modules/template" + "github.com/gogits/gogs/modules/template/highlight" "github.com/gogits/gogs/modules/user" ) @@ -56,7 +56,7 @@ func NewServices() { // GlobalInit is for global configuration reload-able. func GlobalInit() { setting.NewContext() - template.NewContext() + highlight.NewContext() log.Trace("Custom path: %s", setting.CustomPath) log.Trace("Log path: %s", setting.LogRootPath) models.LoadConfigs() diff --git a/routers/repo/commit.go b/routers/repo/commit.go index c3fc4d17..781c3da9 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -183,6 +183,7 @@ func Diff(ctx *middleware.Context) { ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", parents[0]) } ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", commitID) + ctx.Data["RequireHighlightJS"] = true ctx.HTML(200, DIFF) } diff --git a/routers/repo/pull.go b/routers/repo/pull.go index e3500716..3240461c 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -362,6 +362,7 @@ func ViewPullFiles(ctx *middleware.Context) { ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", endCommitID) ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", startCommitID) ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", endCommitID) + ctx.Data["RequireHighlightJS"] = true ctx.HTML(200, PULL_FILES) } @@ -538,6 +539,7 @@ func CompareAndPullRequest(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes") ctx.Data["PageIsComparePull"] = true ctx.Data["IsDiffCompare"] = true + ctx.Data["RequireHighlightJS"] = true renderAttachmentSettings(ctx) headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx) diff --git a/routers/repo/view.go b/routers/repo/view.go index 25e312b1..6a4ad646 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -19,6 +19,7 @@ import ( "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/template" + "github.com/gogits/gogs/modules/template/highlight" ) const ( @@ -79,7 +80,7 @@ func Home(ctx *middleware.Context) { ctx.Data["FileSize"] = blob.Size() ctx.Data["IsFile"] = true ctx.Data["FileName"] = blob.Name() - ctx.Data["HighlightClass"] = template.FileNameToHighlightClass(blob.Name()) + ctx.Data["HighlightClass"] = highlight.FileNameToHighlightClass(blob.Name()) ctx.Data["FileLink"] = rawLink + "/" + treename buf := make([]byte, 1024) diff --git a/templates/repo/diff_box.tmpl b/templates/repo/diff_box.tmpl index 4ea531c3..f96ebda5 100644 --- a/templates/repo/diff_box.tmpl +++ b/templates/repo/diff_box.tmpl @@ -76,13 +76,13 @@ {{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}} -
{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}
+
{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}
{{if $line.RightIdx}}{{$line.RightIdx}}{{end}} -
{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}
+
{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}
{{end}} @@ -104,7 +104,7 @@ {{end}} -
{{$section.GetComputedInlineDiffFor $line}}
+
{{$section.GetComputedInlineDiffFor $line}}
{{end}} -- cgit v1.2.3