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