Browse Source

Merge pull request #2528 from andreynering/diff-sintax-highlight-733

Enable syntax highlighting on diff view
Unknwon 4 years ago
parent
commit
f15a2f9b25

+ 9 - 0
models/git_diff.go

@@ -26,6 +26,7 @@ import (
26 26
 	"github.com/gogits/gogs/modules/base"
27 27
 	"github.com/gogits/gogs/modules/log"
28 28
 	"github.com/gogits/gogs/modules/process"
29
+	"github.com/gogits/gogs/modules/template/highlight"
29 30
 )
30 31
 
31 32
 type DiffLineType uint8
@@ -160,12 +161,20 @@ type DiffFile struct {
160 161
 	IsBin              bool
161 162
 	IsRenamed          bool
162 163
 	Sections           []*DiffSection
164
+	HighlightClass     string
163 165
 }
164 166
 
165 167
 func (diffFile *DiffFile) GetType() int {
166 168
 	return int(diffFile.Type)
167 169
 }
168 170
 
171
+func (diffFile *DiffFile) GetHighlightClass() string {
172
+	if diffFile.HighlightClass == "" {
173
+		diffFile.HighlightClass = highlight.FileNameToHighlightClass(diffFile.Name)
174
+	}
175
+	return diffFile.HighlightClass
176
+}
177
+
169 178
 type Diff struct {
170 179
 	TotalAddition, TotalDeletion int
171 180
 	Files                        []*DiffFile

+ 50 - 50
models/git_diff_test.go

@@ -1,70 +1,70 @@
1 1
 package models
2 2
 
3 3
 import (
4
-  dmp "github.com/sergi/go-diff/diffmatchpatch"
5
-  "html/template"
6
-  "testing"
4
+	dmp "github.com/sergi/go-diff/diffmatchpatch"
5
+	"html/template"
6
+	"testing"
7 7
 )
8 8
 
9 9
 func assertEqual(t *testing.T, s1 string, s2 template.HTML) {
10
-  if s1 != string(s2) {
11
-    t.Errorf("%s should be equal %s", s2, s1)
12
-  }
10
+	if s1 != string(s2) {
11
+		t.Errorf("%s should be equal %s", s2, s1)
12
+	}
13 13
 }
14 14
 
15 15
 func assertLineEqual(t *testing.T, d1 *DiffLine, d2 *DiffLine) {
16
-  if d1 != d2 {
17
-    t.Errorf("%v should be equal %v", d1, d2)
18
-  }
16
+	if d1 != d2 {
17
+		t.Errorf("%v should be equal %v", d1, d2)
18
+	}
19 19
 }
20 20
 
21 21
 func TestDiffToHTML(t *testing.T) {
22
-  assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
23
-    dmp.Diff{dmp.DiffEqual, "foo "},
24
-    dmp.Diff{dmp.DiffInsert, "bar"},
25
-    dmp.Diff{dmp.DiffDelete, " baz"},
26
-    dmp.Diff{dmp.DiffEqual, " biz"},
27
-  }, DIFF_LINE_ADD))
22
+	assertEqual(t, "foo <span class=\"added-code\">bar</span> biz", diffToHTML([]dmp.Diff{
23
+		dmp.Diff{dmp.DiffEqual, "foo "},
24
+		dmp.Diff{dmp.DiffInsert, "bar"},
25
+		dmp.Diff{dmp.DiffDelete, " baz"},
26
+		dmp.Diff{dmp.DiffEqual, " biz"},
27
+	}, DIFF_LINE_ADD))
28 28
 
29
-  assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
30
-    dmp.Diff{dmp.DiffEqual, "foo "},
31
-    dmp.Diff{dmp.DiffDelete, "bar"},
32
-    dmp.Diff{dmp.DiffInsert, " baz"},
33
-    dmp.Diff{dmp.DiffEqual, " biz"},
34
-  }, DIFF_LINE_DEL))
29
+	assertEqual(t, "foo <span class=\"removed-code\">bar</span> biz", diffToHTML([]dmp.Diff{
30
+		dmp.Diff{dmp.DiffEqual, "foo "},
31
+		dmp.Diff{dmp.DiffDelete, "bar"},
32
+		dmp.Diff{dmp.DiffInsert, " baz"},
33
+		dmp.Diff{dmp.DiffEqual, " biz"},
34
+	}, DIFF_LINE_DEL))
35 35
 }
36 36
 
37 37
 // test if GetLine is return the correct lines
38 38
 func TestGetLine(t *testing.T) {
39
-  ds := DiffSection{Lines: []*DiffLine{
40
-    &DiffLine{LeftIdx: 28,  RightIdx:   28, Type: DIFF_LINE_PLAIN},
41
-    &DiffLine{LeftIdx: 29,  RightIdx:   29, Type: DIFF_LINE_PLAIN},
42
-    &DiffLine{LeftIdx: 30,  RightIdx:   30, Type: DIFF_LINE_PLAIN},
43
-    &DiffLine{LeftIdx: 31,  RightIdx:    0, Type: DIFF_LINE_DEL},
44
-    &DiffLine{LeftIdx:  0,  RightIdx:   31, Type: DIFF_LINE_ADD},
45
-    &DiffLine{LeftIdx:  0,  RightIdx:   32, Type: DIFF_LINE_ADD},
46
-    &DiffLine{LeftIdx: 32,  RightIdx:   33, Type: DIFF_LINE_PLAIN},
47
-    &DiffLine{LeftIdx: 33,  RightIdx:    0, Type: DIFF_LINE_DEL},
48
-    &DiffLine{LeftIdx: 34,  RightIdx:    0, Type: DIFF_LINE_DEL},
49
-    &DiffLine{LeftIdx: 35,  RightIdx:    0, Type: DIFF_LINE_DEL},
50
-    &DiffLine{LeftIdx: 36,  RightIdx:    0, Type: DIFF_LINE_DEL},
51
-    &DiffLine{LeftIdx:  0,  RightIdx:   34, Type: DIFF_LINE_ADD},
52
-    &DiffLine{LeftIdx:  0,  RightIdx:   35, Type: DIFF_LINE_ADD},
53
-    &DiffLine{LeftIdx:  0,  RightIdx:   36, Type: DIFF_LINE_ADD},
54
-    &DiffLine{LeftIdx:  0,  RightIdx:   37, Type: DIFF_LINE_ADD},
55
-    &DiffLine{LeftIdx: 37,  RightIdx:   38, Type: DIFF_LINE_PLAIN},
56
-    &DiffLine{LeftIdx: 38,  RightIdx:   39, Type: DIFF_LINE_PLAIN},
57
-  }}
39
+	ds := DiffSection{Lines: []*DiffLine{
40
+		&DiffLine{LeftIdx: 28, RightIdx: 28, Type: DIFF_LINE_PLAIN},
41
+		&DiffLine{LeftIdx: 29, RightIdx: 29, Type: DIFF_LINE_PLAIN},
42
+		&DiffLine{LeftIdx: 30, RightIdx: 30, Type: DIFF_LINE_PLAIN},
43
+		&DiffLine{LeftIdx: 31, RightIdx: 0, Type: DIFF_LINE_DEL},
44
+		&DiffLine{LeftIdx: 0, RightIdx: 31, Type: DIFF_LINE_ADD},
45
+		&DiffLine{LeftIdx: 0, RightIdx: 32, Type: DIFF_LINE_ADD},
46
+		&DiffLine{LeftIdx: 32, RightIdx: 33, Type: DIFF_LINE_PLAIN},
47
+		&DiffLine{LeftIdx: 33, RightIdx: 0, Type: DIFF_LINE_DEL},
48
+		&DiffLine{LeftIdx: 34, RightIdx: 0, Type: DIFF_LINE_DEL},
49
+		&DiffLine{LeftIdx: 35, RightIdx: 0, Type: DIFF_LINE_DEL},
50
+		&DiffLine{LeftIdx: 36, RightIdx: 0, Type: DIFF_LINE_DEL},
51
+		&DiffLine{LeftIdx: 0, RightIdx: 34, Type: DIFF_LINE_ADD},
52
+		&DiffLine{LeftIdx: 0, RightIdx: 35, Type: DIFF_LINE_ADD},
53
+		&DiffLine{LeftIdx: 0, RightIdx: 36, Type: DIFF_LINE_ADD},
54
+		&DiffLine{LeftIdx: 0, RightIdx: 37, Type: DIFF_LINE_ADD},
55
+		&DiffLine{LeftIdx: 37, RightIdx: 38, Type: DIFF_LINE_PLAIN},
56
+		&DiffLine{LeftIdx: 38, RightIdx: 39, Type: DIFF_LINE_PLAIN},
57
+	}}
58 58
 
59
-  assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 31), ds.Lines[4])
60
-  assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 31), ds.Lines[3])
59
+	assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 31), ds.Lines[4])
60
+	assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 31), ds.Lines[3])
61 61
 
62
-  assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 33), ds.Lines[11])
63
-  assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 34), ds.Lines[12])
64
-  assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 35), ds.Lines[13])
65
-  assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 36), ds.Lines[14])
66
-  assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 34), ds.Lines[7])
67
-  assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 35), ds.Lines[8])
68
-  assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 36), ds.Lines[9])
69
-  assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 37), ds.Lines[10])
62
+	assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 33), ds.Lines[11])
63
+	assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 34), ds.Lines[12])
64
+	assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 35), ds.Lines[13])
65
+	assertLineEqual(t, ds.GetLine(DIFF_LINE_ADD, 36), ds.Lines[14])
66
+	assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 34), ds.Lines[7])
67
+	assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 35), ds.Lines[8])
68
+	assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 36), ds.Lines[9])
69
+	assertLineEqual(t, ds.GetLine(DIFF_LINE_DEL, 37), ds.Lines[10])
70 70
 }

+ 1 - 1
modules/template/highlight.go

@@ -2,7 +2,7 @@
2 2
 // Use of this source code is governed by a MIT-style
3 3
 // license that can be found in the LICENSE file.
4 4
 
5
-package template
5
+package highlight
6 6
 
7 7
 import (
8 8
 	"path"

+ 11 - 3
public/css/gogs.css

@@ -12,10 +12,12 @@ body {
12 12
 img {
13 13
   border-radius: 3px;
14 14
 }
15
-pre {
15
+pre,
16
+code {
16 17
   font: 12px Consolas, "Liberation Mono", Menlo, Courier, monospace;
17 18
 }
18
-pre.raw {
19
+pre.raw,
20
+code.raw {
19 21
   padding: 7px 12px;
20 22
   margin: 10px 0;
21 23
   background-color: #f8f8f8;
@@ -25,7 +27,8 @@ pre.raw {
25 27
   line-height: 1.5;
26 28
   overflow: auto;
27 29
 }
28
-pre.wrap {
30
+pre.wrap,
31
+code.wrap {
29 32
   white-space: pre-wrap;
30 33
   /* CSS 3 */
31 34
   word-break: break-word;
@@ -387,6 +390,11 @@ footer .container .links > *:first-child {
387 390
     width: 95%;
388 391
   }
389 392
 }
393
+/* Overrides some styles of the Highlight.js plugin */
394
+.hljs {
395
+  background: inherit !important;
396
+  padding: 0 !important;
397
+}
390 398
 .markdown {
391 399
   overflow: hidden;
392 400
   font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif;

+ 7 - 1
public/less/_base.less

@@ -8,7 +8,7 @@ body {
8 8
 img {
9 9
 	border-radius: 3px;
10 10
 }
11
-pre {
11
+pre, code {
12 12
 	font: 12px Consolas, "Liberation Mono", Menlo, Courier, monospace;
13 13
 	&.raw {
14 14
 		padding: 7px 12px;
@@ -375,3 +375,9 @@ footer {
375 375
 		width: 95%;
376 376
 	}
377 377
 }
378
+
379
+/* Overrides some styles of the Highlight.js plugin */
380
+.hljs {
381
+	background: inherit !important;
382
+	padding: 0 !important;
383
+}

+ 2 - 2
routers/install.go

@@ -28,7 +28,7 @@ import (
28 28
 	"github.com/gogits/gogs/modules/middleware"
29 29
 	"github.com/gogits/gogs/modules/setting"
30 30
 	"github.com/gogits/gogs/modules/ssh"
31
-	"github.com/gogits/gogs/modules/template"
31
+	"github.com/gogits/gogs/modules/template/highlight"
32 32
 	"github.com/gogits/gogs/modules/user"
33 33
 )
34 34
 
@@ -56,7 +56,7 @@ func NewServices() {
56 56
 // GlobalInit is for global configuration reload-able.
57 57
 func GlobalInit() {
58 58
 	setting.NewContext()
59
-	template.NewContext()
59
+	highlight.NewContext()
60 60
 	log.Trace("Custom path: %s", setting.CustomPath)
61 61
 	log.Trace("Log path: %s", setting.LogRootPath)
62 62
 	models.LoadConfigs()

+ 1 - 0
routers/repo/commit.go

@@ -183,6 +183,7 @@ func Diff(ctx *middleware.Context) {
183 183
 		ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "src", parents[0])
184 184
 	}
185 185
 	ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(userName, repoName, "raw", commitID)
186
+	ctx.Data["RequireHighlightJS"] = true
186 187
 	ctx.HTML(200, DIFF)
187 188
 }
188 189
 

+ 2 - 0
routers/repo/pull.go

@@ -362,6 +362,7 @@ func ViewPullFiles(ctx *middleware.Context) {
362 362
 	ctx.Data["SourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", endCommitID)
363 363
 	ctx.Data["BeforeSourcePath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "src", startCommitID)
364 364
 	ctx.Data["RawPath"] = setting.AppSubUrl + "/" + path.Join(headTarget, "raw", endCommitID)
365
+	ctx.Data["RequireHighlightJS"] = true
365 366
 
366 367
 	ctx.HTML(200, PULL_FILES)
367 368
 }
@@ -538,6 +539,7 @@ func CompareAndPullRequest(ctx *middleware.Context) {
538 539
 	ctx.Data["Title"] = ctx.Tr("repo.pulls.compare_changes")
539 540
 	ctx.Data["PageIsComparePull"] = true
540 541
 	ctx.Data["IsDiffCompare"] = true
542
+	ctx.Data["RequireHighlightJS"] = true
541 543
 	renderAttachmentSettings(ctx)
542 544
 
543 545
 	headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(ctx)

+ 2 - 1
routers/repo/view.go

@@ -19,6 +19,7 @@ import (
19 19
 	"github.com/gogits/gogs/modules/log"
20 20
 	"github.com/gogits/gogs/modules/middleware"
21 21
 	"github.com/gogits/gogs/modules/template"
22
+	"github.com/gogits/gogs/modules/template/highlight"
22 23
 )
23 24
 
24 25
 const (
@@ -79,7 +80,7 @@ func Home(ctx *middleware.Context) {
79 80
 			ctx.Data["FileSize"] = blob.Size()
80 81
 			ctx.Data["IsFile"] = true
81 82
 			ctx.Data["FileName"] = blob.Name()
82
-			ctx.Data["HighlightClass"] = template.FileNameToHighlightClass(blob.Name())
83
+			ctx.Data["HighlightClass"] = highlight.FileNameToHighlightClass(blob.Name())
83 84
 			ctx.Data["FileLink"] = rawLink + "/" + treename
84 85
 
85 86
 			buf := make([]byte, 1024)

+ 3 - 3
templates/repo/diff_box.tmpl

@@ -76,13 +76,13 @@
76 76
 														<span rel="{{if $line.LeftIdx}}diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}{{end}}">{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}</span>
77 77
 													</td>
78 78
 													<td class="lines-code halfwidth">
79
-														<pre class="wrap">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</pre>
79
+														<pre><code class="wrap {{if $file.GetHighlightClass}}language-{{$file.GetHighlightClass}}{{else}}nohighlight{{end}}">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></pre>
80 80
 													</td>
81 81
 													<td class="lines-num lines-num-new">
82 82
 														<span rel="{{if $line.RightIdx}}diff-{{Sha1 $file.Name}}R{{$line.RightIdx}}{{end}}">{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}</span>
83 83
 													</td>
84 84
 													<td class="lines-code halfwidth">
85
-														<pre class="wrap">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</pre>
85
+														<pre><code class="wrap {{if $file.GetHighlightClass}}language-{{$file.GetHighlightClass}}{{else}}nohighlight{{end}}">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></pre>
86 86
 													</td>
87 87
 												</tr>
88 88
 											{{end}}
@@ -104,7 +104,7 @@
104 104
 													</td>
105 105
 													{{end}}
106 106
 													<td class="lines-code">
107
-														<pre>{{$section.GetComputedInlineDiffFor $line}}</pre>
107
+														<pre><code class="{{if $file.GetHighlightClass}}language-{{$file.GetHighlightClass}}{{else}}nohighlight{{end}}">{{$section.GetComputedInlineDiffFor $line}}</code></pre>
108 108
 													</td>
109 109
 												</tr>
110 110
 											{{end}}