Merge pull request #29 from hacdias/development

Development
pull/31/head
Henrique Dias 2015-10-18 15:55:50 +01:00
commit ba966e725c
13 changed files with 64 additions and 84 deletions

View File

@ -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```.

View File

@ -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.")
}
}

View File

@ -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

View File

@ -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"
)

View File

@ -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()
}
}

View File

@ -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
}
}
}

View File

@ -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.")
}
}

View File

@ -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"
)

View File

@ -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

View File

@ -6,7 +6,7 @@ import (
"sort"
"strings"
"github.com/hacdias/caddy-cms/utils"
"github.com/hacdias/caddy-hugo/utils"
"github.com/spf13/hugo/parser"
)

View File

@ -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
}

View File

@ -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 <hacdias@gmail.com>",
"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"
},

View File

@ -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)