From 788218e1154410fd4beabdc758ca0391f63b7c8e Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 13 Sep 2015 22:48:52 +0100 Subject: [PATCH] updates! settings! --- hugo.go | 69 +++++++++++++++++------------------ page/page.go | 38 ++++++++++++++++++- settings/settings.go | 81 +++++++++++++++++++++++++++++++++++++++++ templates/settings.tmpl | 30 +++++++++++++++ 4 files changed, 182 insertions(+), 36 deletions(-) create mode 100644 settings/settings.go diff --git a/hugo.go b/hugo.go index 70e44cae..ea2b1f4a 100644 --- a/hugo.go +++ b/hugo.go @@ -10,6 +10,7 @@ import ( "github.com/hacdias/caddy-hugo/assets" "github.com/hacdias/caddy-hugo/edit" + "github.com/hacdias/caddy-hugo/settings" "github.com/mholt/caddy/config/setup" "github.com/mholt/caddy/middleware" "github.com/spf13/hugo/commands" @@ -27,46 +28,44 @@ func Setup(c *setup.Controller) (middleware.Middleware, error) { type handler struct{ Next middleware.Handler } func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { - if urlMatch(r, "/admin") { - return route(w, r) + if middleware.Path(r.URL.Path).Matches("/admin") { + page := parseComponents(r)[1] + + if page == "assets" { + filename := strings.Replace(r.URL.Path, "/admin/assets", "static", 1) + file, err := assets.Asset(filename) + + if err != nil { + return 404, nil + } + + extension := filepath.Ext(filename) + mime := mime.TypeByExtension(extension) + + header := w.Header() + header.Set("Content-Type", mime) + + w.Write(file) + return 200, nil + } else if page == "content" { + w.Write([]byte("Content Page")) + return 200, nil + } else if page == "browse" { + w.Write([]byte("Show Data Folder")) + return 200, nil + } else if page == "edit" { + return edit.Execute(w, r) + } else if page == "settings" { + return settings.Execute(w, r) + } + + return 404, nil } return h.Next.ServeHTTP(w, r) } -func route(w http.ResponseWriter, r *http.Request) (int, error) { - page := parseComponents(r)[1] - - if page == "assets" { - filename := strings.Replace(r.URL.Path, assetsURL, "static", 1) - file, err := assets.Asset(filename) - - if err != nil { - return 404, nil - } - - extension := filepath.Ext(filename) - mime := mime.TypeByExtension(extension) - - header := w.Header() - header.Set("Content-Type", mime) - - w.Write(file) - } else if page == "content" { - w.Write([]byte("Content Page")) - } else if page == "browse" { - w.Write([]byte("Show Data Folder")) - } else if page == "edit" { - return edit.Execute(w, r) - } else if page == "settings" { - w.Write([]byte("Settings Page")) - } else { - return 404, nil - } - - return 200, nil -} - +// TODO: utils package func parseComponents(r *http.Request) []string { //The URL that the user queried. path := r.URL.Path diff --git a/page/page.go b/page/page.go index d1d9b938..420dde73 100644 --- a/page/page.go +++ b/page/page.go @@ -3,7 +3,9 @@ package page import ( "html/template" "net/http" + "reflect" "strings" + "unicode" "github.com/hacdias/caddy-hugo/assets" ) @@ -14,6 +16,40 @@ const ( footerMark = "{{#FOOTER#}}" ) +var funcMap = template.FuncMap{ + "splitCapitalize": splitCapitalize, + "isMap": isMap, +} + +// TODO: utilspackage +func isMap(sth interface{}) bool { + return reflect.ValueOf(sth).Kind() == reflect.Map +} + +// TODO: utils package +func splitCapitalize(name string) string { + var words []string + l := 0 + for s := name; s != ""; s = s[l:] { + l = strings.IndexFunc(s[1:], unicode.IsUpper) + 1 + if l <= 0 { + l = len(s) + } + words = append(words, s[:l]) + } + + name = "" + + for _, element := range words { + name += element + " " + } + + name = strings.ToLower(name[:len(name)-1]) + name = strings.ToUpper(string(name[0])) + name[1:len(name)] + + return name +} + // Page type type Page struct { Title string @@ -48,7 +84,7 @@ func (p *Page) Render(name string, w http.ResponseWriter) (int, error) { page = strings.Replace(page, headerMark, header, -1) page = strings.Replace(page, footerMark, footer, -1) - tpl, err := template.New("page").Parse(page) + tpl, err := template.New("page").Funcs(funcMap).Parse(page) if err != nil { return 500, err diff --git a/settings/settings.go b/settings/settings.go new file mode 100644 index 00000000..1028dc56 --- /dev/null +++ b/settings/settings.go @@ -0,0 +1,81 @@ +package settings + +import ( + "io/ioutil" + "net/http" + "os" + + "github.com/hacdias/caddy-hugo/page" + "github.com/spf13/hugo/parser" +) + +// Execute the page +func Execute(w http.ResponseWriter, r *http.Request) (int, error) { + if r.Method == "POST" { + + } else { + frontmatter := getConfigFrontMatter() + + // 500 if the format of frontmatter can't be defined + if frontmatter == "" { + return 500, nil + } + + config, err := getConfig(frontmatter) + + if err != nil { + return 500, err + } + + page := new(page.Page) + page.Title = "settings" + page.Body = config + return page.Render("settings", w) + } + + return 200, nil +} + +func getConfigFrontMatter() string { + var frontmatter string + + if _, err := os.Stat("config.yaml"); err == nil { + frontmatter = "yaml" + } + + if _, err := os.Stat("config.json"); err == nil { + frontmatter = "json" + } + + if _, err := os.Stat("config.toml"); err == nil { + frontmatter = "toml" + } + + return frontmatter +} + +func getConfig(frontmatter string) (interface{}, error) { + content := getConfigFileContent(frontmatter) + + switch frontmatter { + case "yaml": + return parser.HandleYAMLMetaData(content) + case "json": + return parser.HandleJSONMetaData(content) + case "toml": + return parser.HandleTOMLMetaData(content) + } + + return []string{}, nil +} + +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 +} diff --git a/templates/settings.tmpl b/templates/settings.tmpl index e69de29b..61200fd7 100644 --- a/templates/settings.tmpl +++ b/templates/settings.tmpl @@ -0,0 +1,30 @@ +{{#HEADER#}} +{{ with .Body }} + +
+ +

Settings

+ +
+ {{ range $key, $value := . }} + {{ if isMap $value }} +

{{ splitCapitalize $key }}

+ +
+ +
+ + {{ else }} + + +
+ + {{ end }} + {{ end }} +
+ + +
+ +{{ end }} +{{#FOOTER#}}