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"
 	"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=")
 	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
 			return
 		}
 		}
 
 
-		c.Data["IsOdML"] = tool.IsOdmlFile(buf)
+		c.Data["IsODML"] = tool.IsODMLFile(buf)
 
 
 		d, _ := ioutil.ReadAll(dataRc)
 		d, _ := ioutil.ReadAll(dataRc)
 		buf = append(buf, d...)
 		buf = append(buf, d...)

+ 11 - 20
routes/repo/view.go

@@ -5,21 +5,15 @@
 package repo
 package repo
 
 
 import (
 import (
-	"bufio"
 	"bytes"
 	"bytes"
-	"encoding/json"
-	"encoding/xml"
 	"fmt"
 	"fmt"
 	gotemplate "html/template"
 	gotemplate "html/template"
-	"io"
 	"io/ioutil"
 	"io/ioutil"
-	"os"
 	"path"
 	"path"
 	"strings"
 	"strings"
 
 
 	"github.com/G-Node/git-module"
 	"github.com/G-Node/git-module"
 	gannex "github.com/G-Node/go-annex"
 	gannex "github.com/G-Node/go-annex"
-	"github.com/G-Node/godML/odml"
 	"github.com/G-Node/gogs/models"
 	"github.com/G-Node/gogs/models"
 	"github.com/G-Node/gogs/pkg/context"
 	"github.com/G-Node/gogs/pkg/context"
 	"github.com/G-Node/gogs/pkg/markup"
 	"github.com/G-Node/gogs/pkg/markup"
@@ -29,7 +23,6 @@ import (
 	"github.com/G-Node/gogs/pkg/tool"
 	"github.com/G-Node/gogs/pkg/tool"
 	"github.com/Unknwon/paginater"
 	"github.com/Unknwon/paginater"
 	"github.com/go-macaron/captcha"
 	"github.com/go-macaron/captcha"
-	"golang.org/x/net/html/charset"
 	log "gopkg.in/clog.v1"
 	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()))
 			c.Data["FileContent"] = string(markup.OrgMode(buf, path.Dir(treeLink), c.Repo.Repository.ComposeMetas()))
 		case markup.IPYTHON_NOTEBOOK:
 		case markup.IPYTHON_NOTEBOOK:
 			c.Data["IsIPythonNotebook"] = true
 			c.Data["IsIPythonNotebook"] = true
+			// GIN mod: JSON, YAML, and odML render support with jsTree
 		case markup.JSON:
 		case markup.JSON:
 			c.Data["IsJSON"] = true
 			c.Data["IsJSON"] = true
 			c.Data["RawFileContent"] = string(buf)
 			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["IsYAML"] = true
 			c.Data["RawFileContent"] = string(buf)
 			c.Data["RawFileContent"] = string(buf)
 			fallthrough
 			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
 			fallthrough
 		default:
 		default:
 			// Building code view blocks with line number on server side.
 			// 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 {
 			} else {
 				fileContent = content
 				fileContent = content
 			}
 			}
+
 			var output bytes.Buffer
 			var output bytes.Buffer
 			lines := strings.Split(fileContent, "\n")
 			lines := strings.Split(fileContent, "\n")
 			// Remove blank line at the end of file
 			// Remove blank line at the end of file
 			if len(lines) > 0 && len(lines[len(lines)-1]) == 0 {
 			if len(lines) > 0 && len(lines[len(lines)-1]) == 0 {
 				lines = lines[:len(lines)-1]
 				lines = lines[:len(lines)-1]
 			}
 			}
+			// > GIN
 			if len(lines) > setting.UI.MaxLineHighlight {
 			if len(lines) > setting.UI.MaxLineHighlight {
 				c.Data["HighlightClass"] = "nohighlight"
 				c.Data["HighlightClass"] = "nohighlight"
 			}
 			}
+			// < GIN
 			for index, line := range lines {
 			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")
 				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>
 		<script src="{{AppSubURL}}/plugins/marked-0.3.6/marked.min.js"></script>
 	{{end}}
 	{{end}}
 
 
-	{{if .IsOdML}}
+	{{if .IsODML}}
 		<script type="text/javascript" src="{{AppSubURL}}/plugins/xonomy/xonomy.js"></script>
 		<script type="text/javascript" src="{{AppSubURL}}/plugins/xonomy/xonomy.js"></script>
 		<link type="text/css" rel="stylesheet" href="{{AppSubURL}}/plugins/xonomy/xonomy.css"/>
 		<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}}
 					{{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>
 					<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}}
 					{{end}}
-					{{if .IsOdML}}
+					{{if .IsODML}}
 					<a class="item" data-tab="odmleditor"><i class="octicon octicon-file"></i>Graphical Editor</a>
 					<a class="item" data-tab="odmleditor"><i class="octicon octicon-file"></i>Graphical Editor</a>
 					{{end}}
 					{{end}}
 					{{if not .IsNewFile}}
 					{{if not .IsNewFile}}
@@ -106,7 +106,7 @@
 				</div>
 				</div>
 				{{end}}
 				{{end}}
 
 
-				{{if .IsOdML}}
+				{{if .IsODML}}
 				<div class="ui bottom attached tab segment" data-tab="odmleditor">
 				<div class="ui bottom attached tab segment" data-tab="odmleditor">
 					<div id="xonomy_edit"></div>
 					<div id="xonomy_edit"></div>
 				</div>
 				</div>

+ 7 - 7
templates/repo/view_file.tmpl

@@ -35,17 +35,17 @@
 			</div>
 			</div>
 		{{end}}
 		{{end}}
 	</h4>
 	</h4>
-	{{if or .IsJSON (or .IsYAML .IsOdML)}}
+	{{if or .IsJSON (or .IsYAML .IsODML)}}
 	<div class="ui top attached tabular menu">
 	<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 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>
 	</div>
 	{{end}}
 	{{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}}">
 		<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 .IsMarkdown}}
 				{{if .FileContent}}{{.FileContent | Str2HTML}}{{end}}
 				{{if .FileContent}}{{.FileContent | Str2HTML}}{{end}}
-			{{else if .IsOdML}}
+			{{else if .IsODML}}
 			<div class="ui fluid input">
 			<div class="ui fluid input">
 				<input class="search-input form-control" placeholder="Search"></input>
 				<input class="search-input form-control" placeholder="Search"></input>
 			</div>
 			</div>
@@ -61,7 +61,7 @@
 					});
 					});
 					$('#jstree').jstree({
 					$('#jstree').jstree({
 						'core': {
 						'core': {
-							'data': [{{.OdML| Str2JS}}]
+							'data': [{{.ODML| Str2JS}}]
 				},
 				},
 					"search": {
 					"search": {
 						"case_insensitive": true,
 						"case_insensitive": true,
@@ -163,8 +163,8 @@
 			{{end}}
 			{{end}}
 		</div>
 		</div>
 	</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">
 		<div class="file-view code-view has-emoji">
 			<table>
 			<table>
 				<tbody>
 				<tbody>