Forráskód Böngészése

[GinR] Show odML Files with jsTree

cgars 7 éve
szülő
commit
f9cb444a57

+ 1 - 1
models/repo_editor.go

@@ -585,7 +585,7 @@ func StartIndexing(user, owner *User, repo *Repository) {
 	client := http.Client{}
 	resp, err := client.Do(req)
 	if err != nil || resp.StatusCode != http.StatusOK {
-		log.Trace("Error doing index request:%+v,%d", err, resp.StatusCode)
+		log.Trace("Error doing index request:%+v,%d", err)
 		return
 	}
 }

+ 5 - 0
pkg/template/template.go

@@ -63,6 +63,7 @@ func NewFuncMap() []template.FuncMap {
 		"Safe":         Safe,
 		"Sanitize":     bluemonday.UGCPolicy().Sanitize,
 		"Str2html":     Str2html,
+		"Str2JS":       Str2JS,
 		"TimeSince":    tool.TimeSince,
 		"RawTimeSince": tool.RawTimeSince,
 		"FileSize":     tool.FileSize,
@@ -128,6 +129,10 @@ func Str2html(raw string) template.HTML {
 	return template.HTML(markup.Sanitize(raw))
 }
 
+func Str2JS(raw string) template.JS {
+	return template.JS(raw)
+}
+
 func List(l *list.List) chan interface{} {
 	e := l.Front()
 	c := make(chan interface{})

+ 7 - 0
pkg/tool/file.go

@@ -11,6 +11,13 @@ import (
 	"strings"
 )
 
+func IsOdmlFile(data []byte) bool {
+	if len(data) == 0 {
+		return true
+	}
+	return strings.Contains(string(data), "<odML version=")
+}
+
 // IsTextFile returns true if file content format is plain text or empty.
 func IsTextFile(data []byte) bool {
 	if len(data) == 0 {

+ 20 - 0
routes/repo/view.go

@@ -32,6 +32,10 @@ import (
 	"github.com/G-Node/gogs/pkg/tool"
 	"github.com/go-macaron/captcha"
 	"gopkg.in/yaml.v2"
+	"github.com/G-Node/godML/odml"
+	"encoding/json"
+	"encoding/xml"
+	"golang.org/x/net/html/charset"
 )
 
 const (
@@ -270,6 +274,22 @@ func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink stri
 			c.Data["FileContent"] = string(markup.OrgMode(buf, path.Dir(treeLink), c.Repo.Repository.ComposeMetas()))
 		case markup.IPYTHON_NOTEBOOK:
 			c.Data["IsIPythonNotebook"] = true
+		case markup.UNRECOGNIZED:
+			if tool.IsOdmlFile(buf) {
+				c.Data["IsOdML"] = true
+				od := odml.Odml{}
+				xml.Unmarshal(buf, &od)
+				decoder := xml.NewDecoder(bytes.NewReader(buf))
+				decoder.CharsetReader = charset.NewReaderLabel
+				decoder.Decode(&od)
+				data, _ := json.Marshal(od)
+				c.Data["FileContent"] = string(data)
+				break
+			} else {
+				goto End
+			}
+		End:
+			fallthrough
 		default:
 			// Building code view blocks with line number on server side.
 			var fileContent string

+ 5 - 0
templates/base/head.tmpl

@@ -61,6 +61,11 @@
 		<script src="{{AppSubURL}}/plugins/marked-0.3.6/marked.min.js"></script>
 		{{end}}
 
+		{{if .IsOdML}}
+		<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
+		<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css"/>
+		{{end}}
+
 		{{if .RequireSimpleMDE}}
 		<link rel="stylesheet" href="{{AppSubURL}}/plugins/simplemde-1.10.1/simplemde.min.css">
 		<script src="{{AppSubURL}}/plugins/simplemde-1.10.1/simplemde.min.js"></script>

+ 26 - 0
templates/repo/view_file.tmpl

@@ -39,6 +39,32 @@
 		<div id="{{if .IsIPythonNotebook}}ipython-notebook{{end}}" class="file-view {{if .IsMarkdown}}markdown{{else if .IsIPythonNotebook}}ipython-notebook{{else if .ReadmeInList}}plain-text{{else if and .IsTextFile}}code-view{{end}} has-emoji">
 			{{if .IsMarkdown}}
 				{{if .FileContent}}{{.FileContent | Str2html}}{{end}}
+			{{else if .IsOdML}}
+			<div class="ui fluid action input">
+				<input class="search-input form-control" placeholder="Search"></input>
+			</div>
+			<div id="jstree"></div>
+			<script>
+				$(function() {
+					$(".search-input").keyup(function(e) {
+						if(e.keyCode==13) {
+							var searchString = $(this).val();
+							console.log(searchString);
+							$('#jstree').jstree('search', searchString);
+						}
+					});
+					$('#jstree').jstree({
+						'core': {
+							'data': [{{.FileContent| Str2JS}}]
+				},
+					"search": {
+						"case_insensitive": true,
+							"show_only_matches" : true
+					},
+					"plugins": ["search"]
+				});
+				});
+			</script>
 			{{else if .IsIPythonNotebook}}
 				<script>
 					var rendered = null;

+ 5 - 0
vendor/vendor.json

@@ -32,6 +32,11 @@
 			"revision": "f44cb4a8236be506a35a47ea3da591c0b2bb58de",
 			"revisionTime": "2017-07-13T18:18:00Z"
 		},
+		{
+			"checksumSHA1": "dpa+gHrdw4GXFknbMysx2/32kUM=",
+			"path": "github.com/G-Node/odML/odml",
+			"revision": ""
+		},
 		{
 			"checksumSHA1": "IyfS7Rbl6OgR83QR7TOfKdDCq+M=",
 			"path": "github.com/Unknwon/cae",