This commit is contained in:
Henrique Dias 2016-06-29 10:16:10 +01:00
parent e359792e69
commit b86c7fc7ed
2 changed files with 22 additions and 16 deletions

View File

@ -15,6 +15,7 @@ type Config struct {
Root http.FileSystem Root http.FileSystem
BaseURL string BaseURL string
StyleSheet string // Costum stylesheet StyleSheet string // Costum stylesheet
FrontMatter string // Default frontmatter to save files in
HugoEnabled bool // Enables the Hugo plugin for File Manager HugoEnabled bool // Enables the Hugo plugin for File Manager
} }
@ -34,7 +35,7 @@ func Parse(c *caddy.Controller) ([]Config, error) {
} }
for c.Next() { for c.Next() {
var cfg = Config{PathScope: ".", BaseURL: "", HugoEnabled: false} var cfg = Config{PathScope: ".", BaseURL: "", FrontMatter: "json", HugoEnabled: false}
for c.NextBlock() { for c.NextBlock() {
switch c.Val() { switch c.Val() {
case "show": case "show":

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"path/filepath" "path/filepath"
@ -44,7 +45,7 @@ func (i *Info) Update(w http.ResponseWriter, r *http.Request, c *config.Config)
mainContent = strings.TrimSpace(mainContent) mainContent = strings.TrimSpace(mainContent)
file = []byte(mainContent) file = []byte(mainContent)
case "complete": case "complete":
if file, code, err = parseCompleteFile(data, i.Name); err != nil { if file, code, err = parseCompleteFile(data, i.Name, c.FrontMatter); err != nil {
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
} }
default: default:
@ -63,6 +64,10 @@ func (i *Info) Update(w http.ResponseWriter, r *http.Request, c *config.Config)
func parseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, int, error) { func parseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, int, error) {
frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".") frontmatter := strings.TrimPrefix(filepath.Ext(filename), ".")
return parseFrontMatter(data, frontmatter)
}
func parseFrontMatter(data interface{}, frontmatter string) ([]byte, int, error) {
var mark rune var mark rune
switch frontmatter { switch frontmatter {
@ -99,32 +104,32 @@ func parseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, int, e
return f, http.StatusOK, nil return f, http.StatusOK, nil
} }
func parseCompleteFile(data map[string]interface{}, filename string) ([]byte, int, error) { func parseCompleteFile(data map[string]interface{}, filename string, frontmatter string) ([]byte, int, error) {
// The main content of the file mainContent := ""
mainContent := data["content"].(string)
mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"
// Removes the main content from the rest of the frontmatter if _, ok := data["content"]; ok {
delete(data, "content") // The main content of the file
mainContent = data["content"].(string)
mainContent = "\n\n" + strings.TrimSpace(mainContent) + "\n"
// Removes the main content from the rest of the frontmatter
delete(data, "content")
}
if _, ok := data["date"]; ok { if _, ok := data["date"]; ok {
data["date"] = data["date"].(string) + ":00" data["date"] = data["date"].(string) + ":00"
} }
// Converts the frontmatter in JSON front, code, err := parseFrontMatter(data, frontmatter)
jsonFrontmatter, err := json.Marshal(data)
if err != nil { if err != nil {
return []byte{}, http.StatusInternalServerError, err fmt.Println(frontmatter)
return []byte{}, code, err
} }
// Indents the json
frontMatterBuffer := new(bytes.Buffer)
json.Indent(frontMatterBuffer, jsonFrontmatter, "", " ")
// Generates the final file // Generates the final file
f := new(bytes.Buffer) f := new(bytes.Buffer)
f.Write(frontMatterBuffer.Bytes()) f.Write(front)
f.Write([]byte(mainContent)) f.Write([]byte(mainContent))
return f.Bytes(), http.StatusOK, nil return f.Bytes(), http.StatusOK, nil
} }