diff --git a/file.go b/file.go index eb4f442b..ed5a11b5 100644 --- a/file.go +++ b/file.go @@ -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": diff --git a/frontmatter/runes.go b/frontmatter/runes.go deleted file mode 100644 index 2e20fddc..00000000 --- a/frontmatter/runes.go +++ /dev/null @@ -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 -} diff --git a/frontmatter/runes_test.go b/frontmatter/runes_test.go deleted file mode 100644 index c8abc066..00000000 --- a/frontmatter/runes_test.go +++ /dev/null @@ -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) - } - } -}