2016-06-16 16:04:38 +00:00
|
|
|
package hugo
|
|
|
|
|
|
|
|
import (
|
2016-06-21 14:28:15 +00:00
|
|
|
"io/ioutil"
|
2016-06-16 16:04:38 +00:00
|
|
|
"log"
|
2016-06-21 14:28:15 +00:00
|
|
|
"net/http"
|
2016-06-16 16:04:38 +00:00
|
|
|
"os"
|
2016-06-21 14:28:15 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2016-06-16 16:04:38 +00:00
|
|
|
|
2016-06-21 14:28:15 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager"
|
|
|
|
"github.com/hacdias/caddy-hugo/installer"
|
2016-06-16 16:04:38 +00:00
|
|
|
"github.com/mholt/caddy"
|
|
|
|
"github.com/mholt/caddy/caddyhttp/httpserver"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
caddy.RegisterPlugin("hugo", caddy.Plugin{
|
|
|
|
ServerType: "http",
|
|
|
|
Action: setup,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup is the init function of Caddy plugins and it configures the whole
|
|
|
|
// middleware thing.
|
|
|
|
func setup(c *caddy.Controller) error {
|
|
|
|
cnf := httpserver.GetConfig(c.Key)
|
2016-06-21 14:28:15 +00:00
|
|
|
conf, _ := ParseHugo(c, cnf.Root)
|
2016-06-16 16:04:38 +00:00
|
|
|
|
|
|
|
// Checks if there is an Hugo website in the path that is provided.
|
|
|
|
// If not, a new website will be created.
|
|
|
|
create := true
|
|
|
|
|
2016-06-21 14:28:15 +00:00
|
|
|
if _, err := os.Stat(conf.Root + "config.yaml"); err == nil {
|
2016-06-16 16:04:38 +00:00
|
|
|
create = false
|
|
|
|
}
|
|
|
|
|
2016-06-21 14:28:15 +00:00
|
|
|
if _, err := os.Stat(conf.Root + "config.json"); err == nil {
|
2016-06-16 16:04:38 +00:00
|
|
|
create = false
|
|
|
|
}
|
|
|
|
|
2016-06-21 14:28:15 +00:00
|
|
|
if _, err := os.Stat(conf.Root + "config.toml"); err == nil {
|
2016-06-16 16:04:38 +00:00
|
|
|
create = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if create {
|
2016-06-21 14:28:15 +00:00
|
|
|
err := Run(conf.Hugo, []string{"new", "site", conf.Root, "--force"}, ".")
|
2016-06-16 16:04:38 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generates the Hugo website for the first time the plugin is activated.
|
2016-06-21 14:28:15 +00:00
|
|
|
go RunHugo(conf, true)
|
2016-06-16 16:04:38 +00:00
|
|
|
|
|
|
|
mid := func(next httpserver.Handler) httpserver.Handler {
|
2016-06-21 14:28:15 +00:00
|
|
|
return &Hugo{
|
|
|
|
Next: next,
|
|
|
|
Config: conf,
|
|
|
|
FileManager: &filemanager.FileManager{
|
|
|
|
Next: next,
|
|
|
|
Configs: []filemanager.Config{
|
|
|
|
filemanager.Config{
|
|
|
|
PathScope: conf.Root,
|
|
|
|
Root: http.Dir(conf.Root),
|
|
|
|
BaseURL: conf.BaseURL,
|
|
|
|
StyleSheet: conf.Styles,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-06-16 16:04:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cnf.AddMiddleware(mid)
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-21 14:28:15 +00:00
|
|
|
|
|
|
|
// Config contains the configuration of hugo plugin
|
|
|
|
type Config struct {
|
|
|
|
Args []string // Hugo arguments
|
|
|
|
Git bool // Is this site a git repository
|
|
|
|
BaseURL string // Admin URL to listen on
|
|
|
|
Hugo string // Hugo executable path
|
|
|
|
Root string // Hugo website path
|
|
|
|
Public string // Public content path
|
|
|
|
Styles string // Admin stylesheet
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseHugo parses the configuration file
|
|
|
|
func ParseHugo(c *caddy.Controller, root string) (*Config, error) {
|
|
|
|
conf := &Config{
|
|
|
|
Public: strings.Replace(root, "./", "", -1),
|
|
|
|
BaseURL: "/admin",
|
|
|
|
Root: "./",
|
|
|
|
Git: false,
|
|
|
|
Hugo: installer.GetPath(),
|
|
|
|
}
|
|
|
|
|
2016-06-21 15:39:53 +00:00
|
|
|
stlsbytes, err := Asset("public/css/styles.css")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return conf, err
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.Styles = string(stlsbytes)
|
|
|
|
|
2016-06-21 14:28:15 +00:00
|
|
|
for c.Next() {
|
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
|
|
|
switch len(args) {
|
|
|
|
case 1:
|
|
|
|
conf.Root = args[0]
|
|
|
|
conf.Root = strings.TrimSuffix(conf.Root, "/")
|
|
|
|
conf.Root += "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case "styles":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return conf, c.ArgErr()
|
|
|
|
}
|
|
|
|
stylesheet, err := ioutil.ReadFile(c.Val())
|
|
|
|
if err != nil {
|
|
|
|
return conf, err
|
|
|
|
}
|
2016-06-21 15:39:53 +00:00
|
|
|
conf.Styles += string(stylesheet)
|
2016-06-21 14:28:15 +00:00
|
|
|
case "admin":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
conf.BaseURL = c.Val()
|
|
|
|
// Remove the beginning slash if it exists or not
|
|
|
|
conf.BaseURL = strings.TrimPrefix(conf.BaseURL, "/")
|
|
|
|
// Add a beginning slash to make a
|
|
|
|
conf.BaseURL = "/" + conf.BaseURL
|
|
|
|
default:
|
|
|
|
key := "--" + c.Val()
|
|
|
|
value := "true"
|
|
|
|
|
|
|
|
if c.NextArg() {
|
|
|
|
value = c.Val()
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.Args = append(conf.Args, key+"="+value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(filepath.Join(conf.Root, ".git")); err == nil {
|
|
|
|
conf.Git = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|