From 6f972d1ae4a9e922fd01ee34ae0653b577b6d975 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 20 Sep 2015 22:00:25 +0100 Subject: [PATCH] close #14 --- README.md | 9 ++++++--- config/config.go | 13 +++++++++++++ hugo.go | 5 ++--- utils/utils.go | 8 ++++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 901133a5..7545a262 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,17 @@ This is an add-on for Caddy which wants to deliver a good UI to edit the content ## 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] + styles file + flags flags... } ``` ++ **file** is the relative path to ```public``` folder of the admin UI styles. They will not replace the defaults, they will be added. + ++ **flags** are the Hugo flags (those which can be set in the command line) and they must follow one of these syntaxes: ```-f=value``` and ```--flag=value```. + ## Try it ### Prepare your machine diff --git a/config/config.go b/config/config.go index aa0cc4ed..91ef7f2f 100644 --- a/config/config.go +++ b/config/config.go @@ -9,6 +9,7 @@ import ( // Config is the add-on configuration set on Caddyfile type Config struct { Styles string + Flags []string } // ParseHugo parses the configuration file @@ -29,9 +30,21 @@ func ParseHugo(c *setup.Controller) (*Config, error) { conf.Styles = strings.TrimPrefix(conf.Styles, "/") // Add a beginning slash to make a conf.Styles = "/" + conf.Styles + case "flags": + conf.Flags = c.RemainingArgs() + if len(conf.Flags) == 0 { + return conf, c.ArgErr() + } } } } + conf.parseFlags() return conf, nil } + +func (c *Config) parseFlags() { + for index, element := range c.Flags { + c.Flags[index] = strings.Replace(element, "\"", "", -1) + } +} diff --git a/hugo.go b/hugo.go index 05905185..8761d8e2 100644 --- a/hugo.go +++ b/hugo.go @@ -16,13 +16,12 @@ import ( "github.com/hacdias/caddy-hugo/utils" "github.com/mholt/caddy/config/setup" "github.com/mholt/caddy/middleware" - "github.com/spf13/hugo/commands" ) // Setup configures the middleware func Setup(c *setup.Controller) (middleware.Middleware, error) { config, _ := config.ParseHugo(c) - commands.Execute() + utils.RunHugo(config) return func(next middleware.Handler) middleware.Handler { return &CaddyHugo{Next: next, Config: config} @@ -120,7 +119,7 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error // Whenever the header "X-Refenerate" is true, the website should be // regenerated. Used in edit and settings, for example. if r.Header.Get("X-Regenerate") == "true" { - commands.Execute() + utils.RunHugo(h.Config) } return code, err diff --git a/utils/utils.go b/utils/utils.go index d014437e..f1bda8a3 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -12,8 +12,16 @@ import ( "unicode" "github.com/hacdias/caddy-hugo/assets" + "github.com/hacdias/caddy-hugo/config" + "github.com/spf13/hugo/commands" ) +// RunHugo is used to run hugo +func RunHugo(c *config.Config) { + commands.HugoCmd.ParseFlags(c.Flags) + commands.HugoCmd.Run(commands.HugoCmd, make([]string, 0)) +} + // CopyFile is used to copy a file func CopyFile(old, new string) error { // Open the file and create a new one