reuse frontmatter functions
parent
0d9c1fa6ef
commit
0bf1182c91
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
<h2>{{ splitCapitalize $value.Name }}</h2>
|
||||
{{ template "frontmatter" $value.Content }}
|
||||
{{ else}}
|
||||
<label for="{{ $value.Master }}_{{ $value.Name }}">{{ splitCapitalize $value.Name }}</label>
|
||||
<input name="{{ $value.Master }}_{{ $value.Name }}" id="{{ $value.Master }}_{{ $value.Name }}" value="{{ $value.Content }}"></input><br>
|
||||
<label for="{{ $value.Tag }}">{{ splitCapitalize $value.Name }}</label>
|
||||
<input name="{{ $value.Tag }}" id="{{ $value.Tag }}" value="{{ $value.Content }}"></input><br>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
<div class="content">
|
||||
<h1>Settings</h1>
|
||||
<form>
|
||||
{{ template "frontmatter" .Settings }}
|
||||
{{ template "frontmatter" . }}
|
||||
</form>
|
||||
</div>
|
||||
{{ end }}
|
||||
|
|
Loading…
Reference in New Issue