close #47
parent
9e135967cb
commit
5fd7adec7a
|
@ -49,51 +49,32 @@ func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error)
|
||||||
filename = strings.TrimSuffix(filename, "/")
|
filename = strings.TrimSuffix(filename, "/")
|
||||||
filename = c.Path + r.URL.Path + filename
|
filename = c.Path + r.URL.Path + filename
|
||||||
|
|
||||||
// Check if the archetype is defined
|
url := "/admin/edit/" + filename
|
||||||
if info["archetype"] != "" {
|
|
||||||
// Sanitize the archetype path
|
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 := 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 archetype != "" {
|
||||||
if _, err := os.Stat(archetype + ".markdown"); err == nil {
|
args = append(args, "--kind", archetype)
|
||||||
err = utils.CopyFile(archetype+".markdown", filename)
|
}
|
||||||
if err != nil {
|
|
||||||
|
if err := utils.RunCommand("hugo", args, c.Path); err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
w.Header().Set("Location", "/admin/edit/"+filename)
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
w.Write([]byte("{}"))
|
|
||||||
return 201, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
wf, err := os.Create(filename)
|
wf, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer wf.Close()
|
defer wf.Close()
|
||||||
|
}
|
||||||
|
|
||||||
w.Header().Set("Location", "/admin/edit/"+filename)
|
w.Header().Set("Location", url)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write([]byte("{}"))
|
w.Write([]byte("{}"))
|
||||||
return http.StatusOK, nil
|
return http.StatusOK, nil
|
||||||
|
|
|
@ -50,7 +50,7 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
|
||||||
value = c.Val()
|
value = c.Val()
|
||||||
}
|
}
|
||||||
|
|
||||||
conf.Args = append(conf.Args, key, value)
|
conf.Args = append(conf.Args, key+"="+value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -144,7 +144,7 @@ func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]int
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
utils.Run(c)
|
go utils.Run(c, false)
|
||||||
})
|
})
|
||||||
scheduler.Start()
|
scheduler.Start()
|
||||||
}
|
}
|
||||||
|
|
4
hugo.go
4
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.
|
// 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 func(next middleware.Handler) middleware.Handler {
|
||||||
return &CaddyHugo{Next: next, Config: config}
|
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
|
// Whenever the header "X-Regenerate" is true, the website should be
|
||||||
// regenerated. Used in edit and settings, for example.
|
// regenerated. Used in edit and settings, for example.
|
||||||
if r.Header.Get("X-Regenerate") == "true" {
|
if r.Header.Get("X-Regenerate") == "true" {
|
||||||
go utils.Run(h.Config)
|
go utils.Run(h.Config, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -164,9 +164,20 @@ func ParseComponents(r *http.Request) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run is used to run the static website generator
|
// 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")
|
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 {
|
if err := RunCommand("hugo", c.Args, c.Path); err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
@ -181,6 +192,15 @@ func RunCommand(command string, args []string, path string) error {
|
||||||
return cmd.Run()
|
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{
|
var splitCapitalizeExceptions = map[string]string{
|
||||||
"youtube": "YouTube",
|
"youtube": "YouTube",
|
||||||
"github": "GitHub",
|
"github": "GitHub",
|
||||||
|
|
Loading…
Reference in New Issue