You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.0 KiB
137 lines
3.0 KiB
8 years ago
|
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>`
|