pull/20/head
Henrique Dias 2015-09-20 22:00:25 +01:00
parent 96e4ec0baf
commit 6f972d1ae4
4 changed files with 29 additions and 6 deletions

View File

@ -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 ## 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 { 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 ## Try it
### Prepare your machine ### Prepare your machine

View File

@ -9,6 +9,7 @@ import (
// Config is the add-on configuration set on Caddyfile // Config is the add-on configuration set on Caddyfile
type Config struct { type Config struct {
Styles string Styles string
Flags []string
} }
// ParseHugo parses the configuration file // ParseHugo parses the configuration file
@ -29,9 +30,21 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
conf.Styles = strings.TrimPrefix(conf.Styles, "/") conf.Styles = strings.TrimPrefix(conf.Styles, "/")
// Add a beginning slash to make a // Add a beginning slash to make a
conf.Styles = "/" + conf.Styles conf.Styles = "/" + conf.Styles
case "flags":
conf.Flags = c.RemainingArgs()
if len(conf.Flags) == 0 {
return conf, c.ArgErr()
}
} }
} }
} }
conf.parseFlags()
return conf, nil return conf, nil
} }
func (c *Config) parseFlags() {
for index, element := range c.Flags {
c.Flags[index] = strings.Replace(element, "\"", "", -1)
}
}

View File

@ -16,13 +16,12 @@ import (
"github.com/hacdias/caddy-hugo/utils" "github.com/hacdias/caddy-hugo/utils"
"github.com/mholt/caddy/config/setup" "github.com/mholt/caddy/config/setup"
"github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware"
"github.com/spf13/hugo/commands"
) )
// Setup configures the middleware // Setup configures the middleware
func Setup(c *setup.Controller) (middleware.Middleware, error) { func Setup(c *setup.Controller) (middleware.Middleware, error) {
config, _ := config.ParseHugo(c) config, _ := config.ParseHugo(c)
commands.Execute() utils.RunHugo(config)
return func(next middleware.Handler) middleware.Handler { return func(next middleware.Handler) middleware.Handler {
return &CaddyHugo{Next: next, Config: config} 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 // Whenever the header "X-Refenerate" is true, the website should be
// regenerated. Used in edit and settings, for example. // regenerated. Used in edit and settings, for example.
if r.Header.Get("X-Regenerate") == "true" { if r.Header.Get("X-Regenerate") == "true" {
commands.Execute() utils.RunHugo(h.Config)
} }
return code, err return code, err

View File

@ -12,8 +12,16 @@ import (
"unicode" "unicode"
"github.com/hacdias/caddy-hugo/assets" "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 // CopyFile is used to copy a file
func CopyFile(old, new string) error { func CopyFile(old, new string) error {
// Open the file and create a new one // Open the file and create a new one