move some stuff

This commit is contained in:
Henrique Dias
2016-10-22 12:07:19 +01:00
parent df888b604a
commit 5fce287cd2
10 changed files with 45 additions and 44 deletions

84
handlers/editor.go Normal file
View File

@@ -0,0 +1,84 @@
package handlers
import (
"bytes"
"path/filepath"
"strings"
"github.com/hacdias/caddy-filemanager/file"
"github.com/hacdias/caddy-filemanager/frontmatter"
"github.com/spf13/hugo/parser"
)
// Editor contains the information for the editor page
type Editor struct {
Class string
Mode string
Content string
FrontMatter *frontmatter.Content
}
// GetEditor gets the editor based on a FileInfo struct
func GetEditor(i *file.Info) (*Editor, error) {
// Create a new editor variable and set the mode
editor := new(Editor)
editor.Mode = strings.TrimPrefix(filepath.Ext(i.Name()), ".")
switch editor.Mode {
case "md", "markdown", "mdown", "mmark":
editor.Mode = "markdown"
case "asciidoc", "adoc", "ad":
editor.Mode = "asciidoc"
case "rst":
editor.Mode = "rst"
case "html", "htm":
editor.Mode = "html"
case "js":
editor.Mode = "javascript"
}
var page parser.Page
var err error
// Handle the content depending on the file extension
switch editor.Mode {
case "json", "toml", "yaml":
// Defines the class and declares an error
editor.Class = "frontmatter-only"
// Checks if the file already has the frontmatter rune and parses it
if frontmatter.HasRune(i.Content) {
editor.FrontMatter, _, err = frontmatter.Pretty(i.Content)
} else {
editor.FrontMatter, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.Mode))
}
// Check if there were any errors
if err == nil {
break
}
fallthrough
case "markdown", "asciidoc", "rst":
if frontmatter.HasRune(i.Content) {
// Starts a new buffer and parses the file using Hugo's functions
buffer := bytes.NewBuffer(i.Content)
page, err = parser.ReadFrom(buffer)
editor.Class = "complete"
if err == nil {
// Parses the page content and the frontmatter
editor.Content = strings.TrimSpace(string(page.Content()))
editor.FrontMatter, _, err = frontmatter.Pretty(page.FrontMatter())
break
}
}
fallthrough
default:
editor.Class = "content-only"
editor.Content = i.StringifyContent()
}
return editor, nil
}

View File

@@ -9,7 +9,7 @@ import (
"github.com/hacdias/caddy-filemanager/config"
"github.com/hacdias/caddy-filemanager/file"
"github.com/hacdias/caddy-filemanager/page"
"github.com/hacdias/caddy-filemanager/utils"
"github.com/hacdias/caddy-filemanager/utils/errors"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
@@ -20,7 +20,7 @@ func ServeListing(w http.ResponseWriter, r *http.Request, c *config.Config, u *c
// Loads the content of the directory
listing, err := file.GetListing(u, i.VirtualPath, r.URL.Path)
if err != nil {
return utils.ErrorToHTTPCode(err, true), err
return errors.ErrorToHTTPCode(err, true), err
}
listing.Context = httpserver.Context{

View File

@@ -6,7 +6,7 @@ import (
"github.com/hacdias/caddy-filemanager/config"
"github.com/hacdias/caddy-filemanager/file"
"github.com/hacdias/caddy-filemanager/page"
"github.com/hacdias/caddy-filemanager/utils"
"github.com/hacdias/caddy-filemanager/utils/errors"
)
// ServeSingle serves a single file in an editor (if it is editable), shows the
@@ -14,7 +14,7 @@ import (
func ServeSingle(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.User, i *file.Info) (int, error) {
err := i.Read()
if err != nil {
return utils.ErrorToHTTPCode(err, true), err
return errors.ErrorToHTTPCode(err, true), err
}
p := &page.Page{
@@ -29,7 +29,7 @@ func ServeSingle(w http.ResponseWriter, r *http.Request, c *config.Config, u *co
}
if i.CanBeEdited() && u.AllowEdit {
p.Data, err = i.GetEditor()
p.Data, err = GetEditor(i)
if err != nil {
return http.StatusInternalServerError, err
}