diff --git a/README.md b/README.md index 10175335..901133a5 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,16 @@ This is an add-on for Caddy which wants to deliver a good UI to edit the content of the website. +## Add-on configuration + +You can define, or not, the admin UI styles. It will **not** replace the default ones, it will be included after it. The path must be relative to ```public``` folder. + +``` +hugo { + styles [file] +} +``` + ## Try it ### Prepare your machine diff --git a/browse/browse.go b/browse/browse.go index b62ce675..253aebf8 100644 --- a/browse/browse.go +++ b/browse/browse.go @@ -6,27 +6,25 @@ import ( "strings" "text/template" - "github.com/hacdias/caddy-hugo/editor" + "github.com/hacdias/caddy-hugo/config" "github.com/hacdias/caddy-hugo/utils" "github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware/browse" ) -// ServeHTTP is... -func ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { - if r.URL.Path[len(r.URL.Path)-1] != '/' { - http.Redirect(w, r, r.URL.Path+"/", http.StatusTemporaryRedirect) - return 0, nil - } - +// ServeHTTP is used to serve the content of Browse page +// using Browse middleware from Caddy +func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) { + // Removes the page main path from the URL r.URL.Path = strings.Replace(r.URL.Path, "/admin/browse", "", 1) + // If the URL is blank now, replace it with a trailing slash if r.URL.Path == "" { r.URL.Path = "/" } functions := template.FuncMap{ - "canBeEdited": editor.CanBeEdited, + "CanBeEdited": utils.CanBeEdited, "Defined": utils.Defined, } @@ -45,6 +43,7 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { Configs: []browse.Config{ browse.Config{ PathScope: "/", + Variables: c, Template: tpl, }, }, diff --git a/config/config.go b/config/config.go new file mode 100644 index 00000000..b9f90b24 --- /dev/null +++ b/config/config.go @@ -0,0 +1,35 @@ +package config + +import ( + "strings" + + "github.com/mholt/caddy/config/setup" +) + +// Config is the add-on configuration set on Caddyfile +type Config struct { + Styles string +} + +// ParseHugo parses the configuration file +func ParseHugo(c *setup.Controller) (*Config, error) { + conf := &Config{} + + for c.Next() { + for c.NextBlock() { + switch c.Val() { + case "styles": + if !c.NextArg() { + return nil, c.ArgErr() + } + conf.Styles = c.Val() + // Remove the beginning slash if it exists or not + conf.Styles = strings.TrimPrefix(conf.Styles, "/") + // Add a beginning slash to make a + conf.Styles = "/" + conf.Styles + } + } + } + + return conf, nil +} diff --git a/editor/editor.go b/editor/editor.go index 28ffc3f7..9ff5324d 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -12,6 +12,7 @@ import ( "strings" "text/template" + "github.com/hacdias/caddy-hugo/config" "github.com/hacdias/caddy-hugo/frontmatter" "github.com/hacdias/caddy-hugo/utils" "github.com/spf13/hugo/parser" @@ -23,38 +24,21 @@ type editor struct { Mode string Content string FrontMatter interface{} + Config *config.Config } -// ServeHTTP is... -func ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { +// ServeHTTP serves the editor page +func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) { filename := strings.Replace(r.URL.Path, "/admin/edit/", "", 1) if r.Method == "POST" { - return post(w, r, filename) + return servePost(w, r, filename) } - return get(w, r, filename) + return serveGet(w, r, c, filename) } -// CanBeEdited checks if a file has a supported extension -func CanBeEdited(filename string) bool { - extensions := [...]string{".markdown", ".md", - ".json", ".toml", ".yaml", - ".css", ".sass", ".scss", - ".js", - ".html", - } - - for _, extension := range extensions { - if strings.HasSuffix(filename, extension) { - return true - } - } - - return false -} - -func post(w http.ResponseWriter, r *http.Request, filename string) (int, error) { +func servePost(w http.ResponseWriter, r *http.Request, filename string) (int, error) { // Get the JSON information sent using a buffer rawBuffer := new(bytes.Buffer) rawBuffer.ReadFrom(r.Body) @@ -152,10 +136,10 @@ func post(w http.ResponseWriter, r *http.Request, filename string) (int, error) return 200, nil } -func get(w http.ResponseWriter, r *http.Request, filename string) (int, error) { +func serveGet(w http.ResponseWriter, r *http.Request, c *config.Config, filename string) (int, error) { // Check if the file format is supported. If not, send a "Not Acceptable" // header and an error - if !CanBeEdited(filename) { + if !utils.CanBeEdited(filename) { return 406, errors.New("File format not supported.") } @@ -176,6 +160,7 @@ func get(w http.ResponseWriter, r *http.Request, filename string) (int, error) { page := new(editor) page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".") page.Name = filename + page.Config = c // Sanitize the extension page.Mode = sanitizeMode(page.Mode) @@ -227,7 +212,7 @@ func get(w http.ResponseWriter, r *http.Request, filename string) (int, error) { // Create the functions map, then the template, check for erros and // execute the template if there aren't errors functions := template.FuncMap{ - "splitCapitalize": utils.SplitCapitalize, + "SplitCapitalize": utils.SplitCapitalize, "Defined": utils.Defined, } diff --git a/hugo.go b/hugo.go index 3780993c..05905185 100644 --- a/hugo.go +++ b/hugo.go @@ -11,6 +11,7 @@ import ( "github.com/hacdias/caddy-hugo/assets" "github.com/hacdias/caddy-hugo/browse" + "github.com/hacdias/caddy-hugo/config" "github.com/hacdias/caddy-hugo/editor" "github.com/hacdias/caddy-hugo/utils" "github.com/mholt/caddy/config/setup" @@ -20,16 +21,21 @@ import ( // Setup configures the middleware func Setup(c *setup.Controller) (middleware.Middleware, error) { + config, _ := config.ParseHugo(c) commands.Execute() return func(next middleware.Handler) middleware.Handler { - return &handler{Next: next} + return &CaddyHugo{Next: next, Config: config} }, nil } -type handler struct{ Next middleware.Handler } +// CaddyHugo main type +type CaddyHugo struct { + Next middleware.Handler + Config *config.Config +} -func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { +func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { // Only handle /admin path if middleware.Path(r.URL.Path).Matches("/admin") { var err error @@ -103,12 +109,12 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) // Browse page if page == "browse" { - code, err = browse.ServeHTTP(w, r) + code, err = browse.ServeHTTP(w, r, h.Config) } // Edit page if page == "edit" { - code, err = editor.ServeHTTP(w, r) + code, err = editor.ServeHTTP(w, r, h.Config) } // Whenever the header "X-Refenerate" is true, the website should be diff --git a/templates/base_full.tmpl b/templates/base_full.tmpl index 1c88e6c1..b464c42c 100644 --- a/templates/base_full.tmpl +++ b/templates/base_full.tmpl @@ -10,6 +10,8 @@ + {{ if and (Defined . "Config")}}{{ if not (eq .Config.Styles "") }}{{ end }}{{ end }} + {{ if and (Defined . "User") }}{{ if not (eq .User.Styles "") }}{{ end }}{{ end }} diff --git a/templates/browse.tmpl b/templates/browse.tmpl index 8fa550f8..0366ebb6 100644 --- a/templates/browse.tmpl +++ b/templates/browse.tmpl @@ -46,7 +46,7 @@