editor improvements

This commit is contained in:
Henrique Dias 2017-07-02 17:53:47 +01:00
parent 1f7974de38
commit de66f1a9fb
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
3 changed files with 20 additions and 183 deletions

44
file.go
View File

@ -1,6 +1,7 @@
package filemanager
import (
"bytes"
"context"
"crypto/md5"
"crypto/sha1"
@ -20,7 +21,7 @@ import (
"strings"
"time"
"github.com/hacdias/filemanager/frontmatter"
"github.com/spf13/hugo/parser"
)
var (
@ -87,7 +88,7 @@ func getInfo(url *url.URL, c *FileManager, u *User) (*file, error) {
Path: filepath.Join(u.Scope, url.Path),
}
info, err := u.fileSystem.Stat(context.TODO(), i.Path)
info, err := u.fileSystem.Stat(context.TODO(), url.Path)
if err != nil {
return i, err
}
@ -165,40 +166,28 @@ func (i *file) getListing(c *requestContext, r *http.Request) error {
// getEditor gets the editor based on a Info struct
func (i *file) getEditor() error {
i.Language = editorLanguage(i.Extension)
// If the editor will hold only content, leave now.
if editorMode(i.Language) == "content" {
return nil
}
// If the file doesn't have any kind of metadata, leave now.
if !frontmatter.HasRune(i.Language) {
if !hasRune(i.Content) {
return nil
}
/*
if e.Mode == "complete" && hasRune {
var page parser.Page
content := []byte(i.Content)
// Starts a new buffer and parses the file using Hugo's functions
buffer := bytes.NewBuffer([]byte(i.Content))
page, err := parser.ReadFrom(buffer)
buffer := bytes.NewBuffer(content)
page, err = parser.ReadFrom(buffer)
// If there is an error, just ignore it and return nil.
// This way, the file can be served for editing.
if err != nil {
if err != nil {
goto Error
}
// Parses the page content and the frontmatter
i.Content = strings.TrimSpace(string(page.Content()))
e.FrontMatter.Rune = rune(content[0])
e.FrontMatter.Content, _, err = frontmatter.Pretty(page.FrontMatter())
}
if e.Mode == "complete" && !hasRune {
err = errors.New("Complete but without rune")
} */
return nil
}
i.Content = strings.TrimSpace(string(page.Content()))
i.Metadata = strings.TrimSpace(string(page.FrontMatter()))
return nil
}
@ -416,6 +405,13 @@ var textExtensions = [...]string{
".f", ".bas", ".d", ".ada", ".nim", ".cr", ".java", ".cs", ".vala", ".vapi",
}
// hasRune checks if the file has the frontmatter rune
func hasRune(file string) bool {
return strings.HasPrefix(file, "---") ||
strings.HasPrefix(file, "+++") ||
strings.HasPrefix(file, "{")
}
func editorMode(language string) string {
switch language {
case "markdown", "asciidoc", "rst":

View File

@ -1,28 +0,0 @@
package frontmatter
import (
"strings"
)
// HasRune checks if the file has the frontmatter rune
func HasRune(file string) bool {
return strings.HasPrefix(file, "---") ||
strings.HasPrefix(file, "+++") ||
strings.HasPrefix(file, "{")
}
// AppendRune appends the frontmatter rune to a file
func AppendRune(frontmatter string, mark rune) string {
frontmatter = strings.TrimSpace(frontmatter)
switch mark {
case '-':
return "---\n" + frontmatter + "\n---"
case '+':
return "+++\n" + frontmatter + "\n+++"
case '{':
return "{\n" + frontmatter + "\n}"
}
return frontmatter
}

View File

@ -1,131 +0,0 @@
package frontmatter
import "testing"
type hasRuneTest struct {
File []byte
Return bool
}
var testHasRune = []hasRuneTest{
{
File: []byte(`---
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed auctor libero eget ante fermentum commodo.
---`),
Return: true,
},
{
File: []byte(`+++
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed auctor libero eget ante fermentum commodo.
+++`),
Return: true,
},
{
File: []byte(`{
"json": "Lorem ipsum dolor sit amet"
}`),
Return: true,
},
{
File: []byte(`+`),
Return: false,
},
{
File: []byte(`++`),
Return: false,
},
{
File: []byte(`-`),
Return: false,
},
{
File: []byte(`--`),
Return: false,
},
{
File: []byte(`Lorem ipsum`),
Return: false,
},
}
func TestHasRune(t *testing.T) {
for _, test := range testHasRune {
if HasRune(test.File) != test.Return {
t.Error("Incorrect value on HasRune")
}
}
}
type appendRuneTest struct {
Before []byte
After []byte
Mark rune
}
var testAppendRuneTest = []appendRuneTest{}
func TestAppendRune(t *testing.T) {
for i, test := range testAppendRuneTest {
if !compareByte(AppendRune(test.Before, test.Mark), test.After) {
t.Errorf("Incorrect value on AppendRune of Test %d", i)
}
}
}
func compareByte(a, b []byte) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
var testRuneToStringFormat = map[rune]string{
'-': "yaml",
'+': "toml",
'{': "json",
'}': "json",
'1': "",
'a': "",
}
func TestRuneToStringFormat(t *testing.T) {
for mark, format := range testRuneToStringFormat {
val, _ := RuneToStringFormat(mark)
if val != format {
t.Errorf("Incorrect value on RuneToStringFormat of %v; want: %s; got: %s", mark, format, val)
}
}
}
var testStringFormatToRune = map[string]rune{
"yaml": '-',
"toml": '+',
"json": '{',
"lorem": '0',
}
func TestStringFormatToRune(t *testing.T) {
for format, mark := range testStringFormatToRune {
val, _ := StringFormatToRune(format)
if val != mark {
t.Errorf("Incorrect value on StringFormatToRune of %s; want: %v; got: %v", format, mark, val)
}
}
}