make frontmatter parser more "DRYer"
parent
f2362a6dcf
commit
992f1c2c01
File diff suppressed because one or more lines are too long
|
@ -43,17 +43,16 @@ $(document).on('ready pjax:success', function() {
|
|||
$('form').submit(function(event) {
|
||||
event.preventDefault();
|
||||
|
||||
var data = $(this).serializeJSON(),
|
||||
var data = JSON.stringify($(this).serializeJSON()),
|
||||
url = $(this).attr('action'),
|
||||
button = $(this).find("input[type=submit]:focus"),
|
||||
action = button.val();
|
||||
button = $(this).find("input[type=submit]:focus");
|
||||
|
||||
$.ajax({
|
||||
type: 'POST',
|
||||
url: url,
|
||||
data: data,
|
||||
beforeSend: function(xhr) {
|
||||
xhr.setRequestHeader('X-Save-Mode', action);
|
||||
xhr.setRequestHeader('X-Regenerate', button.data("regenerate"));
|
||||
},
|
||||
dataType: 'json',
|
||||
encode: true,
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
|
||||
"github.com/hacdias/caddy-hugo/frontmatter"
|
||||
"github.com/hacdias/caddy-hugo/page"
|
||||
"github.com/spf13/hugo/commands"
|
||||
"github.com/spf13/hugo/parser"
|
||||
)
|
||||
|
||||
|
@ -67,10 +66,6 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte("{}"))
|
||||
|
||||
if r.Header.Get("X-Save-Mode") == "Publish" {
|
||||
go commands.Execute()
|
||||
}
|
||||
} else {
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
log.Print(err)
|
||||
|
|
|
@ -2,12 +2,15 @@ package frontmatter
|
|||
|
||||
import (
|
||||
"log"
|
||||
"reflect"
|
||||
"sort"
|
||||
|
||||
"github.com/hacdias/caddy-hugo/utils"
|
||||
"github.com/spf13/hugo/parser"
|
||||
)
|
||||
|
||||
const mainName = "#MAIN#"
|
||||
|
||||
// Pretty creates a new FrontMatter object
|
||||
func Pretty(content []byte) (interface{}, error) {
|
||||
frontType := parser.DetectFrontMatter(rune(content[0]))
|
||||
|
@ -19,6 +22,7 @@ func Pretty(content []byte) (interface{}, error) {
|
|||
|
||||
object := new(frontmatter)
|
||||
object.Type = "object"
|
||||
object.Name = mainName
|
||||
|
||||
return rawToPretty(front, object), nil
|
||||
}
|
||||
|
@ -59,76 +63,22 @@ func rawToPretty(config interface{}, parent *frontmatter) interface{} {
|
|||
|
||||
if parent.Type == "array" {
|
||||
for index, element := range config.([]interface{}) {
|
||||
c := new(frontmatter)
|
||||
c.Parent = parent
|
||||
|
||||
if utils.IsMap(element) {
|
||||
c.Type = "object"
|
||||
|
||||
if parent.Name == "" {
|
||||
c.Name = c.Title
|
||||
} else {
|
||||
c.Name = parent.Name + "[" + c.Name + "]"
|
||||
}
|
||||
|
||||
c.Content = rawToPretty(config.([]interface{})[index], c)
|
||||
objects = append(objects, c)
|
||||
objects = append(objects, handleObjects(element, parent, string(index)))
|
||||
} else if utils.IsSlice(element) {
|
||||
c.Type = "array"
|
||||
c.Name = parent.Name + "[" + c.Name + "]"
|
||||
c.Content = rawToPretty(config.([]interface{})[index], c)
|
||||
|
||||
arrays = append(arrays, c)
|
||||
arrays = append(arrays, handleArrays(element, parent, string(index)))
|
||||
} else {
|
||||
// TODO: add string, boolean, number
|
||||
c.Type = "string"
|
||||
c.Name = parent.Name + "[]"
|
||||
c.Title = element.(string)
|
||||
c.Content = config.([]interface{})[index]
|
||||
fields = append(fields, c)
|
||||
fields = append(fields, handleFlatValues(element, parent, string(index)))
|
||||
}
|
||||
}
|
||||
} else if parent.Type == "object" {
|
||||
for name, element := range config.(map[string]interface{}) {
|
||||
c := new(frontmatter)
|
||||
c.Title = name
|
||||
c.Parent = parent
|
||||
|
||||
if utils.IsMap(element) {
|
||||
c.Type = "object"
|
||||
|
||||
if parent.Name == "" {
|
||||
c.Name = c.Title
|
||||
} else {
|
||||
c.Name = parent.Name + "[" + c.Title + "]"
|
||||
}
|
||||
|
||||
c.Content = rawToPretty(config.(map[string]interface{})[name], c)
|
||||
objects = append(objects, c)
|
||||
objects = append(objects, handleObjects(element, parent, name))
|
||||
} else if utils.IsSlice(element) {
|
||||
c.Type = "array"
|
||||
|
||||
if parent.Name == "" {
|
||||
c.Name = name
|
||||
} else {
|
||||
c.Name = parent.Name + "[" + c.Name + "]"
|
||||
}
|
||||
|
||||
c.Content = rawToPretty(config.(map[string]interface{})[c.Title], c)
|
||||
|
||||
arrays = append(arrays, c)
|
||||
arrays = append(arrays, handleArrays(element, parent, name))
|
||||
} else {
|
||||
// TODO: add string, boolean, number
|
||||
c.Type = "string"
|
||||
|
||||
if parent.Name == "" {
|
||||
c.Name = name
|
||||
} else {
|
||||
c.Name = parent.Name + "[" + name + "]"
|
||||
}
|
||||
|
||||
c.Content = element
|
||||
fields = append(fields, c)
|
||||
fields = append(fields, handleFlatValues(element, parent, name))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -143,6 +93,71 @@ func rawToPretty(config interface{}, parent *frontmatter) interface{} {
|
|||
settings = append(settings, fields...)
|
||||
settings = append(settings, arrays...)
|
||||
settings = append(settings, objects...)
|
||||
|
||||
return settings
|
||||
}
|
||||
|
||||
func handleObjects(content interface{}, parent *frontmatter, name string) *frontmatter {
|
||||
c := new(frontmatter)
|
||||
c.Parent = parent
|
||||
c.Type = "object"
|
||||
c.Title = name
|
||||
|
||||
if parent.Name == mainName {
|
||||
c.Name = c.Title
|
||||
} else {
|
||||
c.Name = parent.Name + "[" + c.Title + "]"
|
||||
}
|
||||
|
||||
c.Content = rawToPretty(content, c)
|
||||
return c
|
||||
}
|
||||
|
||||
func handleArrays(content interface{}, parent *frontmatter, name string) *frontmatter {
|
||||
c := new(frontmatter)
|
||||
c.Parent = parent
|
||||
c.Type = "array"
|
||||
c.Title = name
|
||||
|
||||
if parent.Type == "object" && parent.Name == mainName {
|
||||
c.Name = name
|
||||
} else {
|
||||
c.Name = parent.Name + "[" + c.Name + "]"
|
||||
}
|
||||
|
||||
c.Content = rawToPretty(content, c)
|
||||
return c
|
||||
}
|
||||
|
||||
func handleFlatValues(content interface{}, parent *frontmatter, name string) *frontmatter {
|
||||
c := new(frontmatter)
|
||||
c.Parent = parent
|
||||
|
||||
switch reflect.ValueOf(content).Kind() {
|
||||
case reflect.Bool:
|
||||
c.Type = "boolean"
|
||||
case reflect.Int:
|
||||
case reflect.Float32:
|
||||
case reflect.Float64:
|
||||
c.Type = "number"
|
||||
case reflect.String:
|
||||
default:
|
||||
c.Type = "string"
|
||||
}
|
||||
|
||||
if parent.Type == "array" {
|
||||
c.Name = parent.Name + "[]"
|
||||
c.Title = content.(string)
|
||||
} else if parent.Type == "object" {
|
||||
c.Title = name
|
||||
c.Name = parent.Name + "[" + name + "]"
|
||||
|
||||
if parent.Name == mainName {
|
||||
c.Name = name
|
||||
}
|
||||
} else {
|
||||
log.Panic("Parent type not allowed in handleFlatValues.")
|
||||
}
|
||||
|
||||
c.Content = content
|
||||
return c
|
||||
}
|
||||
|
|
14
hugo.go
14
hugo.go
|
@ -32,6 +32,8 @@ type handler struct{ Next middleware.Handler }
|
|||
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||
if middleware.Path(r.URL.Path).Matches("/admin") {
|
||||
page := utils.ParseComponents(r)[1]
|
||||
code := 400
|
||||
var err error
|
||||
|
||||
if page == "assets" {
|
||||
filename := strings.Replace(r.URL.Path, "/admin/", "", 1)
|
||||
|
@ -50,14 +52,18 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)
|
|||
w.Write(file)
|
||||
return 200, nil
|
||||
} else if page == "browse" {
|
||||
return browse.Execute(w, r)
|
||||
code, err = browse.Execute(w, r)
|
||||
} else if page == "edit" {
|
||||
return edit.Execute(w, r)
|
||||
code, err = edit.Execute(w, r)
|
||||
} else if page == "settings" {
|
||||
return settings.Execute(w, r)
|
||||
code, err = settings.Execute(w, r)
|
||||
}
|
||||
|
||||
return 404, nil
|
||||
if r.Header.Get("X-Regenerate") == "true" {
|
||||
commands.Execute()
|
||||
}
|
||||
|
||||
return code, err
|
||||
}
|
||||
|
||||
return h.Next.ServeHTTP(w, r)
|
||||
|
|
|
@ -10,7 +10,6 @@ import (
|
|||
|
||||
"github.com/hacdias/caddy-hugo/frontmatter"
|
||||
"github.com/hacdias/caddy-hugo/page"
|
||||
"github.com/spf13/hugo/commands"
|
||||
)
|
||||
|
||||
// Execute the page
|
||||
|
@ -46,10 +45,6 @@ func Execute(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte("{}"))
|
||||
|
||||
if r.Header.Get("X-Save-Mode") == "publish" {
|
||||
commands.Execute()
|
||||
}
|
||||
} else {
|
||||
content, err := ioutil.ReadFile("config." + language)
|
||||
|
||||
|
|
|
@ -14,8 +14,8 @@
|
|||
|
||||
<div class="action-bar">
|
||||
<button id="preview" class="left">Preview</button>
|
||||
<input type="submit" data-message="Post saved." value="Save">
|
||||
<input type="submit" data-message="Post published successfully." class="default" value="Publish">
|
||||
<input type="submit" data-message="Post saved." data-regenerate="false" value="Save">
|
||||
<input type="submit" data-message="Post published successfully." data-regenerate="true" class="default" value="Publish">
|
||||
</div>
|
||||
</form>
|
||||
</main>
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<h1>Settings</h1>
|
||||
<form method="POST" action="/admin/settings">
|
||||
{{ template "frontmatter" . }}
|
||||
<input type="submit" data-message="Settings updated." value="Save">
|
||||
<input type="submit" data-message="Settings updated." data-regenerate="true" value="Save">
|
||||
</form>
|
||||
</div>
|
||||
{{ end }}
|
||||
|
|
Loading…
Reference in New Issue