|
|
@ -311,25 +311,33 @@ func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, er |
|
|
|
ch.mtx.Lock() |
|
|
|
ch.mtx.Lock() |
|
|
|
defer ch.mtx.Unlock() |
|
|
|
defer ch.mtx.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
docref, ok := ch.docs[name] |
|
|
|
docref, ok := ch.docs[ch.docname(name)] |
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
return http.StatusNotFound, fmt.Errorf("draft not found") |
|
|
|
return http.StatusNotFound, fmt.Errorf("draft not found") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
http.StripPrefix( |
|
|
|
r.URL.Path = strings.ToLower(r.URL.Path) |
|
|
|
"/hugo/draft/"+encoded, |
|
|
|
|
|
|
|
http.FileServer(http.Dir(docref.tmpdir))).ServeHTTP(w, r) |
|
|
|
prefix := "/hugo/draft/" + encoded |
|
|
|
|
|
|
|
r.URL.Path = r.URL.Path[len(prefix):] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
http.FileServer(http.Dir(docref.tmpdir)).ServeHTTP(w, r) |
|
|
|
return 200, nil |
|
|
|
return 200, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) docname(orig string) string { |
|
|
|
|
|
|
|
return strings.ToLower(orig) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { |
|
|
|
func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { |
|
|
|
ch.mtx.Lock() |
|
|
|
ch.mtx.Lock() |
|
|
|
defer ch.mtx.Unlock() |
|
|
|
defer ch.mtx.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
name := r.URL.Path[len("/hugo/edit/"):] |
|
|
|
name := r.URL.Path[len("/hugo/edit/"):] |
|
|
|
name = filepath.Join(ch.Site.Root, name) |
|
|
|
name = filepath.Join(ch.Site.Root, name) |
|
|
|
|
|
|
|
name = strings.ToLower(name) |
|
|
|
|
|
|
|
|
|
|
|
_, ok := ch.docs[name] |
|
|
|
_, ok := ch.docs[ch.docname(name)] |
|
|
|
if !ok { |
|
|
|
if !ok { |
|
|
|
fmt.Println("opening", name) |
|
|
|
fmt.Println("opening", name) |
|
|
|
contents, err := ioutil.ReadFile(name) |
|
|
|
contents, err := ioutil.ReadFile(name) |
|
|
@ -352,7 +360,7 @@ func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ch.docs[name] = ref |
|
|
|
ch.docs[ch.docname(name)] = ref |
|
|
|
|
|
|
|
|
|
|
|
hugoCmd := exec.Command("hugo", "--watch", "-D", "-d", ref.tmpdir) |
|
|
|
hugoCmd := exec.Command("hugo", "--watch", "-D", "-d", ref.tmpdir) |
|
|
|
hugoCmd.Dir = ch.Site.Root |
|
|
|
hugoCmd.Dir = ch.Site.Root |
|
|
@ -397,7 +405,7 @@ func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { |
|
|
|
|
|
|
|
|
|
|
|
ref.doc.Close() |
|
|
|
ref.doc.Close() |
|
|
|
os.RemoveAll(tmpdir) |
|
|
|
os.RemoveAll(tmpdir) |
|
|
|
delete(ch.docs, name) |
|
|
|
delete(ch.docs, ch.docname(name)) |
|
|
|
ch.mtx.Unlock() |
|
|
|
ch.mtx.Unlock() |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
@ -409,7 +417,7 @@ func (ch *CaddyHugo) doc(r *http.Request) (*docref, error) { |
|
|
|
}() |
|
|
|
}() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return ch.docs[name], nil |
|
|
|
return ch.docs[ch.docname(name)], nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ch *CaddyHugo) Publish() error { |
|
|
|
func (ch *CaddyHugo) Publish() error { |
|
|
@ -504,17 +512,22 @@ func (ch CaddyHugo) NewContent(w http.ResponseWriter, r *http.Request) (int, err |
|
|
|
name += ".md" |
|
|
|
name += ".md" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
name = strings.ToLower(name) |
|
|
|
|
|
|
|
|
|
|
|
filename := path.Join(ctype, name) |
|
|
|
filename := path.Join(ctype, name) |
|
|
|
if ctype == "default" { |
|
|
|
if ctype == "default" { |
|
|
|
filename = name |
|
|
|
filename = name |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cmd := exec.Command("hugo", "new", filename) |
|
|
|
_, err := os.Stat(path.Join(ch.Site.Root, "content", filename)) |
|
|
|
cmd.Dir = ch.Site.Root |
|
|
|
if os.IsNotExist(err) { |
|
|
|
out, err := cmd.CombinedOutput() |
|
|
|
cmd := exec.Command("hugo", "new", filename) |
|
|
|
if err != nil { |
|
|
|
cmd.Dir = ch.Site.Root |
|
|
|
fmt.Println("error running hugo:\n", string(out)) |
|
|
|
out, err := cmd.CombinedOutput() |
|
|
|
return http.StatusInternalServerError, err |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
fmt.Println("error running hugo:\n", string(out)) |
|
|
|
|
|
|
|
return http.StatusInternalServerError, err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// serve redirect
|
|
|
|
// serve redirect
|
|
|
|