From 5fd7adec7a0e20d881725ac5ef8b2d5ee0b8a3cc Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 20 Feb 2016 12:04:12 +0000 Subject: [PATCH] close #47 --- browse/post.go | 57 ++++++++++++++++-------------------------------- config/config.go | 2 +- editor/post.go | 2 +- hugo.go | 4 ++-- utils/utils.go | 22 ++++++++++++++++++- 5 files changed, 44 insertions(+), 43 deletions(-) diff --git a/browse/post.go b/browse/post.go index 0c56cd7e..cbad12ba 100644 --- a/browse/post.go +++ b/browse/post.go @@ -49,51 +49,32 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) filename = strings.TrimSuffix(filename, "/") filename = c.Path + r.URL.Path + filename - // Check if the archetype is defined - if info["archetype"] != "" { - // Sanitize the archetype path + url := "/admin/edit/" + filename + + if strings.HasPrefix(filename, c.Path+"content/") && + (strings.HasSuffix(filename, ".md") || strings.HasSuffix(filename, ".markdown")) { + + filename = strings.Replace(filename, c.Path+"content/", "", 1) + args := []string{"new", filename} archetype := info["archetype"].(string) - archetype = strings.Replace(archetype, "/archetypes", "", 1) - archetype = strings.Replace(archetype, "archetypes", "", 1) - archetype = strings.TrimPrefix(archetype, "/") - archetype = strings.TrimSuffix(archetype, "/") - archetype = c.Path + "archetypes/" + archetype - // Check if the archetype ending with .markdown exists - if _, err := os.Stat(archetype + ".markdown"); err == nil { - err = utils.CopyFile(archetype+".markdown", filename) - if err != nil { - return http.StatusInternalServerError, err - } - - w.Header().Set("Location", "/admin/edit/"+filename) - w.Header().Set("Content-Type", "application/json") - w.Write([]byte("{}")) - return 201, nil + if archetype != "" { + args = append(args, "--kind", archetype) } - // Check if the archetype ending with .md exists - if _, err := os.Stat(archetype + ".md"); err == nil { - err = utils.CopyFile(archetype+".md", filename) - if err != nil { - return http.StatusInternalServerError, err - } - - w.Header().Set("Location", "/admin/edit/"+filename) - w.Header().Set("Content-Type", "application/json") - w.Write([]byte("{}")) - return 201, nil + if err := utils.RunCommand("hugo", args, c.Path); err != nil { + return http.StatusInternalServerError, err } + } else { + wf, err := os.Create(filename) + if err != nil { + return http.StatusInternalServerError, err + } + + defer wf.Close() } - wf, err := os.Create(filename) - if err != nil { - return http.StatusInternalServerError, err - } - - defer wf.Close() - - w.Header().Set("Location", "/admin/edit/"+filename) + w.Header().Set("Location", url) w.Header().Set("Content-Type", "application/json") w.Write([]byte("{}")) return http.StatusOK, nil diff --git a/config/config.go b/config/config.go index ba8a817c..bc177057 100644 --- a/config/config.go +++ b/config/config.go @@ -50,7 +50,7 @@ func ParseHugo(c *setup.Controller) (*Config, error) { value = c.Val() } - conf.Args = append(conf.Args, key, value) + conf.Args = append(conf.Args, key+"="+value) } } } diff --git a/editor/post.go b/editor/post.go index abba119d..0f6f1207 100644 --- a/editor/post.go +++ b/editor/post.go @@ -144,7 +144,7 @@ func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]int return } - utils.Run(c) + go utils.Run(c, false) }) scheduler.Start() } diff --git a/hugo.go b/hugo.go index c1736cc2..a82da5e5 100644 --- a/hugo.go +++ b/hugo.go @@ -54,7 +54,7 @@ func Setup(c *setup.Controller) (middleware.Middleware, error) { } // Generates the Hugo website for the first time the plugin is activated. - go utils.Run(config) + go utils.Run(config, true) return func(next middleware.Handler) middleware.Handler { return &CaddyHugo{Next: next, Config: config} @@ -140,7 +140,7 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error // Whenever the header "X-Regenerate" is true, the website should be // regenerated. Used in edit and settings, for example. if r.Header.Get("X-Regenerate") == "true" { - go utils.Run(h.Config) + go utils.Run(h.Config, false) } if err != nil { diff --git a/utils/utils.go b/utils/utils.go index 3f719bf2..4a8d9cb1 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -164,9 +164,20 @@ func ParseComponents(r *http.Request) []string { } // Run is used to run the static website generator -func Run(c *config.Config) { +func Run(c *config.Config, force bool) { os.RemoveAll(c.Path + "public") + // Prevent running if watching is enabled + if b, pos := stringInSlice("--watch", c.Args); b && !force { + if len(c.Args) > pos && c.Args[pos+1] != "false" { + return + } + + if len(c.Args) == pos+1 { + return + } + } + if err := RunCommand("hugo", c.Args, c.Path); err != nil { log.Panic(err) } @@ -181,6 +192,15 @@ func RunCommand(command string, args []string, path string) error { return cmd.Run() } +func stringInSlice(a string, list []string) (bool, int) { + for i, b := range list { + if b == a { + return true, i + } + } + return false, 0 +} + var splitCapitalizeExceptions = map[string]string{ "youtube": "YouTube", "github": "GitHub",