This commit is contained in:
Henrique Dias 2017-01-18 18:20:56 +00:00
parent b46dcb6ccb
commit e4a5da856c
7 changed files with 69 additions and 31 deletions

View File

@ -188,7 +188,8 @@ document.addEventListener("DOMContentLoaded", (event) => {
} }
let container = document.getElementById('editor'), let container = document.getElementById('editor'),
kind = container.dataset.kind; kind = container.dataset.kind,
rune = container.dataset.rune;
if(kind != 'frontmatter-only') { if(kind != 'frontmatter-only') {
let editor = document.querySelector('.content #ace'), let editor = document.querySelector('.content #ace'),
@ -240,7 +241,8 @@ document.addEventListener("DOMContentLoaded", (event) => {
buttons.setLoading('save') buttons.setLoading('save')
webdav.put(window.location.pathname, JSON.stringify(data), { webdav.put(window.location.pathname, JSON.stringify(data), {
'Kind': kind 'Kind': kind,
'Rune': rune
}) })
.then(() => { .then(() => {
buttons.setDone('save') buttons.setDone('save')

View File

@ -1,12 +1,12 @@
{{ define "content" }} {{ define "content" }}
{{- with .Data }} {{- with .Data }}
<form id="editor" {{ if eq .Mode "markdown" }}class="markdown"{{ end }} data-kind="{{ .Class }}"> <form id="editor" {{ if eq .Mode "markdown" }}class="markdown"{{ end }} data-kind="{{ .Class }}" data-rune="{{ if eq .Class "complete" }}{{ .FrontMatter.Rune }}{{ end }}">
{{- if or (eq .Class "frontmatter-only") (eq .Class "complete") }} {{- if or (eq .Class "frontmatter-only") (eq .Class "complete") }}
{{- if (eq .Class "complete")}} {{- if (eq .Class "complete")}}
<h2>Metadata</h2> <h2>Metadata</h2>
{{- end }} {{- end }}
<div class="frontmatter" data-type="parent"> <div class="frontmatter" data-type="parent">
{{- template "blocks" .FrontMatter }} {{- template "blocks" .FrontMatter.Content }}
<div class="button add">Add field</div> <div class="button add">Add field</div>
</div> </div>
{{- end }} {{- end }}

View File

@ -66,7 +66,6 @@ func Parse(c *caddy.Controller) ([]Config, error) {
cfg.Scope = "." cfg.Scope = "."
cfg.FileSystem = webdav.Dir(cfg.Scope) cfg.FileSystem = webdav.Dir(cfg.Scope)
cfg.BaseURL = "" cfg.BaseURL = ""
cfg.FrontMatter = "yaml"
cfg.HugoEnabled = false cfg.HugoEnabled = false
cfg.Users = map[string]*User{} cfg.Users = map[string]*User{}
cfg.AllowCommands = true cfg.AllowCommands = true
@ -102,15 +101,6 @@ func Parse(c *caddy.Controller) ([]Config, error) {
for c.NextBlock() { for c.NextBlock() {
switch c.Val() { switch c.Val() {
case "frontmatter":
if !c.NextArg() {
return configs, c.ArgErr()
}
user.FrontMatter = c.Val()
if user.FrontMatter != "yaml" && user.FrontMatter != "json" && user.FrontMatter != "toml" {
return configs, c.Err("frontmatter type not supported")
}
case "before_save": case "before_save":
if cfg.BeforeSave, err = CommandRunner(c); err != nil { if cfg.BeforeSave, err = CommandRunner(c); err != nil {
return configs, err return configs, err
@ -239,7 +229,6 @@ func Parse(c *caddy.Controller) ([]Config, error) {
user.AllowEdit = cfg.AllowEdit user.AllowEdit = cfg.AllowEdit
user.AllowNew = cfg.AllowEdit user.AllowNew = cfg.AllowEdit
user.Commands = cfg.Commands user.Commands = cfg.Commands
user.FrontMatter = cfg.FrontMatter
user.Scope = cfg.Scope user.Scope = cfg.Scope
user.FileSystem = cfg.FileSystem user.FileSystem = cfg.FileSystem
user.Rules = cfg.Rules user.Rules = cfg.Rules

View File

@ -12,7 +12,6 @@ type User struct {
FileSystem webdav.FileSystem `json:"-"` // The virtual file system the user have access FileSystem webdav.FileSystem `json:"-"` // The virtual file system the user have access
Handler *webdav.Handler `json:"-"` // The WebDav HTTP Handler Handler *webdav.Handler `json:"-"` // The WebDav HTTP Handler
StyleSheet string `json:"-"` // Costum stylesheet StyleSheet string `json:"-"` // Costum stylesheet
FrontMatter string `json:"-"` // Default frontmatter to save files in
AllowNew bool // Can create files and folders AllowNew bool // Can create files and folders
AllowEdit bool // Can edit/rename files AllowEdit bool // Can edit/rename files
AllowCommands bool // Can execute commands AllowCommands bool // Can execute commands

View File

@ -1,6 +1,9 @@
package frontmatter package frontmatter
import "strings" import (
"bytes"
"strings"
)
// HasRune checks if the file has the frontmatter rune // HasRune checks if the file has the frontmatter rune
func HasRune(file []byte) bool { func HasRune(file []byte) bool {
@ -10,15 +13,43 @@ func HasRune(file []byte) bool {
} }
// AppendRune appends the frontmatter rune to a file // AppendRune appends the frontmatter rune to a file
func AppendRune(frontmatter []byte, language string) []byte { func AppendRune(frontmatter []byte, mark rune) []byte {
switch language { frontmatter = bytes.TrimSpace(frontmatter)
case "yaml":
switch mark {
case '-':
return []byte("---\n" + string(frontmatter) + "\n---") return []byte("---\n" + string(frontmatter) + "\n---")
case "toml": case '+':
return []byte("+++\n" + string(frontmatter) + "\n+++") return []byte("+++\n" + string(frontmatter) + "\n+++")
case "json": case '{':
return []byte("{\n" + string(frontmatter) + "\n}") return []byte("{\n" + string(frontmatter) + "\n}")
} }
return frontmatter return frontmatter
} }
func RuneToStringFormat(mark rune) string {
switch mark {
case '-':
return "yaml"
case '+':
return "toml"
case '{':
return "json"
default:
return ""
}
}
func StringFormatToRune(format string) rune {
switch format {
case "yaml":
return '-'
case "toml":
return '+'
case "json":
return '{'
default:
return '0'
}
}

View File

@ -18,7 +18,10 @@ type Editor struct {
Mode string Mode string
Visual bool Visual bool
Content string Content string
FrontMatter *frontmatter.Content FrontMatter struct {
Content *frontmatter.Content
Rune rune
}
} }
// GetEditor gets the editor based on a FileInfo struct // GetEditor gets the editor based on a FileInfo struct
@ -41,9 +44,10 @@ func GetEditor(r *http.Request, i *file.Info) (*Editor, error) {
if editor.Class == "frontmatter-only" { if editor.Class == "frontmatter-only" {
// Checks if the file already has the frontmatter rune and parses it // Checks if the file already has the frontmatter rune and parses it
if frontmatter.HasRune(i.Content) { if frontmatter.HasRune(i.Content) {
editor.FrontMatter, _, err = frontmatter.Pretty(i.Content) editor.FrontMatter.Content, _, err = frontmatter.Pretty(i.Content)
} else { } else {
editor.FrontMatter, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.Mode)) editor.FrontMatter.Rune = frontmatter.StringFormatToRune(editor.Mode)
editor.FrontMatter.Content, _, err = frontmatter.Pretty(frontmatter.AppendRune(i.Content, editor.FrontMatter.Rune))
} }
} }
@ -52,12 +56,12 @@ func GetEditor(r *http.Request, i *file.Info) (*Editor, error) {
// Starts a new buffer and parses the file using Hugo's functions // Starts a new buffer and parses the file using Hugo's functions
buffer := bytes.NewBuffer(i.Content) buffer := bytes.NewBuffer(i.Content)
page, err = parser.ReadFrom(buffer) page, err = parser.ReadFrom(buffer)
editor.Class = "complete"
if err == nil { if err == nil {
// Parses the page content and the frontmatter // Parses the page content and the frontmatter
editor.Content = strings.TrimSpace(string(page.Content())) editor.Content = strings.TrimSpace(string(page.Content()))
editor.FrontMatter, _, err = frontmatter.Pretty(page.FrontMatter()) editor.FrontMatter.Rune = rune(i.Content[0])
editor.FrontMatter.Content, _, err = frontmatter.Pretty(page.FrontMatter())
} }
} }

View File

@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"github.com/hacdias/caddy-filemanager/config" "github.com/hacdias/caddy-filemanager/config"
@ -48,7 +49,18 @@ func PreProccessPUT(
mainContent = strings.TrimSpace(mainContent) mainContent = strings.TrimSpace(mainContent)
file = []byte(mainContent) file = []byte(mainContent)
case "complete": case "complete":
if file, err = ParseCompleteFile(data, r.URL.Path, u.FrontMatter); err != nil { var mark rune
if v := r.Header.Get("Rune"); v != "" {
n, err := strconv.Atoi(v)
if err != nil {
return err
}
mark = rune(n)
}
if file, err = ParseCompleteFile(data, r.URL.Path, mark); err != nil {
return return
} }
default: default:
@ -100,7 +112,7 @@ func ParseFrontMatter(data interface{}, front string) ([]byte, error) {
} }
// ParseCompleteFile parses a complete file // ParseCompleteFile parses a complete file
func ParseCompleteFile(data map[string]interface{}, filename string, frontmatter string) ([]byte, error) { func ParseCompleteFile(data map[string]interface{}, filename string, mark rune) ([]byte, error) {
mainContent := "" mainContent := ""
if _, ok := data["content"]; ok { if _, ok := data["content"]; ok {
@ -116,12 +128,13 @@ func ParseCompleteFile(data map[string]interface{}, filename string, frontmatter
data["date"] = data["date"].(string) + ":00" data["date"] = data["date"].(string) + ":00"
} }
front, err := ParseFrontMatter(data, frontmatter) front, err := frontmatter.Marshal(data, mark)
if err != nil { if err != nil {
return []byte{}, err return []byte{}, err
} }
front = frontmatter.AppendRune(front, mark)
// Generates the final file // Generates the final file
f := new(bytes.Buffer) f := new(bytes.Buffer)
f.Write(front) f.Write(front)