Browse Source

Clean up odML, JSON, and YAML rendering code

- Cleaned up the sloppy odML render condition in the markup switch.
- odML marshaling moved to separate function in its own file in the
pkg/markup package.
- IsODMLFile() first checks if the file is text before reading the
header.
- All mentions of odML have been changed to ODML to normalise
capitalisation.  Although the proper capitalisation is odML, public
members would have to be named 'OdML' and functions with the 'Is' prefix
would be named 'IsodML', both of which look ugly.
Achilleas Koutsou 6 years ago
parent
commit
a1ca3c8e2e

+ 20 - 0
pkg/markup/odml.go

@@ -0,0 +1,20 @@
+package markup
+
+import (
+	"bytes"
+	"encoding/json"
+	"encoding/xml"
+
+	"github.com/G-Node/godML/odml"
+	"golang.org/x/net/html/charset"
+)
+
+// ODML takes a []byte and returns a JSON []byte representation for rendering with jsTree
+func MarshalODML(buf []byte) []byte {
+	od := odml.Odml{}
+	decoder := xml.NewDecoder(bytes.NewReader(buf))
+	decoder.CharsetReader = charset.NewReaderLabel
+	decoder.Decode(&od)
+	data, _ := json.Marshal(od)
+	return data
+}

+ 4 - 3
pkg/tool/file.go

@@ -11,9 +11,10 @@ import (
 	"strings"
 )
 
-func IsOdmlFile(data []byte) bool {
-	if len(data) == 0 {
-		return true
+// IsODMLFile returns true of the file has an odML header
+func IsODMLFile(data []byte) bool {
+	if !IsTextFile(data) {
+		return false
 	}
 	return strings.Contains(string(data), "<odML version=")
 }

+ 1 - 1
routes/repo/editor.go

@@ -91,7 +91,7 @@ func editFile(c *context.Context, isNewFile bool) {
 			return
 		}
 
-		c.Data["IsOdML"] = tool.IsOdmlFile(buf)
+		c.Data["IsODML"] = tool.IsODMLFile(buf)
 
 		d, _ := ioutil.ReadAll(dataRc)
 		buf = append(buf, d...)

+ 11 - 20
routes/repo/view.go

@@ -5,21 +5,15 @@
 package repo
 
 import (
-	"bufio"
 	"bytes"
-	"encoding/json"
-	"encoding/xml"
 	"fmt"
 	gotemplate "html/template"
-	"io"
 	"io/ioutil"
-	"os"
 	"path"
 	"strings"
 
 	"github.com/G-Node/git-module"
 	gannex "github.com/G-Node/go-annex"
-	"github.com/G-Node/godML/odml"
 	"github.com/G-Node/gogs/models"
 	"github.com/G-Node/gogs/pkg/context"
 	"github.com/G-Node/gogs/pkg/markup"
@@ -29,7 +23,6 @@ import (
 	"github.com/G-Node/gogs/pkg/tool"
 	"github.com/Unknwon/paginater"
 	"github.com/go-macaron/captcha"
-	"golang.org/x/net/html/charset"
 	log "gopkg.in/clog.v1"
 )
 
@@ -190,6 +183,7 @@ 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
+			// GIN mod: JSON, YAML, and odML render support with jsTree
 		case markup.JSON:
 			c.Data["IsJSON"] = true
 			c.Data["RawFileContent"] = string(buf)
@@ -198,20 +192,14 @@ func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink stri
 			c.Data["IsYAML"] = true
 			c.Data["RawFileContent"] = string(buf)
 			fallthrough
-		case markup.UNRECOGNIZED:
-			if tool.IsOdmlFile(buf) {
-				c.Data["IsOdML"] = true
-				od := odml.Odml{}
-				decoder := xml.NewDecoder(bytes.NewReader(buf))
-				decoder.CharsetReader = charset.NewReaderLabel
-				decoder.Decode(&od)
-				data, _ := json.Marshal(od)
-				c.Data["OdML"] = string(data)
-				goto End
-			} else {
-				goto End
+		case markup.XML:
+			// pass XML down to ODML checker
+			fallthrough
+		case markup.ODML:
+			if tool.IsODMLFile(buf) {
+				c.Data["IsODML"] = true
+				c.Data["ODML"] = string(markup.MarshalODML(buf))
 			}
-		End:
 			fallthrough
 		default:
 			// Building code view blocks with line number on server side.
@@ -224,15 +212,18 @@ func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink stri
 			} else {
 				fileContent = content
 			}
+
 			var output bytes.Buffer
 			lines := strings.Split(fileContent, "\n")
 			// Remove blank line at the end of file
 			if len(lines) > 0 && len(lines[len(lines)-1]) == 0 {
 				lines = lines[:len(lines)-1]
 			}
+			// > GIN
 			if len(lines) > setting.UI.MaxLineHighlight {
 				c.Data["HighlightClass"] = "nohighlight"
 			}
+			// < GIN
 			for index, line := range lines {
 				output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(strings.TrimRight(line, "\r"))) + "\n")
 			}

+ 1 - 1
templates/base/head.tmpl

@@ -61,7 +61,7 @@
 		<script src="{{AppSubURL}}/plugins/marked-0.3.6/marked.min.js"></script>
 	{{end}}
 
-	{{if .IsOdML}}
+	{{if .IsODML}}
 		<script type="text/javascript" src="{{AppSubURL}}/plugins/xonomy/xonomy.js"></script>
 		<link type="text/css" rel="stylesheet" href="{{AppSubURL}}/plugins/xonomy/xonomy.css"/>
 

+ 2 - 2
templates/repo/editor/edit.tmpl

@@ -35,7 +35,7 @@
 					{{if .IsYAML}}
 					<a class="item" data-tab="yamleditor" data-tooltip="Using the graphical YAML editor will change the file layout and remove comments!"><i class="octicon octicon-file"></i>Graphical Editor</a>
 					{{end}}
-					{{if .IsOdML}}
+					{{if .IsODML}}
 					<a class="item" data-tab="odmleditor"><i class="octicon octicon-file"></i>Graphical Editor</a>
 					{{end}}
 					{{if not .IsNewFile}}
@@ -106,7 +106,7 @@
 				</div>
 				{{end}}
 
-				{{if .IsOdML}}
+				{{if .IsODML}}
 				<div class="ui bottom attached tab segment" data-tab="odmleditor">
 					<div id="xonomy_edit"></div>
 				</div>

+ 7 - 7
templates/repo/view_file.tmpl

@@ -35,17 +35,17 @@
 			</div>
 		{{end}}
 	</h4>
-	{{if or .IsJSON (or .IsYAML .IsOdML)}}
+	{{if or .IsJSON (or .IsYAML .IsODML)}}
 	<div class="ui top attached tabular menu">
 		<a class="item {{if or .IsJSON .IsYAML}}active{{end}}" data-tab="code"><i class="octicon octicon-file-code"></i> Code</a>
-		<a class="item {{if .IsOdML}}active{{end}}" data-tab="view"><i class="octicon octicon-file"></i> View</a>
+		<a class="item {{if .IsODML}}active{{end}}" data-tab="view"><i class="octicon octicon-file"></i> View</a>
 	</div>
 	{{end}}
-	<div class="{{if or .IsJSON (or .IsYAML .IsOdML)}}ui bottom attached tab {{if .IsOdML}} active{{end}} segment" data-tab="view{{else}}ui attached table segment{{end}}">
+	<div class="{{if or .IsJSON (or .IsYAML .IsODML)}}ui bottom attached tab {{if .IsODML}} active{{end}} segment" data-tab="view{{else}}ui attached table segment{{end}}">
 		<div id="{{if not (or .IsJSON .IsYAML)}}{{if .IsIPythonNotebook}}ipython-notebook{{end}}" class="file-view {{if .IsMarkdown}}markdown{{else if .IsIPythonNotebook}}ipython-notebook{{else if .IsIPythonNotebook}}ipython-notebook{{else if .ReadmeInList}}plain-text{{else if and .IsTextFile}}code-view{{end}} has-emoji{{end}}">
 			{{if .IsMarkdown}}
 				{{if .FileContent}}{{.FileContent | Str2HTML}}{{end}}
-			{{else if .IsOdML}}
+			{{else if .IsODML}}
 			<div class="ui fluid input">
 				<input class="search-input form-control" placeholder="Search"></input>
 			</div>
@@ -61,7 +61,7 @@
 					});
 					$('#jstree').jstree({
 						'core': {
-							'data': [{{.OdML| Str2JS}}]
+							'data': [{{.ODML| Str2JS}}]
 				},
 					"search": {
 						"case_insensitive": true,
@@ -163,8 +163,8 @@
 			{{end}}
 		</div>
 	</div>
-	{{if or .IsJSON (or .IsYAML .IsOdML)}}
-	<div class="ui codetab bottom attached tab {{if not .IsOdML}}active{{end}} segment" data-tab="code">
+	{{if or .IsJSON (or .IsYAML .IsODML)}}
+	<div class="ui codetab bottom attached tab {{if not .IsODML}}active{{end}} segment" data-tab="code">
 		<div class="file-view code-view has-emoji">
 			<table>
 				<tbody>