updates! settings!

This commit is contained in:
Henrique Dias 2015-09-13 22:48:52 +01:00
parent 082073acda
commit 788218e115
4 changed files with 182 additions and 36 deletions

23
hugo.go
View File

@ -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,18 +28,11 @@ 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)
}
return h.Next.ServeHTTP(w, r)
}
func route(w http.ResponseWriter, r *http.Request) (int, error) {
if middleware.Path(r.URL.Path).Matches("/admin") {
page := parseComponents(r)[1]
if page == "assets" {
filename := strings.Replace(r.URL.Path, assetsURL, "static", 1)
filename := strings.Replace(r.URL.Path, "/admin/assets", "static", 1)
file, err := assets.Asset(filename)
if err != nil {
@ -52,21 +46,26 @@ func route(w http.ResponseWriter, r *http.Request) (int, error) {
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" {
w.Write([]byte("Settings Page"))
} else {
return settings.Execute(w, r)
}
return 404, nil
}
return 200, nil
return h.Next.ServeHTTP(w, r)
}
// TODO: utils package
func parseComponents(r *http.Request) []string {
//The URL that the user queried.
path := r.URL.Path

View File

@ -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

81
settings/settings.go Normal file
View File

@ -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
}

View File

@ -0,0 +1,30 @@
{{#HEADER#}}
{{ with .Body }}
<div class="content">
<h1>Settings</h1>
<form>
{{ range $key, $value := . }}
{{ if isMap $value }}
<h2>{{ splitCapitalize $key }}</h2>
<div id="{{ $key }}">
<!-- call this range again -->
</div>
{{ else }}
<label for="{{ $key }}">{{ splitCapitalize $key }}</label>
<input name="{{ $key }}" id="{{ $key }}" value="{{ $value }}"></input><br>
{{ end }}
{{ end }}
</form>
</div>
{{ end }}
{{#FOOTER#}}