next commit

pull/8/head
Stephen Searles 8 years ago
parent b956647700
commit bf8eb53f7f
  1. 215
      caddyhugo.go
  2. 136
      templates.go
  3. 0
      testdir/testsite/content/.md

@ -4,13 +4,13 @@ import (
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/gorilla/websocket"
"github.com/mholt/caddy" "github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver" "github.com/mholt/caddy/caddyhttp/httpserver"
) )
@ -29,20 +29,14 @@ func init() {
type CaddyHugo struct { type CaddyHugo struct {
ServerType string ServerType string
Site *httpserver.SiteConfig
authorTmpl, adminTmpl, editTmpl *template.Template authorTmpl, adminTmpl, editTmpl *template.Template
} }
func (c CaddyHugo) Name() string {
if c.ServerType == "" {
return "hugo"
}
return c.ServerType
}
func (ch CaddyHugo) Setup(c *caddy.Controller) error { func (ch CaddyHugo) Setup(c *caddy.Controller) error {
site := httpserver.GetConfig(c) ch.Site = httpserver.GetConfig(c)
var err error var err error
@ -63,12 +57,12 @@ func (ch CaddyHugo) Setup(c *caddy.Controller) error {
// add a function that wraps listeners for the HTTP server // add a function that wraps listeners for the HTTP server
// (it's more common for a directive to call this rather than a standalone plugin) // (it's more common for a directive to call this rather than a standalone plugin)
site.AddMiddleware(ch.Middleware(c, site)) ch.Site.AddMiddleware(ch.Middleware(c))
return nil return nil
} }
func (ch CaddyHugo) Middleware(c *caddy.Controller, site *httpserver.SiteConfig) httpserver.Middleware { func (ch CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware {
return func(h httpserver.Handler) httpserver.Handler { return func(h httpserver.Handler) httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
if !ch.Match(r) { if !ch.Match(r) {
@ -80,13 +74,13 @@ func (ch CaddyHugo) Middleware(c *caddy.Controller, site *httpserver.SiteConfig)
} }
if strings.HasPrefix(r.URL.Path, "/hugo/admin") { if strings.HasPrefix(r.URL.Path, "/hugo/admin") {
return ch.Admin(site).ServeHTTP(w, r) return ch.Admin().ServeHTTP(w, r)
} }
if strings.HasPrefix(r.URL.Path, "/hugo/author") { if strings.HasPrefix(r.URL.Path, "/hugo/author") {
return ch.AuthorHome(site).ServeHTTP(w, r) return ch.AuthorHome().ServeHTTP(w, r)
} }
if strings.HasPrefix(r.URL.Path, "/hugo/edit/") { if strings.HasPrefix(r.URL.Path, "/hugo/edit/") {
return ch.Edit(c, site).ServeHTTP(w, r) return ch.Edit(c).ServeHTTP(w, r)
} }
return http.StatusNotFound, errors.New("not found") return http.StatusNotFound, errors.New("not found")
@ -110,9 +104,9 @@ func (ch CaddyHugo) BasePath() string {
return "/hugo" return "/hugo"
} }
func (ch CaddyHugo) Admin(site *httpserver.SiteConfig) httpserver.Handler { func (ch CaddyHugo) Admin() httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
err := ch.adminTmpl.Execute(w, ch.TmplData(r, site)) err := ch.adminTmpl.Execute(w, ch.TmplData(r))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
@ -123,9 +117,9 @@ func (ch CaddyHugo) Admin(site *httpserver.SiteConfig) httpserver.Handler {
}) })
} }
func (ch CaddyHugo) AuthorHome(site *httpserver.SiteConfig) httpserver.Handler { func (ch CaddyHugo) AuthorHome() httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
err := ch.authorTmpl.Execute(w, ch.TmplData(r, site)) err := ch.authorTmpl.Execute(w, ch.TmplData(r))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusInternalServerError, err return http.StatusInternalServerError, err
@ -135,48 +129,16 @@ func (ch CaddyHugo) AuthorHome(site *httpserver.SiteConfig) httpserver.Handler {
}) })
} }
func (ch CaddyHugo) Edit(c *caddy.Controller, site *httpserver.SiteConfig) httpserver.Handler { func (ch CaddyHugo) Edit(c *caddy.Controller) httpserver.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
if r.URL.Path == "/hugo/edit/new" { if r.URL.Path == "/hugo/edit/new" {
return ch.NewContent(w, r)
name := r.FormValue("name") }
ctype := r.FormValue("type") if r.URL.Path == "/hugo/edit/websocket" {
return ch.DeltaWebsocket(w, r)
if filepath.Ext(name) != ".md" {
name += ".md"
}
filename := path.Join(site.Root, "content", ctype, name)
dir := filepath.Dir(filename)
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
fmt.Println(err)
return http.StatusInternalServerError, err
}
}
// create content
f, err := os.Create(filename)
if err != nil && !os.IsExist(err) {
fmt.Println(err)
return http.StatusInternalServerError, err
}
// we only needed to make the file though
err = f.Close()
if err != nil {
fmt.Println(err)
return http.StatusInternalServerError, err
}
// serve redirect
http.Redirect(w, r, filepath.Join("/hugo/edit/", filename), http.StatusFound)
return http.StatusFound, nil
} }
err := ch.editTmpl.Execute(w, ch.TmplData(r, site)) err := ch.editTmpl.Execute(w, ch.TmplData(r))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -187,121 +149,66 @@ func (ch CaddyHugo) Edit(c *caddy.Controller, site *httpserver.SiteConfig) https
}) })
} }
func (t tmplData) Content() ([]string, error) { func (ch CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int, error) {
var files []string var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
err := filepath.Walk(path.Join(t.Site.Root, "content"), func(name string, fi os.FileInfo, err error) error { WriteBufferSize: 1024,
if fi.IsDir() { }
return nil
}
files = append(files, name)
return nil
})
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return nil, err return http.StatusBadRequest, err
} }
return files, nil for {
} messageType, p, err := conn.ReadMessage()
if err != nil {
return http.StatusBadRequest, err
}
func (ch CaddyHugo) TmplData(r *http.Request, site *httpserver.SiteConfig) interface{} { fmt.Println(messageType, string(p))
return tmplData{site, r, ch} }
} }
func (t tmplData) ContentTypes() ([]string, error) { func (ch CaddyHugo) NewContent(w http.ResponseWriter, r *http.Request) (int, error) {
layoutDir, err := os.Open(path.Join(t.Site.Root, "archetypes")) name := r.FormValue("name")
if err != nil { ctype := r.FormValue("type")
fmt.Println("opening layout dir", err)
return nil, err
}
defer layoutDir.Close()
names, err := layoutDir.Readdirnames(0) if filepath.Ext(name) != ".md" {
if err != nil { name += ".md"
fmt.Println("reading dir", err)
return nil, err
} }
out := []string{"default"} filename := path.Join(ch.Site.Root, "content", ctype, name)
for _, name := range names {
out = append(out, name[:len(name)-len(filepath.Ext(name))])
}
return out, nil dir := filepath.Dir(filename)
} if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
err = os.MkdirAll(dir, 0755)
if err != nil {
fmt.Println(err)
return http.StatusInternalServerError, err
}
}
type tmplData struct { // create content
Site *httpserver.SiteConfig f, err := os.Create(filename)
R *http.Request if err != nil && !os.IsExist(err) {
CaddyHugo fmt.Println(err)
} return http.StatusInternalServerError, err
}
func (t tmplData) LoadContent() (string, error) { // we only needed to make the file though
path := t.R.URL.Path[len("/hugo/edit/"):] err = f.Close()
f, err := os.Open(path)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return "", err return http.StatusInternalServerError, err
} }
out, err := ioutil.ReadAll(f)
return string(out), err // serve redirect
http.Redirect(w, r, filepath.Join("/hugo/edit/", filename), http.StatusFound)
return http.StatusFound, nil
} }
var EditPage = `<html> func (ch CaddyHugo) TmplData(r *http.Request) interface{} {
<head> return tmplData{ch.Site, r, ch}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-settings_menu.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-keybinding_menu.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/keybinding-vim.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/mode-markdown.js"></script>
<style type="text/css" media="screen">
#editor {
position: absolute;
top: 50px;
right: 0;
bottom: 0;
left: 0;
} }
</style>
<body>
<div id="editor">{{ .LoadContent }}</div>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
var session = editor.getSession();
session.setMode("ace/mode/markdown");
session.on("change", function (delta) {
console.log(delta);
})
</script>
</body>
</html>`
var AdminPage = `<html><body>not implemented</body></html>`
var AuthorPage = `<html>
<head>
</head>
<body>
<p>Create content:</p>
<form action="/hugo/edit/new" method="POST">
<label>Name: <input type="text" name="name" /></label>
<select name="type">
{{- range .ContentTypes }}
<option value="{{ . }}">{{ . }}</option>
{{- end }}
</select>
<input type="submit" />
</form>
<p>Edit content:</p>
<ul>{{ range .Content }}
<li><a href="/hugo/edit/{{ . }}">{{ . }}</a></li>
{{- end }}
</ul>
</body>
</html>`

@ -0,0 +1,136 @@
package caddyhugo
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
func (t tmplData) Content() ([]string, error) {
var files []string
err := filepath.Walk(path.Join(t.Site.Root, "content"), func(name string, fi os.FileInfo, err error) error {
if fi.IsDir() {
return nil
}
files = append(files, name)
return nil
})
if err != nil {
fmt.Println(err)
return nil, err
}
return files, nil
}
func (t tmplData) ContentTypes() ([]string, error) {
layoutDir, err := os.Open(path.Join(t.Site.Root, "archetypes"))
if err != nil {
fmt.Println("opening layout dir", err)
return nil, err
}
defer layoutDir.Close()
names, err := layoutDir.Readdirnames(0)
if err != nil {
fmt.Println("reading dir", err)
return nil, err
}
out := []string{"default"}
for _, name := range names {
out = append(out, name[:len(name)-len(filepath.Ext(name))])
}
return out, nil
}
type tmplData struct {
Site *httpserver.SiteConfig
R *http.Request
CaddyHugo
}
func (t tmplData) LoadContent() (string, error) {
path := t.R.URL.Path[len("/hugo/edit/"):]
f, err := os.Open(path)
if err != nil {
fmt.Println(err)
return "", err
}
out, err := ioutil.ReadAll(f)
return string(out), err
}
var EditPage = `<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-settings_menu.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-keybinding_menu.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/keybinding-vim.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/mode-markdown.js"></script>
<style type="text/css" media="screen">
#editor {
position: absolute;
top: 50px;
right: 0;
bottom: 0;
left: 0;
}
</style>
<body>
<div id="editor">{{ .LoadContent }}</div>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
var session = editor.getSession();
session.setMode("ace/mode/markdown");
// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:8080/hugo/edit/websocket');
// Listen for messages
socket.addEventListener('message', function (event) {
console.log('Message from server', event.data);
});
session.on("change", function (delta) {
socket.send(JSON.stringify(delta))
})
</script>
</body>
</html>`
var AdminPage = `<html><body>not implemented</body></html>`
var AuthorPage = `<html>
<head>
</head>
<body>
<p>Create content:</p>
<form action="/hugo/edit/new" method="POST">
<label>Name: <input type="text" name="name" /></label>
<select name="type">
{{- range .ContentTypes }}
<option value="{{ . }}">{{ . }}</option>
{{- end }}
</select>
<input type="submit" />
</form>
<p>Edit content:</p>
<ul>{{ range .Content }}
<li><a href="/hugo/edit/{{ . }}">{{ . }}</a></li>
{{- end }}
</ul>
</body>
</html>`
Loading…
Cancel
Save