This commit is contained in:
Henrique Dias 2016-02-20 12:04:12 +00:00
parent 9e135967cb
commit 5fd7adec7a
5 changed files with 44 additions and 43 deletions

View File

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

View File

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

View File

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

View File

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

View File

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