From 00e9e849eac7b56a2a749bedc91bbb80407cea07 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 16 Oct 2015 19:34:27 +0100 Subject: [PATCH 1/6] Remove non-hugo web gen support. See #27. Close #24 --- config/config.go | 36 +++++++++++++++--------------------- cms.go => hugo.go | 16 ++++++++-------- utils/utils.go | 20 +------------------- 3 files changed, 24 insertions(+), 48 deletions(-) rename cms.go => hugo.go (90%) 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/cms.go b/hugo.go similarity index 90% rename from cms.go rename to hugo.go index 72da5d1b..f05d6a18 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" @@ -22,21 +22,21 @@ import ( // 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/utils/utils.go b/utils/utils.go index 0f6876a1..8ae68b3a 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,12 +2,10 @@ package utils import ( "errors" - "fmt" "io" "log" "net/http" "os" - "os/exec" "reflect" "strings" "text/template" @@ -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) From 09f4d901420658bccc9abcd86d1693ab5c04c5df Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 16 Oct 2015 20:00:26 +0100 Subject: [PATCH 2/6] add docs --- docs.html | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 docs.html diff --git a/docs.html b/docs.html new file mode 100644 index 00000000..4c0e67e6 --- /dev/null +++ b/docs.html @@ -0,0 +1,69 @@ + + + + hugo - Caddy Directives + {{.Include "/includes/docs-head.html"}} + + +
+

hugo
Addon

+ +
+
Addon
+ This directive is a Caddy extension. Questions should be directed to its maintainer. github.com/hacdias/caddy-hugo +
+ +

+ hugo allows you to deploy and manage a static site with an web-based admin interface where you can edit content, schedule posts, upload files, etc. The admin interface will be available at /admin on your website. It is built to be used with Hugo - which is bundled with the plugin so you don't have to install it separately. +

+ +

+ Requirements: this add-on does not create a new Hugo site for you; you'll need to run hugo new site if you don't already have a Hugo site. +

+ +

Syntax

+ root public +hugo dir { + styles file + args cmd +} + +
    +
  • public is the folder with the public content.
  • +
  • dir is the folder where the commands are going to be executed. By default, it is the current working directory.
  • +
  • file is the relative path to public folder of the admin UI styles. These styles won't replace the defaults, they will be added.
  • +
  • cmd must be surrounded by double quotes and it corresponds to the Hugo flags you want to use.
  • +
+ +

+ All of the directives above are optional. +

+ +

+ To schedule a post, its date field must be in '2006-01-02 15:04:05-07:00' format. +

+ +

+ This directive should be used with root, basicauth and errors middleware to have the best experience. See the examples to know more. +

+ +

Examples

+ +

+ A simple configuration to use with Hugo static website generator: +

+ + localhost:8080 # website URL and port + +root public # the folder where Hugo generates the website +basicauth /admin user pass # protect the admin area using HTTP basic auth +hugo # enable the admin panel + +
+ © 2015 +
+
+ + {{.Include "/includes/docs-nav.html"}} + + From 109f27170a231d1570ad9f0b97fea8b409fa2f89 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 16 Oct 2015 20:03:59 +0100 Subject: [PATCH 3/6] fix #26 --- browse/browse.go | 3 ++- editor/editor.go | 3 ++- editor/post.go | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/browse/browse.go b/browse/browse.go index 8527d6c5..1463b22b 100644 --- a/browse/browse.go +++ b/browse/browse.go @@ -1,6 +1,7 @@ package browse import ( + "errors" "net/http" "strings" @@ -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/editor/editor.go b/editor/editor.go index 40ad2e61..03c958df 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -1,6 +1,7 @@ package editor import ( + "errors" "net/http" "strings" @@ -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/post.go b/editor/post.go index 83c0590c..5d8b2967 100644 --- a/editor/post.go +++ b/editor/post.go @@ -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) From ac69a3acbaa579f34e974dd1532d0c2a9bcb9ae3 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 17 Oct 2015 08:10:42 +0100 Subject: [PATCH 4/6] fix some things --- browse/post.go | 2 ++ docs.html | 69 -------------------------------------------------- editor/post.go | 2 +- 3 files changed, 3 insertions(+), 70 deletions(-) delete mode 100644 docs.html diff --git a/browse/post.go b/browse/post.go index b0f09c7f..6e3280d8 100644 --- a/browse/post.go +++ b/browse/post.go @@ -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/docs.html b/docs.html deleted file mode 100644 index 4c0e67e6..00000000 --- a/docs.html +++ /dev/null @@ -1,69 +0,0 @@ - - - - hugo - Caddy Directives - {{.Include "/includes/docs-head.html"}} - - -
-

hugo
Addon

- -
-
Addon
- This directive is a Caddy extension. Questions should be directed to its maintainer. github.com/hacdias/caddy-hugo -
- -

- hugo allows you to deploy and manage a static site with an web-based admin interface where you can edit content, schedule posts, upload files, etc. The admin interface will be available at /admin on your website. It is built to be used with Hugo - which is bundled with the plugin so you don't have to install it separately. -

- -

- Requirements: this add-on does not create a new Hugo site for you; you'll need to run hugo new site if you don't already have a Hugo site. -

- -

Syntax

- root public -hugo dir { - styles file - args cmd -} - -
    -
  • public is the folder with the public content.
  • -
  • dir is the folder where the commands are going to be executed. By default, it is the current working directory.
  • -
  • file is the relative path to public folder of the admin UI styles. These styles won't replace the defaults, they will be added.
  • -
  • cmd must be surrounded by double quotes and it corresponds to the Hugo flags you want to use.
  • -
- -

- All of the directives above are optional. -

- -

- To schedule a post, its date field must be in '2006-01-02 15:04:05-07:00' format. -

- -

- This directive should be used with root, basicauth and errors middleware to have the best experience. See the examples to know more. -

- -

Examples

- -

- A simple configuration to use with Hugo static website generator: -

- - localhost:8080 # website URL and port - -root public # the folder where Hugo generates the website -basicauth /admin user pass # protect the admin area using HTTP basic auth -hugo # enable the admin panel - -
- © 2015 -
-
- - {{.Include "/includes/docs-nav.html"}} - - diff --git a/editor/post.go b/editor/post.go index 5d8b2967..89384e3c 100644 --- a/editor/post.go +++ b/editor/post.go @@ -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 From e2922231656a8efbad7e15ab4fae0ef663bc3290 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 17 Oct 2015 08:12:33 +0100 Subject: [PATCH 5/6] update readme --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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```. From 213dcf179d0d8929cb6802cf7b3dd298a9aa1694 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sun, 18 Oct 2015 15:10:32 +0100 Subject: [PATCH 6/6] change 'caddy-cms' to 'caddy-hugo' --- browse/browse.go | 2 +- browse/delete.go | 2 +- browse/get.go | 4 ++-- browse/post.go | 4 ++-- editor/editor.go | 2 +- editor/get.go | 6 +++--- editor/post.go | 4 ++-- frontmatter/frontmatter.go | 2 +- hugo.go | 10 +++++----- package.json | 6 +++--- utils/utils.go | 4 ++-- 11 files changed, 23 insertions(+), 23 deletions(-) diff --git a/browse/browse.go b/browse/browse.go index 1463b22b..38cc31bd 100644 --- a/browse/browse.go +++ b/browse/browse.go @@ -5,7 +5,7 @@ import ( "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 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 6e3280d8..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 diff --git a/editor/editor.go b/editor/editor.go index 03c958df..97e6f61e 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -5,7 +5,7 @@ import ( "net/http" "strings" - "github.com/hacdias/caddy-cms/config" + "github.com/hacdias/caddy-hugo/config" ) // ServeHTTP serves the editor page 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 89384e3c..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" ) 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/hugo.go b/hugo.go index f05d6a18..e3d80291 100644 --- a/hugo.go +++ b/hugo.go @@ -11,11 +11,11 @@ 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" ) 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 8ae68b3a..cff70398 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -11,8 +11,8 @@ import ( "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" )