diff --git a/frontmatter/frontmatter.go b/frontmatter/frontmatter.go new file mode 100644 index 00000000..2cfb5117 --- /dev/null +++ b/frontmatter/frontmatter.go @@ -0,0 +1,74 @@ +package frontmatter + +import ( + "sort" + + "github.com/hacdias/caddy-hugo/utils" + "github.com/spf13/hugo/parser" +) + +// Pretty creates a new FrontMatter object +func Pretty(content []byte, language string) (interface{}, error) { + var err error + var c interface{} + + if language == "yaml" { + c, err = parser.HandleYAMLMetaData(content) + } else if language == "json" { + c, err = parser.HandleJSONMetaData(content) + } else if language == "toml" { + c, err = parser.HandleTOMLMetaData(content) + } + + if err != nil { + return []string{}, err + } + + return rawToPretty(c, ""), nil +} + +type frontmatter struct { + Name string + Tag string + Content interface{} + SubContent bool +} + +func rawToPretty(config interface{}, master string) interface{} { + var mapsNames []string + var stringsNames []string + + for index, element := range config.(map[string]interface{}) { + if utils.IsMap(element) { + mapsNames = append(mapsNames, index) + } else { + stringsNames = append(stringsNames, index) + } + } + + sort.Strings(mapsNames) + sort.Strings(stringsNames) + names := append(stringsNames, mapsNames...) + + settings := make([]interface{}, len(names)) + + for index := range names { + c := new(frontmatter) + c.Name = names[index] + c.Tag = master + "_" + names[index] + c.SubContent = false + + i := config.(map[string]interface{})[names[index]] + + if utils.IsMap(i) { + c.Content = rawToPretty(i, c.Name) + c.SubContent = true + } else { + c.Content = i + } + + settings[index] = c + } + + return settings +} diff --git a/settings/settings.go b/settings/settings.go index 67be6471..66280ea0 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -2,49 +2,45 @@ package settings import ( "io/ioutil" + "log" "net/http" "os" - "sort" + "github.com/hacdias/caddy-hugo/frontmatter" "github.com/hacdias/caddy-hugo/page" - "github.com/hacdias/caddy-hugo/utils" - "github.com/spf13/hugo/parser" ) -type settings struct { - Settings interface{} - Keys []string -} - // Execute the page func Execute(w http.ResponseWriter, r *http.Request) (int, error) { if r.Method == "POST" { } else { - frontmatter := getConfigFrontMatter() + language := getConfigFrontMatter() - // 500 if the format of frontmatter can't be defined - if frontmatter == "" { + if language == "" { + log.Print("Configuration frontmatter can't be defined") return 500, nil } - config, err := getConfig(frontmatter) + content, err := ioutil.ReadFile("config." + language) if err != nil { + log.Print(err) return 500, err } - // configIndex := getConfigNames(config) + f, err := frontmatter.Pretty(content, language) - cnf := new(settings) - cnf.Settings = getConfigNames(config, "") + if err != nil { + log.Print(err) + return 500, err + } page := new(page.Page) page.Title = "Settings" - page.Body = cnf + page.Body = f return page.Render(w, "settings", "frontmatter") } - return 200, nil } @@ -65,77 +61,3 @@ func getConfigFrontMatter() string { return frontmatter } - -func getConfigFileContent(frontmatter string) []byte { - file, err := ioutil.ReadFile("config." + frontmatter) - - if err != nil { - // there were a problem opening the file - return []byte{} - } - - return file -} - -// make it generic to frontmatter. everything bellow -> new file -func getConfig(frontmatter string) (interface{}, error) { - content := getConfigFileContent(frontmatter) - // config := []string{} - - // get the config into a map - if frontmatter == "yaml" { - return parser.HandleYAMLMetaData(content) - } else if frontmatter == "json" { - return parser.HandleJSONMetaData(content) - } else if frontmatter == "toml" { - return parser.HandleTOMLMetaData(content) - } - - return []string{}, nil -} - -type conf struct { - Name string - Master string - Content interface{} - SubContent bool -} - -func getConfigNames(config interface{}, master string) interface{} { - var mapsNames []string - var stringsNames []string - - for index, element := range config.(map[string]interface{}) { - if utils.IsMap(element) { - mapsNames = append(mapsNames, index) - } else { - stringsNames = append(stringsNames, index) - } - } - - sort.Strings(mapsNames) - sort.Strings(stringsNames) - names := append(stringsNames, mapsNames...) - - settings := make([]interface{}, len(names)) - - for index := range names { - c := new(conf) - c.Name = names[index] - c.Master = master - c.SubContent = false - - i := config.(map[string]interface{})[names[index]] - - if utils.IsMap(i) { - c.Content = getConfigNames(i, c.Name) - c.SubContent = true - } else { - c.Content = i - } - - settings[index] = c - } - - return settings -} diff --git a/templates/frontmatter.tmpl b/templates/frontmatter.tmpl index b7062fcd..cb9261b1 100644 --- a/templates/frontmatter.tmpl +++ b/templates/frontmatter.tmpl @@ -4,8 +4,8 @@