Former-commit-id: e4a5da856c
pull/726/head
Henrique Dias 2017-01-18 18:20:56 +00:00
parent b29d284591
commit 3b9c687af7
7 changed files with 69 additions and 31 deletions

View File

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

View File

@ -1,12 +1,12 @@
{{ define "content" }}
{{- 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 (eq .Class "complete")}}
<h2>Metadata</h2>
{{- end }}
<div class="frontmatter" data-type="parent">
{{- template "blocks" .FrontMatter }}
{{- template "blocks" .FrontMatter.Content }}
<div class="button add">Add field</div>
</div>
{{- end }}

View File

@ -66,7 +66,6 @@ func Parse(c *caddy.Controller) ([]Config, error) {
cfg.Scope = "."
cfg.FileSystem = webdav.Dir(cfg.Scope)
cfg.BaseURL = ""
cfg.FrontMatter = "yaml"
cfg.HugoEnabled = false
cfg.Users = map[string]*User{}
cfg.AllowCommands = true
@ -102,15 +101,6 @@ func Parse(c *caddy.Controller) ([]Config, error) {
for c.NextBlock() {
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":
if cfg.BeforeSave, err = CommandRunner(c); err != nil {
return configs, err
@ -239,7 +229,6 @@ func Parse(c *caddy.Controller) ([]Config, error) {
user.AllowEdit = cfg.AllowEdit
user.AllowNew = cfg.AllowEdit
user.Commands = cfg.Commands
user.FrontMatter = cfg.FrontMatter
user.Scope = cfg.Scope
user.FileSystem = cfg.FileSystem
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
Handler *webdav.Handler `json:"-"` // The WebDav HTTP Handler
StyleSheet string `json:"-"` // Costum stylesheet
FrontMatter string `json:"-"` // Default frontmatter to save files in
AllowNew bool // Can create files and folders
AllowEdit bool // Can edit/rename files
AllowCommands bool // Can execute commands

View File

@ -1,6 +1,9 @@
package frontmatter
import "strings"
import (
"bytes"
"strings"
)
// HasRune checks if the file has the frontmatter rune
func HasRune(file []byte) bool {
@ -10,15 +13,43 @@ func HasRune(file []byte) bool {
}
// AppendRune appends the frontmatter rune to a file
func AppendRune(frontmatter []byte, language string) []byte {
switch language {
case "yaml":
func AppendRune(frontmatter []byte, mark rune) []byte {
frontmatter = bytes.TrimSpace(frontmatter)
switch mark {
case '-':
return []byte("---\n" + string(frontmatter) + "\n---")
case "toml":
case '+':
return []byte("+++\n" + string(frontmatter) + "\n+++")
case "json":
case '{':
return []byte("{\n" + string(frontmatter) + "\n}")
}
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
Visual bool
Content string
FrontMatter *frontmatter.Content
FrontMatter struct {
Content *frontmatter.Content
Rune rune
}
}
// 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" {
// Checks if the file already has the frontmatter rune and parses it
if frontmatter.HasRune(i.Content) {
editor.FrontMatter, _, err = frontmatter.Pretty(i.Content)
editor.FrontMatter.Content, _, err = frontmatter.Pretty(i.Content)
} 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
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())
editor.FrontMatter.Rune = rune(i.Content[0])
editor.FrontMatter.Content, _, err = frontmatter.Pretty(page.FrontMatter())
}
}

View File

@ -7,6 +7,7 @@ import (
"io/ioutil"
"net/http"
"path/filepath"
"strconv"
"strings"
"github.com/hacdias/caddy-filemanager/config"
@ -48,7 +49,18 @@ func PreProccessPUT(
mainContent = strings.TrimSpace(mainContent)
file = []byte(mainContent)
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
}
default:
@ -100,7 +112,7 @@ func ParseFrontMatter(data interface{}, front string) ([]byte, error) {
}
// 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 := ""
if _, ok := data["content"]; ok {
@ -116,12 +128,13 @@ func ParseCompleteFile(data map[string]interface{}, filename string, frontmatter
data["date"] = data["date"].(string) + ":00"
}
front, err := ParseFrontMatter(data, frontmatter)
front, err := frontmatter.Marshal(data, mark)
if err != nil {
return []byte{}, err
}
front = frontmatter.AppendRune(front, mark)
// Generates the final file
f := new(bytes.Buffer)
f.Write(front)