Enable sintax highlighting on diff view. Close #733
This commit is contained in:
		
							parent
							
								
									137a49e834
								
							
						
					
					
						commit
						2bfb8bb5fd
					
				|  | @ -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 | ||||
|  |  | |||
|  | @ -2,7 +2,7 @@ | |||
| // Use of this source code is governed by a MIT-style
 | ||||
| // license that can be found in the LICENSE file.
 | ||||
| 
 | ||||
| package template | ||||
| package highlight | ||||
| 
 | ||||
| import ( | ||||
| 	"path" | ||||
|  | @ -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; | ||||
|  |  | |||
|  | @ -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; | ||||
| } | ||||
|  |  | |||
|  | @ -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() | ||||
|  |  | |||
|  | @ -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) | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
|  | @ -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) | ||||
|  |  | |||
|  | @ -76,13 +76,13 @@ | |||
| 														<span rel="{{if $line.LeftIdx}}diff-{{Sha1 $file.Name}}L{{$line.LeftIdx}}{{end}}">{{if $line.LeftIdx}}{{$line.LeftIdx}}{{end}}</span> | ||||
| 													</td> | ||||
| 													<td class="lines-code halfwidth"> | ||||
| 														<pre class="wrap">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</pre> | ||||
| 														<pre><code class="wrap {{if $file.GetHighlightClass}}language-{{$file.GetHighlightClass}}{{else}}nohighlight{{end}}">{{if $line.LeftIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></pre> | ||||
| 													</td> | ||||
| 													<td class="lines-num lines-num-new"> | ||||
| 														<span rel="{{if $line.RightIdx}}diff-{{Sha1 $file.Name}}R{{$line.RightIdx}}{{end}}">{{if $line.RightIdx}}{{$line.RightIdx}}{{end}}</span> | ||||
| 													</td> | ||||
| 													<td class="lines-code halfwidth"> | ||||
| 														<pre class="wrap">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</pre> | ||||
| 														<pre><code class="wrap {{if $file.GetHighlightClass}}language-{{$file.GetHighlightClass}}{{else}}nohighlight{{end}}">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></pre> | ||||
| 													</td> | ||||
| 												</tr> | ||||
| 											{{end}} | ||||
|  | @ -104,7 +104,7 @@ | |||
| 													</td> | ||||
| 													{{end}} | ||||
| 													<td class="lines-code"> | ||||
| 														<pre>{{$section.GetComputedInlineDiffFor $line}}</pre> | ||||
| 														<pre><code class="{{if $file.GetHighlightClass}}language-{{$file.GetHighlightClass}}{{else}}nohighlight{{end}}">{{$section.GetComputedInlineDiffFor $line}}</code></pre> | ||||
| 													</td> | ||||
| 												</tr> | ||||
| 											{{end}} | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue