diff --git a/README.md b/README.md index 641befcf..af12e082 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ -# caddy-cms +# caddy-hugo -[![Build](https://img.shields.io/travis/hacdias/caddy-cms.svg?style=flat-square)](https://travis-ci.org/hacdias/caddy-cms) -[![Documentation](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](http://godoc.org/github.com/hacdias/caddy-cms) +[![Build](https://img.shields.io/travis/hacdias/caddy-hugo.svg?style=flat-square)](https://travis-ci.org/hacdias/caddy-hugo) +[![Documentation](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](http://godoc.org/github.com/hacdias/caddy-hugo) -Powerful and easy static site generator with admin interface. By default it uses [Hugo](http://gohugo.io/) (and you don't need to install it separately) but you can use whatever you want. +Powerful and easy static site generator with admin interface with [Hugo](http://gohugo.io/) (and you don't need to install it separately). ## Build it from source -If you want to try caddy-cms plugin (and improve it maybe), you'll have to install some tools. +If you want to try caddy-hugo plugin (and improve it maybe), you'll have to install some tools. + [Go 1.4 or higher](https://golang.org/dl/) + [caddydev](https://github.com/caddyserver/caddydev) @@ -22,7 +22,7 @@ If you want to go deeper and make changes in front-end assets like JavaScript or ### Run it -If you have already installed everything above to meet the requirements for what you want to do, let's start. Firstly, open the terminal and navigate to your clone of ```caddy-cms```. Then execute: +If you have already installed everything above to meet the requirements for what you want to do, let's start. Firstly, open the terminal and navigate to your clone of ```caddy-hugo```. Then execute: ``` go-bindata [-debug] -pkg assets -o assets/assets.go templates/ assets/css/ assets/js/ assets/fonts/ @@ -32,14 +32,14 @@ That command will create an ```assets.go``` file which contains all static files Now, open the folder with your static website and create a Caddyfile. Read the [docs](http://caddyserver.com/docs/cms) for more information about the directives of this plugin. -After creating the file, navigate to that folder using the terminal and run the following command, replacing ```{caddy-cms}``` with the location of your clone. +After creating the file, navigate to that folder using the terminal and run the following command, replacing ```{caddy-hugo}``` with the location of your clone. ``` -caddydev --source {caddy-cms} hugo +caddydev --source {caddy-hugo} hugo ``` Navigate to the url you set on Caddyfile to see your blog running on Caddy and Hugo. Go to ```/admin``` to try the Admin UI. Everything is working now. Whenever you make a change in the back-end source code, you'll have to run the command above again. -**For those who want to make changes in front-end**, make sure you have every needed tool installed and run ```npm install``` in the root of ```caddy-cms``` clone. Then, run ```grunt watch```. +**For those who want to make changes in front-end**, make sure you have every needed tool installed and run ```npm install``` in the root of ```caddy-hugo``` clone. Then, run ```grunt watch```. diff --git a/browse/browse.go b/browse/browse.go index 8527d6c5..38cc31bd 100644 --- a/browse/browse.go +++ b/browse/browse.go @@ -1,10 +1,11 @@ package browse import ( + "errors" "net/http" "strings" - "github.com/hacdias/caddy-cms/config" + "github.com/hacdias/caddy-hugo/config" ) // ServeHTTP is used to serve the content of Browse page @@ -21,6 +22,6 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, e case "GET": return GET(w, r, c) default: - return 400, nil + return 400, errors.New("Invalid method.") } } diff --git a/browse/delete.go b/browse/delete.go index 2da7a19a..72099039 100644 --- a/browse/delete.go +++ b/browse/delete.go @@ -5,7 +5,7 @@ import ( "os" "strings" - "github.com/hacdias/caddy-cms/config" + "github.com/hacdias/caddy-hugo/config" ) // DELETE handles the DELETE method on browse page diff --git a/browse/get.go b/browse/get.go index 7006d512..fce9c656 100644 --- a/browse/get.go +++ b/browse/get.go @@ -4,8 +4,8 @@ import ( "net/http" "text/template" - "github.com/hacdias/caddy-cms/config" - "github.com/hacdias/caddy-cms/utils" + "github.com/hacdias/caddy-hugo/config" + "github.com/hacdias/caddy-hugo/utils" "github.com/mholt/caddy/middleware" "github.com/mholt/caddy/middleware/browse" ) diff --git a/browse/post.go b/browse/post.go index b0f09c7f..539b4311 100644 --- a/browse/post.go +++ b/browse/post.go @@ -10,8 +10,8 @@ import ( "os" "strings" - "github.com/hacdias/caddy-cms/config" - "github.com/hacdias/caddy-cms/utils" + "github.com/hacdias/caddy-hugo/config" + "github.com/hacdias/caddy-hugo/utils" ) // POST handles the POST method on browse page @@ -132,6 +132,8 @@ func upload(w http.ResponseWriter, r *http.Request) (int, error) { w.Write([]byte(err.Error())) return http.StatusInternalServerError, err } + + defer outfile.Close() } } diff --git a/config/config.go b/config/config.go index cb115cb1..a514283c 100644 --- a/config/config.go +++ b/config/config.go @@ -8,21 +8,17 @@ import ( // Config is the add-on configuration set on Caddyfile type Config struct { - Public string - Content string - Path string - Styles string - Command string - Hugo bool + Public string // Public content path + Path string // Hugo files path + Styles string // Admin styles path + Args []string // Hugo arguments } -// ParseCMS parses the configuration file -func ParseCMS(c *setup.Controller) (*Config, error) { +// ParseHugo parses the configuration file +func ParseHugo(c *setup.Controller) (*Config, error) { conf := &Config{ - Public: strings.Replace(c.Root, "./", "", -1), - Content: "content", - Hugo: true, - Path: "./", + Public: strings.Replace(c.Root, "./", "", -1), + Path: "./", } for c.Next() { @@ -46,20 +42,18 @@ func ParseCMS(c *setup.Controller) (*Config, error) { conf.Styles = strings.TrimPrefix(conf.Styles, "/") // Add a beginning slash to make a conf.Styles = "/" + conf.Styles - case "content": + case "args": if !c.NextArg() { return nil, c.ArgErr() } - conf.Content = c.Val() - case "command": - if !c.NextArg() { - return nil, c.ArgErr() - } - conf.Command = c.Val() - if conf.Command != "" && !strings.HasPrefix(conf.Command, "-") { - conf.Hugo = false + // Get the arguments and split the array + args := strings.Split(c.Val(), " ") + for index, element := range args { + args[index] = strings.Replace(element, "\"", "", -1) } + + conf.Args = args } } } diff --git a/editor/editor.go b/editor/editor.go index 40ad2e61..97e6f61e 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -1,10 +1,11 @@ package editor import ( + "errors" "net/http" "strings" - "github.com/hacdias/caddy-cms/config" + "github.com/hacdias/caddy-hugo/config" ) // ServeHTTP serves the editor page @@ -18,6 +19,6 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, e case "GET": return GET(w, r, c, filename) default: - return 400, nil + return 400, errors.New("Invalid method.") } } diff --git a/editor/get.go b/editor/get.go index 22159a48..940a46f0 100644 --- a/editor/get.go +++ b/editor/get.go @@ -10,9 +10,9 @@ import ( "strings" "text/template" - "github.com/hacdias/caddy-cms/config" - "github.com/hacdias/caddy-cms/frontmatter" - "github.com/hacdias/caddy-cms/utils" + "github.com/hacdias/caddy-hugo/config" + "github.com/hacdias/caddy-hugo/frontmatter" + "github.com/hacdias/caddy-hugo/utils" "github.com/spf13/hugo/parser" ) diff --git a/editor/post.go b/editor/post.go index 83c0590c..eeed8958 100644 --- a/editor/post.go +++ b/editor/post.go @@ -10,8 +10,8 @@ import ( "strings" "time" - "github.com/hacdias/caddy-cms/config" - "github.com/hacdias/caddy-cms/utils" + "github.com/hacdias/caddy-hugo/config" + "github.com/hacdias/caddy-hugo/utils" "github.com/robfig/cron" "github.com/spf13/hugo/parser" ) @@ -81,7 +81,7 @@ func parseFrontMatterOnlyFile(rawFile map[string]interface{}, filename string) ( case "yaml": mark = rune('-') default: - return []byte{}, http.StatusBadRequest, errors.New("can't define the frontmatter") + return []byte{}, http.StatusBadRequest, errors.New("Can't define the frontmatter.") } f, err := parser.InterfaceToFrontMatter(rawFile, mark) @@ -117,7 +117,7 @@ func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]int // Schedule the post if r.Header.Get("X-Schedule") == "true" { - t, err := time.Parse("http.StatusOK6-01-02 15:04:05-07:00", rawFile["date"].(string)) + t, err := time.Parse("2006-01-02 15:04:05-07:00", rawFile["date"].(string)) if err != nil { return []byte{}, http.StatusInternalServerError, err diff --git a/frontmatter/frontmatter.go b/frontmatter/frontmatter.go index f65d2778..c8668ea7 100644 --- a/frontmatter/frontmatter.go +++ b/frontmatter/frontmatter.go @@ -6,7 +6,7 @@ import ( "sort" "strings" - "github.com/hacdias/caddy-cms/utils" + "github.com/hacdias/caddy-hugo/utils" "github.com/spf13/hugo/parser" ) diff --git a/cms.go b/hugo.go similarity index 85% rename from cms.go rename to hugo.go index 72da5d1b..e3d80291 100644 --- a/cms.go +++ b/hugo.go @@ -2,7 +2,7 @@ //go:generate go install github.com/jteeuwen/go-bindata/go-bindata //go:generate go-bindata -pkg assets -o assets/assets.go templates/ assets/css/ assets/js/ assets/fonts/ -package cms +package hugo import ( "mime" @@ -11,32 +11,32 @@ import ( "path/filepath" "strings" - "github.com/hacdias/caddy-cms/assets" - "github.com/hacdias/caddy-cms/browse" - "github.com/hacdias/caddy-cms/config" - "github.com/hacdias/caddy-cms/editor" - "github.com/hacdias/caddy-cms/utils" + "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" "github.com/mholt/caddy/middleware" ) // Setup configures the middleware func Setup(c *setup.Controller) (middleware.Middleware, error) { - config, _ := config.ParseCMS(c) + config, _ := config.ParseHugo(c) utils.Run(config) return func(next middleware.Handler) middleware.Handler { - return &CaddyCMS{Next: next, Config: config} + return &CaddyHugo{Next: next, Config: config} }, nil } -// CaddyCMS main type -type CaddyCMS struct { +// CaddyHugo main type +type CaddyHugo struct { Next middleware.Handler Config *config.Config } -func (h CaddyCMS) 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 @@ -60,9 +60,9 @@ func (h CaddyCMS) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) } } - // If the current page is only "/admin/", redirect to "/admin/browse/contents" + // If the current page is only "/admin/", redirect to "/admin/browse/content/" if r.URL.Path == "/admin/" { - http.Redirect(w, r, "/admin/browse/"+h.Config.Content+"/", http.StatusTemporaryRedirect) + http.Redirect(w, r, "/admin/browse/content/", http.StatusTemporaryRedirect) return 0, nil } diff --git a/package.json b/package.json index 98ba9378..37dcf852 100644 --- a/package.json +++ b/package.json @@ -4,15 +4,15 @@ "description": "Deploy your Hugo website and enjoy of an admin interface with Caddy server.", "repository": { "type": "git", - "url": "git+https://github.com/hacdias/caddy-cms.git" + "url": "git+https://github.com/hacdias/caddy-hugo.git" }, "main": "Gruntfile.js", "author": "Henrique Dias ", "license": "MIT", "bugs": { - "url": "https://github.com/hacdias/caddy-cms/issues" + "url": "https://github.com/hacdias/caddy-hugo/issues" }, - "homepage": "https://github.com/hacdias/caddy-cms#readme", + "homepage": "https://github.com/hacdias/caddy-hugo#readme", "scripts": { "install": "napa defunkt/jquery-pjax" }, diff --git a/utils/utils.go b/utils/utils.go index 0f6876a1..cff70398 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,19 +2,17 @@ package utils import ( "errors" - "fmt" "io" "log" "net/http" "os" - "os/exec" "reflect" "strings" "text/template" "unicode" - "github.com/hacdias/caddy-cms/assets" - "github.com/hacdias/caddy-cms/config" + "github.com/hacdias/caddy-hugo/assets" + "github.com/hacdias/caddy-hugo/config" "github.com/spf13/hugo/commands" ) @@ -174,23 +172,7 @@ func Run(c *config.Config) { log.Print("Can't get working directory.") } - if !c.Hugo { - out, err := exec.Command(c.Command).Output() - fmt.Print(string(out)) - if err != nil { - log.Panic("Can't execute the commands defined on Caddyfile.") - } - - return - } - - args := strings.Split(c.Command, " ") - - for index, element := range args { - args[index] = strings.Replace(element, "\"", "", -1) - } - - commands.HugoCmd.ParseFlags(args) + commands.HugoCmd.ParseFlags(c.Args) commands.HugoCmd.Run(commands.HugoCmd, make([]string, 0)) err = os.Chdir(cwd)