Compare commits

...

4 Commits

  1. 94
      caddyhugo.go
  2. 1
      content.go
  3. 12
      deltas.go
  4. 30
      frontend/templates.go
  5. 1
      frontend/templates/admin.html.tmpl
  6. 44
      frontend/templates/author.html.tmpl
  7. 316
      frontend/templates/edit.html.tmpl
  8. 70
      frontend/templates/upload.html.tmpl
  9. 134
      go.mod
  10. 1096
      go.sum
  11. 103
      http.go
  12. 8
      makefile
  13. 33
      media.go
  14. 22
      setup.go
  15. 454
      templates.go
  16. 17
      testdir/Caddyfile
  17. 14
      testdir/caddyfile
  18. 1
      testdir/testsite/config.toml
  19. 0
      testdir/testsite/content/.gitkeep
  20. 3
      tools.go

@ -1,8 +1,10 @@
package caddyhugo package caddyhugo
import ( import (
"fmt"
"html/template" "html/template"
"net/http" "net/http"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
@ -10,25 +12,81 @@ import (
"git.stephensearles.com/stephen/acedoc" "git.stephensearles.com/stephen/acedoc"
"git.stephensearles.com/stephen/caddy-hugo2/comments" "git.stephensearles.com/stephen/caddy-hugo2/comments"
"git.stephensearles.com/stephen/caddy-hugo2/media" "git.stephensearles.com/stephen/caddy-hugo2/media"
"go.uber.org/zap"
"github.com/caddyserver/caddy" "github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/caddyhttp/httpserver" "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/gohugoio/hugo/deps" "github.com/gohugoio/hugo/deps"
"github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/hugolib"
"github.com/spf13/afero" "github.com/spf13/afero"
) )
func init() { func init() {
caddy.RegisterPlugin("hugo", caddy.Plugin{ caddy.RegisterModule(&CaddyHugo{})
ServerType: "http", httpcaddyfile.RegisterHandlerDirective("hugo", parseCaddyfile)
Action: SetupCaddy, }
type SiteConfig struct {
Hosts []string
Root string
CommentsEnabled bool
CommentsPassword string
}
func (m *CaddyHugo) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.hugo",
New: func() caddy.Module { return new(CaddyHugo) },
}
}
func (m *CaddyHugo) Provision(ctx caddy.Context) error {
m.logger = ctx.Logger(m)
if m.Site.CommentsEnabled {
m.Comments = comments.WithStorage(comments.NewDiskv(path.Join(m.Site.Root, "comments")))
}
// TODO: not sure where m.Site is getting populated.
// m.Site.Root looks to be the working directory of caddy but it needs to be the directory of the site
root, err := filepath.Abs(m.Site.Root)
if err != nil {
return err
}
err = m.Setup(root)
if err != nil {
return err
}
ctx.OnCancel(func() {
m.persistAllEdits()
}) })
m.commentsSetting()
return nil
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (s *SiteConfig) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.NextBlock(0) {
if d.Val() == "comments" {
if d.NextArg() {
s.CommentsPassword = d.Val()
}
}
}
return nil
} }
// CaddyHugo implements the plugin // CaddyHugo implements the plugin for a single site
type CaddyHugo struct { type CaddyHugo struct {
logger *zap.Logger
ServerType string ServerType string
Site *httpserver.SiteConfig Site SiteConfig
HugoSites *hugolib.HugoSites HugoSites *hugolib.HugoSites
HugoCfg *deps.DepsCfg HugoCfg *deps.DepsCfg
@ -46,6 +104,17 @@ type CaddyHugo struct {
confirmingToClient map[uint64]struct{} confirmingToClient map[uint64]struct{}
} }
func (ch *CaddyHugo) log(msg string, args ...interface{}) {
all := make([]any, len(args)+1)
all[0] = msg
copy(all[1:], args)
ch.logger.Info(fmt.Sprint(all...))
}
func (ch *CaddyHugo) logf(msg string, args ...interface{}) {
ch.logger.Info(fmt.Sprintf(msg, args...))
}
// Build rebuilds the cached state of the site. TODO: determine if this republishes // Build rebuilds the cached state of the site. TODO: determine if this republishes
func (ch *CaddyHugo) Build() error { func (ch *CaddyHugo) Build() error {
return buildSite(ch.HugoSites) return buildSite(ch.HugoSites)
@ -78,3 +147,14 @@ func (ch *CaddyHugo) TmplData(r *http.Request, docref *editSession) interface{}
} }
return &tmplData{ch.Site, r, ch, doc, docref} return &tmplData{ch.Site, r, ch, doc, docref}
} }
// parseCaddyfile unmarshals tokens from h into a new Middleware.
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m CaddyHugo
err := m.Site.UnmarshalCaddyfile(h.Dispenser)
return &m, err
}
var (
_ caddy.Provisioner = (*CaddyHugo)(nil)
)

@ -98,6 +98,7 @@ func (ch *CaddyHugo) NewContent(name, ctype string) (string, error) {
if os.IsNotExist(err) { if os.IsNotExist(err) {
cmd := exec.Command("hugo", "new", filename) cmd := exec.Command("hugo", "new", filename)
cmd.Dir = ch.Dir cmd.Dir = ch.Dir
ch.logf("running `hugo new` in %v", ch.Dir)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return filename, fmt.Errorf("error running 'hugo new': %v; %v", err, string(out)) return filename, fmt.Errorf("error running 'hugo new': %v; %v", err, string(out))

@ -90,7 +90,7 @@ func (ch *CaddyHugo) ClearConfirmed(lowestPending uint64) {
} }
} }
func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) error {
var upgrader = websocket.Upgrader{ var upgrader = websocket.Upgrader{
ReadBufferSize: 1024, ReadBufferSize: 1024,
WriteBufferSize: 1024, WriteBufferSize: 1024,
@ -99,7 +99,7 @@ func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int
conn, err := upgrader.Upgrade(w, r, nil) conn, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusBadRequest, err return err
} }
conn.SetReadDeadline(time.Time{}) conn.SetReadDeadline(time.Time{})
@ -107,7 +107,7 @@ func (ch *CaddyHugo) DeltaWebsocket(w http.ResponseWriter, r *http.Request) (int
doc, err := ch.editSession(docNameFromEditRequest(r)) doc, err := ch.editSession(docNameFromEditRequest(r))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusBadRequest, err return err
} }
return ch.handleDeltaConn(conn, doc) return ch.handleDeltaConn(conn, doc)
@ -122,7 +122,7 @@ func (ch *CaddyHugo) Message(deltas ...acedoc.Delta) Message {
} }
} }
func (ch *CaddyHugo) handleDeltaConn(conn DeltaConn, doc *editSession) (int, error) { func (ch *CaddyHugo) handleDeltaConn(conn DeltaConn, doc *editSession) error {
const idlePing = 15 * time.Second const idlePing = 15 * time.Second
const idlePingShort = 1 * time.Millisecond const idlePingShort = 1 * time.Millisecond
@ -210,7 +210,7 @@ func (ch *CaddyHugo) handleDeltaConn(conn DeltaConn, doc *editSession) (int, err
select { select {
case err := <-errCh: case err := <-errCh:
fmt.Println("error handling websocket connection:", err) fmt.Println("error handling websocket connection:", err)
return 500, err return err
default: default:
} }
@ -223,7 +223,7 @@ func (ch *CaddyHugo) handleDeltaConn(conn DeltaConn, doc *editSession) (int, err
case <-wroteMessagesCh: case <-wroteMessagesCh:
resetTimer(idlePing) resetTimer(idlePing)
case <-doneCh: case <-doneCh:
return 200, nil return nil
} }
} }
} }

@ -0,0 +1,30 @@
package frontend
import "embed"
//go:embed templates
var templates embed.FS
func readFilename(filename string) string {
f, err := templates.ReadFile(filename)
if err != nil {
panic(err)
}
return string(f)
}
func EditPage() string {
return readFilename("templates/edit.html.tmpl")
}
func AdminPage() string {
return readFilename("templates/admin.html.tmpl")
}
func AuthorPage() string {
return readFilename("templates/author.html.tmpl")
}
func UploadPage() string {
return readFilename("templates/upload.html.tmpl")
}

@ -0,0 +1 @@
<html><body>not implemented</body></html>

@ -0,0 +1,44 @@
<html>
<head>
</head>
<body>
{{ $timeFormat := "Jan _2 15:04:05" }}
<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>
<table>{{ range .Content }}
<tr>
{{ if .Metadata }}
<td>
<a href="/hugo/edit/{{ .Filename }}">
{{ .Metadata.Title }}
</a>
</td>
<td>
{{ .Metadata.Date.Format $timeFormat }}
{{ if not (.Metadata.Lastmod.Equal .Metadata.Date) }}
(last modified {{.Metadata.Lastmod.Format $timeFormat }})
{{end}}
</td>
{{ else }}
<td>
<a href="/hugo/edit/{{ .Filename }}">
{{ .Filename }}
</a>
</td>
<td>(unable to load metadata)</td>
{{ end }}
</tr>
{{- end }}
</table>
</body>
</html>

@ -0,0 +1,316 @@
<html>
<head>
<script src="/hugo/simplemde.js"></script>
<style type="text/css" media="screen">
#editor-wrapper {
position: absolute;
top: 50px;
right: 0;
bottom: 150px;
left: 40%;
}
#draft {
position: absolute;
top: 50px;
right: 60%;
bottom: 0;
left: 0;
}
#draft > iframe {
height: 100%;
width: 100%;
border: none;
}
a {
text-decoration: underline;
text-decoration-style: dotted;
cursor: pointer;
}
</style>
<link rel="stylesheet" href="/hugo/simplemde.css" />
<script src="/hugo/vue.js"></script>
<script src="/hugo/moment.js"></script>
<body>
<div id="container" >
<div id="header">
<div id="lastSaved">
<span v-if="sendQueue.length > 0 || Object.keys(needConfirmation).length > 0">last saved ${ lastSaved.from(now) }, saving</span>
<span v-else>saved</span>
<span v-if="connectionError">, ${connectionError}</span>
</div>
<div>
<a id="sideview-toggle-media">media</a>
<a id="sideview-toggle-draft">draft</a>
</div>
</div>
<div id="editor-wrapper">
<textarea id="editor">{{ .LoadContent }}</textarea>
</div>
<div id="draft"><iframe src="{{ .IframeSource }}">Loading draft...</iframe></div>
</div>
<script>
var iframe = document.querySelector("#draft > iframe");
document.onclick = function (event) {
var iframe = document.querySelector("#draft > iframe");
switch (event.target.id) {
case "sideview-toggle-media":
iframe.src = "/hugo/media";
break;
case "sideview-toggle-draft":
iframe.src = "{{ .IframeSource }}";
break;
}
}
var uiBindings = {
ltime: {{ .LTime }},
serverLtime: 0,
lastSaved: moment(),
now: moment(),
connectionError: null,
sendQueue: [],
sentRecently: [],
needConfirmation: {},
};
var app = new Vue({
el: "#container",
data: uiBindings,
delimiters: ["${", "}"],
});
function getLtime() {
uiBindings.ltime++
return uiBindings.ltime
}
function observeServer(l, confirmed) {
uiBindings.serverLtime = l;
if (confirmed && confirmed.length > 0) {
confirmed.forEach(function (e) {
delete uiBindings.needConfirmation[e];
})
}
observe(l);
}
function observe(l) {
if (l > uiBindings.ltime) {
uiBindings.now = moment();
uiBindings.lastSaved = moment();
uiBindings.ltime = l;
}
}
var selectedImage;
var editorElem = document.getElementById("editor");
var editor = new SimpleMDE({
element: editorElem,
forceSync: true,
insertTexts: {
image: ["{\{% thumb filename=\"", "#url#\" width=\"200\" %}}"]
},
imageURLFn: function () {
return selectedImage;
}
});
window.onmessage = function (evt) {
selectedImage = evt.data;
}
// Create WebSocket connection.
var socket = connect();
const sawChangesBumpsTo = 10;
var sentinelSrc = 'about:blank';
var oldSrc = '';
var sawChanges = -1;
window.setInterval(function () {
if (sawChanges >= 0) {
sawChanges--;
if (sawChanges == 0) {
if (iframe.contentWindow) {
iframe.contentWindow.location.reload();
}
}
}
uiBindings.now = moment();
if (uiBindings.connectionError) {
socket = connect();
} else if (uiBindings.sendQueue.length > 0) {
var ltime = getLtime();
// record lowest pending
// ltime at the time this message
// was serialized
var lowestPending = ltime;
for (c in uiBindings.needConfirmation) {
c = parseInt(c, 10);
if (lowestPending === 0 || c < lowestPending) {
lowestPending = c;
}
}
var msg = JSON.stringify({
"deltas": uiBindings.sendQueue,
"ltime": ltime,
"lowestPending": lowestPending,
});
uiBindings.sendQueue = [];
uiBindings.needConfirmation[ltime] = msg;
}
for (ltime in uiBindings.needConfirmation) {
var msg = uiBindings.needConfirmation[ltime];
socket.send(msg);
}
}, 500);
function connect() {
const socket = new WebSocket((location.protocol == "https:" ? 'wss://' : 'ws://') + location.host + location.pathname);
// Listen for messages
socket.addEventListener('message', function (event) {
var message = JSON.parse(event.data);
observeServer(message.ltime, message.confirmed);
var deltas = [];
deltas.push.apply(deltas, message.deltas);
deltas.forEach(function(aceDelta) {
var cmDelta = aceDeltaToCM(aceDelta)
var content = ""
var to = {
line: aceDelta.start.row,
ch: aceDelta.start.column,
}
if (aceDelta.action == "insert") {
content = aceDelta.lines.join("\n");
to = null;
}
editor.codemirror.doc.replaceRange(content, cmDelta.from, to, "dontreflect");
sawChanges = sawChangesBumpsTo;
})
});
socket.addEventListener('open', function () {
uiBindings.connectionError = null;
});
socket.addEventListener('close', function () {
if (!uiBindings.connectionError) {
getLtime();
uiBindings.connectionError = "server connection closed, reconnecting...";
}
});
socket.addEventListener('error', function (err) {
if (!uiBindings.connectionError) {
uiBindings.connectionError = err;
getLtime();
}
console.log(err);
});
return socket;
}
editor.codemirror.on("change", function (cm, cmDelta) {
if (cmDelta.origin == "dontreflect") {
return;
}
var aceDelta = cmDeltaToAce(cmDelta);
console.log(cmDelta, "=>", aceDelta)
sawChanges = sawChangesBumpsTo;
uiBindings.sendQueue.push.apply(uiBindings.sendQueue, aceDelta);
})
function cmDeltaToAce(cmDelta) {
var isRemove = (cmDelta.removed.length > 0 && cmDelta.removed[0].length > 0) || cmDelta.removed.length > 1;
var lines = isRemove ? cmDelta.removed : cmDelta.text;
var aceDelta = {
action: isRemove ? "remove" : "insert",
lines: lines,
start: {
row: cmDelta.from.line,
column: cmDelta.from.ch,
},
end: {
row: cmDelta.from.line + (isRemove ? lines.length - 1 : lines.length - 1 ),
column: lines[lines.length-1].length,
}
};
if (aceDelta.start.row == aceDelta.end.row) {
aceDelta.end.column += cmDelta.from.ch;
}
if (false && isRemove && aceDelta.start.row == aceDelta.end.row) {
var origStart = aceDelta.start;
aceDelta.start = aceDelta.end;
aceDelta.end = origStart;
aceDelta.start.column += cmDelta.from.ch;
}
if (isRemove && ((cmDelta.text.length > 0 && cmDelta.text[0].length > 0) || cmDelta.text.length > 1)) {
cmDelta.removed = [""];
var ret = [aceDelta];
ret.push.apply(ret, cmDeltaToAce(cmDelta));
return ret;
}
return [aceDelta];
}
function aceDeltaToCM(aceDelta) {
var cmDelta = {
text: [],
removed: [],
from: {
line: aceDelta.start.row,
ch: aceDelta.start.column,
},
to: {
// cm deltas are weird. to refers to the selection end, which
// with a simple blinking cursor with no selection, is always
// the same as from
line: aceDelta.start.row,
ch: aceDelta.start.column,
},
}
if (aceDelta.action == "remove") {
var origStart = aceDelta.start;
aceDelta.start = aceDelta.end;
aceDelta.end = origStart;
cmDelta.removed = aceDelta.lines
cmDelta.text = [""]
} else {
cmDelta.text = aceDelta.lines
cmDelta.removed = [""]
}
return cmDelta;
}
</script>
</body>
</html>

@ -0,0 +1,70 @@
<input type="file" style="display: hidden;" id="{{.ElemName}}" />
<div id="{{.ElemName}}_dropzone" ondrop="dropHandler(event);" ondragover="draghandler(event);" ondragenter="draghandler(event);" ondragleave="draghandler(event);" style="background-color: rgba(0,0,0,0.5); visibility: hidden; opacity:0; position: fixed; top: 0; bottom: 0; left: 0; right: 0; width: 100%; height: 100%; ; transition: visibility 175ms, opacity 175ms; z-index: 9999999;"></div>
<script>
document.addEventListener("DOMContentLoaded", function () {
var fileInput = document.getElementById('{{.ElemName}}');
var dropzone = document.getElementById('{{.ElemName}}_dropzone');
fileInput.onchange = function () {
var formData = new FormData();
fileInput.files.forEach(function (file) {
formData.append(file.name, file);
});
upload(formData);
}
var lastTarget = null;
window.addEventListener("dragenter", function(e)
{
lastTarget = e.target; // cache the last target here
// unhide our dropzone overlay
dropzone.style.visibility = "";
dropzone.style.opacity = 1;
});
window.addEventListener("dragleave", function(e)
{
// this is the magic part. when leaving the window,
// e.target happens to be exactly what we want: what we cached
// at the start, the dropzone we dragged into.
// so..if dragleave target matches our cache, we hide the dropzone.
if(e.target === lastTarget)
{
dropzone.style.visibility = "hidden";
dropzone.style.opacity = 0;
}
});
});
function draghandler(evt) {
evt.preventDefault();
}
function dropHandler(evt) {
evt.preventDefault();
var files = evt.dataTransfer.files;
var formData = new FormData();
for (var i = 0; i < files.length; i++) {
formData.append(files[i].name, files[i]);
}
upload(formData);
return false;
}
function upload(formData) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(e) {
if ( 4 == this.readyState ) {
window.location.reload(true);
}
}
xhr.open('POST', '/hugo/upload');
xhr.send(formData);
}
</script>

134
go.mod

@ -1,18 +1,142 @@
module git.stephensearles.com/stephen/caddy-hugo2 module git.stephensearles.com/stephen/caddy-hugo2
go 1.13 go 1.18
require ( require (
git.stephensearles.com/stephen/acedoc v0.0.0-20170928122432-96da2793a59d git.stephensearles.com/stephen/acedoc v0.0.0-20170928122432-96da2793a59d
git.stephensearles.com/stephen/idleshut v0.0.0-20180107224249-cde7779f51c8 git.stephensearles.com/stephen/idleshut v0.0.0-20180107224249-cde7779f51c8
github.com/PuerkitoBio/goquery v1.5.0 github.com/PuerkitoBio/goquery v1.5.0
github.com/caddyserver/caddy v1.0.3 github.com/caddyserver/caddy/v2 v2.1.1
github.com/gobuffalo/envy v1.7.0 // indirect github.com/caddyserver/xcaddy v0.3.0
github.com/gohugoio/hugo v0.58.1 github.com/gohugoio/hugo v0.74.4-0.20200822075643-d39636a5fc6b
github.com/gorilla/websocket v1.4.1 github.com/gorilla/websocket v1.4.1
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/peterbourgon/diskv v2.0.1+incompatible github.com/peterbourgon/diskv v2.0.1+incompatible
github.com/spf13/afero v1.2.2 github.com/spf13/afero v1.2.2
github.com/spf13/viper v1.4.0 github.com/spf13/viper v1.6.1
github.com/tajtiattila/metadata v0.0.0-20180130123038-1ef25f4c37ea github.com/tajtiattila/metadata v0.0.0-20180130123038-1ef25f4c37ea
go.uber.org/zap v1.15.0
)
require (
cloud.google.com/go v0.54.0 // indirect
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9 // indirect
github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69 // indirect
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/DataDog/zstd v1.4.1 // indirect
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.1.0 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/alecthomas/chroma v0.8.0 // indirect
github.com/andybalholm/cascadia v1.0.0 // indirect
github.com/antlr/antlr4 v0.0.0-20200503195918-621b933c7a7f // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/bep/debounce v1.2.0 // indirect
github.com/bep/gitmap v1.1.2 // indirect
github.com/bep/golibsass v0.6.0 // indirect
github.com/bep/tmc v0.5.1 // indirect
github.com/caddyserver/certmagic v0.11.2 // indirect
github.com/cenkalti/backoff/v4 v4.0.0 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cheekybits/genny v1.0.0 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect
github.com/dgraph-io/badger v1.5.3 // indirect
github.com/dgraph-io/badger/v2 v2.0.1-rc1.0.20200413122845-09dd2e1a4195 // indirect
github.com/dgraph-io/ristretto v0.0.2-0.20200115201040-8f368f2f2ab3 // indirect
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 // indirect
github.com/disintegration/gift v1.2.1 // indirect
github.com/dlclark/regexp2 v1.2.0 // indirect
github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac // indirect
github.com/evanw/esbuild v0.6.5 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/getkin/kin-openapi v0.14.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-acme/lego/v3 v3.7.0 // indirect
github.com/go-sql-driver/mysql v1.5.0 // indirect
github.com/gobuffalo/envy v1.7.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/google/cel-go v0.5.1 // indirect
github.com/google/go-cmp v0.5.0 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.1 // indirect
github.com/imdario/mergo v0.3.8 // indirect
github.com/jdkato/prose v1.1.1 // indirect
github.com/joho/godotenv v1.3.0 // indirect
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a // indirect
github.com/klauspost/cpuid v1.3.0 // indirect
github.com/kyokomi/emoji v2.2.1+incompatible // indirect
github.com/libdns/libdns v0.0.0-20200501023120-186724ffc821 // indirect
github.com/lucas-clemente/quic-go v0.18.0 // indirect
github.com/lunixbochs/vtclean v1.0.0 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/manifoldco/promptui v0.3.1 // indirect
github.com/markbates/inflect v1.0.0 // indirect
github.com/marten-seemann/qpack v0.2.0 // indirect
github.com/marten-seemann/qtls v0.10.0 // indirect
github.com/marten-seemann/qtls-go1-15 v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-runewidth v0.0.7 // indirect
github.com/miekg/dns v1.1.30 // indirect
github.com/miekg/mmark v1.3.6 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/hashstructure v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/muesli/smartcrop v0.3.0 // indirect
github.com/nicksnyder/go-i18n v1.10.0 // indirect
github.com/niklasfasching/go-org v1.3.1 // indirect
github.com/olekukonko/tablewriter v0.0.4 // indirect
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.5.1 // indirect
github.com/russross/blackfriday v1.5.3-0.20200218234912-41c5fccfd6f6 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd // indirect
github.com/samfoo/ansi v0.0.0-20160124022901-b6bd2ded7189 // indirect
github.com/sanity-io/litter v1.2.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/smallstep/certificates v0.15.0-rc.1.0.20200506212953-e855707dc274 // indirect
github.com/smallstep/cli v0.14.4 // indirect
github.com/smallstep/nosql v0.3.0 // indirect
github.com/smallstep/truststore v0.9.5 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tdewolff/minify/v2 v2.6.2 // indirect
github.com/tdewolff/parse/v2 v2.4.2 // indirect
github.com/urfave/cli v1.22.2 // indirect
github.com/yuin/goldmark v1.1.32 // indirect
github.com/yuin/goldmark-highlighting v0.0.0-20200307114337-60d527fdb691 // indirect
go.etcd.io/bbolt v1.3.2 // indirect
go.opencensus.io v0.22.3 // indirect
go.uber.org/atomic v1.6.0 // indirect
go.uber.org/multierr v1.5.0 // indirect
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de // indirect
golang.org/x/image v0.0.0-20191214001246-9130b4cfad52 // indirect
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 // indirect
golang.org/x/text v0.3.2 // indirect
google.golang.org/api v0.20.0 // indirect
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/grpc v1.27.1 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/ini.v1 v1.51.1 // indirect
gopkg.in/square/go-jose.v2 v2.4.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
howett.net/plist v0.0.0-20200419221736-3b63eb3a43b5 // indirect
) )

1096
go.sum

File diff suppressed because it is too large Load Diff

@ -2,9 +2,9 @@ package caddyhugo
import ( import (
"encoding/base64" "encoding/base64"
"errors"
"fmt" "fmt"
"io" "io"
"net"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -12,26 +12,25 @@ import (
"strings" "strings"
"git.stephensearles.com/stephen/caddy-hugo2/assets" "git.stephensearles.com/stephen/caddy-hugo2/assets"
"github.com/caddyserver/caddy" "github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
"github.com/spf13/afero" "github.com/spf13/afero"
) )
func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
if !ch.Match(r) { if !ch.Match(r) {
p := path.Join(ch.Dir, "public", r.URL.Path) p := path.Join(ch.Dir, "public", r.URL.Path)
http.ServeFile(w, r, p) http.ServeFile(w, r, p)
return 200, nil return nil
} }
if ch.Comments != nil && strings.HasSuffix(r.URL.Path, "/comments") { if ch.Comments != nil && strings.HasSuffix(r.URL.Path, "/comments") {
docName := docNameFromCommentRequest(r) docName := docNameFromCommentRequest(r)
err := ch.Comments.ServeComments(docName, w, r) err := ch.Comments.ServeComments(docName, w, r)
if err != nil { if err != nil {
return 500, fmt.Errorf("couldn't load comments:", err) return fmt.Errorf("couldn't load comments:", err)
} }
return 200, nil return nil
} }
if r.URL.Path == "/login" { if r.URL.Path == "/login" {
@ -41,32 +40,31 @@ func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, w http.ResponseW
if strings.HasPrefix(r.URL.Path, "/hugo/publish") { if strings.HasPrefix(r.URL.Path, "/hugo/publish") {
err := ch.Publish() err := ch.Publish()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) return err
return http.StatusInternalServerError, nil
} }
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
return http.StatusFound, nil return nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/simplemde.css") { if strings.HasPrefix(r.URL.Path, "/hugo/simplemde.css") {
w.Write(assets.MustAsset("simplemde/dist/simplemde.min.css")) w.Write(assets.MustAsset("simplemde/dist/simplemde.min.css"))
return http.StatusOK, nil return nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/simplemde.js") { if strings.HasPrefix(r.URL.Path, "/hugo/simplemde.js") {
w.Write(assets.MustAsset("simplemde/debug/simplemde.js")) w.Write(assets.MustAsset("simplemde/debug/simplemde.js"))
return http.StatusOK, nil return nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/vue.js") { if strings.HasPrefix(r.URL.Path, "/hugo/vue.js") {
w.Write(assets.MustAsset("js/vue.js")) w.Write(assets.MustAsset("js/vue.js"))
return http.StatusOK, nil return nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/moment.js") { if strings.HasPrefix(r.URL.Path, "/hugo/moment.js") {
w.Write(assets.MustAsset("js/moment.js")) w.Write(assets.MustAsset("js/moment.js"))
return http.StatusOK, nil return nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/font-awesome.css") { if strings.HasPrefix(r.URL.Path, "/hugo/font-awesome.css") {
w.Write(assets.MustAsset("css/font-awesome.min.css")) w.Write(assets.MustAsset("css/font-awesome.min.css"))
return http.StatusOK, nil return nil
} }
if strings.HasPrefix(r.URL.Path, "/hugo/admin") { if strings.HasPrefix(r.URL.Path, "/hugo/admin") {
return ch.Admin().ServeHTTP(w, r) return ch.Admin().ServeHTTP(w, r)
@ -91,38 +89,25 @@ func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, w http.ResponseW
} }
if strings.HasPrefix(r.URL.Path, "/hugo/fs/") { if strings.HasPrefix(r.URL.Path, "/hugo/fs/") {
printTree(afero.NewOsFs(), w, ch.Dir) printTree(afero.NewOsFs(), w, ch.Dir)
return 200, nil return nil
} }
return next.ServeHTTP(w, r) return next.ServeHTTP(w, r)
} }
func (ch *CaddyHugo) ServeNewContent(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) ServeNewContent(w http.ResponseWriter, r *http.Request) error {
name := r.FormValue("name") name := r.FormValue("name")
ctype := r.FormValue("type") ctype := r.FormValue("type")
filename, err := ch.NewContent(name, ctype) filename, err := ch.NewContent(name, ctype)
if err != nil { if err != nil {
fmt.Println("error creating new content:", err) fmt.Println("error creating new content:", err)
return http.StatusInternalServerError, err return err
} }
// serve redirect // serve redirect
http.Redirect(w, r, filepath.Join("/hugo/edit/", "content", filename), http.StatusFound) http.Redirect(w, r, filepath.Join("/hugo/edit/", "content", filename), http.StatusFound)
return http.StatusFound, nil return nil
}
func (ch *CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware {
return func(next httpserver.Handler) httpserver.Handler {
host := ch.Site.Addr.Host
hostport := net.JoinHostPort(ch.Site.Addr.Host, ch.Site.Addr.Port)
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
if r.Host != host && r.Host != hostport {
return next.ServeHTTP(w, r)
}
return ch.ServeHTTPWithNext(next, w, r)
})
}
} }
func (ch *CaddyHugo) Auth(r *http.Request) bool { func (ch *CaddyHugo) Auth(r *http.Request) bool {
@ -131,11 +116,6 @@ func (ch *CaddyHugo) Auth(r *http.Request) bool {
} }
func (ch *CaddyHugo) Match(r *http.Request) bool { func (ch *CaddyHugo) Match(r *http.Request) bool {
host := ch.Site.Addr.Host
hostport := net.JoinHostPort(ch.Site.Addr.Host, ch.Site.Addr.Port)
if r.Host != host && r.Host != hostport {
return false
}
if strings.HasPrefix(r.URL.Path, "/media/") { if strings.HasPrefix(r.URL.Path, "/media/") {
return true return true
} }
@ -155,34 +135,34 @@ func (ch *CaddyHugo) Match(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, "/hugo/") return strings.HasPrefix(r.URL.Path, "/hugo/")
} }
func (ch *CaddyHugo) Admin() httpserver.Handler { func (ch *CaddyHugo) Admin() caddyhttp.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
err := ch.adminTmpl.Execute(w, ch.TmplData(r, nil)) err := ch.adminTmpl.Execute(w, ch.TmplData(r, nil))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusInternalServerError, err return err
} }
return http.StatusOK, nil return nil
}) })
} }
func (ch *CaddyHugo) AuthorHome() httpserver.Handler { func (ch *CaddyHugo) AuthorHome() caddyhttp.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
td := ch.TmplData(r, nil) td := ch.TmplData(r, nil)
err := ch.authorTmpl.Execute(w, td) err := ch.authorTmpl.Execute(w, td)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusInternalServerError, err return err
} }
return http.StatusOK, nil return nil
}) })
} }
func (ch *CaddyHugo) Edit() httpserver.Handler { func (ch *CaddyHugo) Edit() caddyhttp.Handler {
return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
if r.URL.Path == "/hugo/edit/new" { if r.URL.Path == "/hugo/edit/new" {
return ch.ServeNewContent(w, r) return ch.ServeNewContent(w, r)
} }
@ -194,32 +174,31 @@ func (ch *CaddyHugo) Edit() httpserver.Handler {
doc, err := ch.editSession(docNameFromEditRequest(r)) doc, err := ch.editSession(docNameFromEditRequest(r))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusNotFound, err http.Error(w, err.Error(), http.StatusNotFound)
return err
} }
err = ch.editTmpl.Execute(w, ch.TmplData(r, doc)) err = ch.editTmpl.Execute(w, ch.TmplData(r, doc))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return http.StatusInternalServerError, err return err
} }
return http.StatusOK, nil return nil
}) })
} }
func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) error {
pathSegments := strings.SplitN(r.URL.Path, "/", 5) pathSegments := strings.SplitN(r.URL.Path, "/", 5)
if len(pathSegments) < 4 { if len(pathSegments) < 4 {
return errors.New("not found")
return http.StatusNotFound, nil
} }
encoded := pathSegments[3] encoded := pathSegments[3]
nameBytes, err := base64.RawURLEncoding.DecodeString(encoded) nameBytes, err := base64.RawURLEncoding.DecodeString(encoded)
if err != nil { if err != nil {
return http.StatusNotFound, err return errors.New("not found")
} }
name := string(nameBytes) name := string(nameBytes)
@ -229,7 +208,7 @@ func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, er
docref, ok := ch.docs[ch.docFilename(name)] docref, ok := ch.docs[ch.docFilename(name)]
if !ok { if !ok {
return http.StatusNotFound, fmt.Errorf("draft not found") return fmt.Errorf("draft not found")
} }
r.URL.Path = strings.ToLower(r.URL.Path) r.URL.Path = strings.ToLower(r.URL.Path)
@ -240,13 +219,13 @@ func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, er
page := ch.HugoSites.GetContentPage(ch.docFilename(name)) page := ch.HugoSites.GetContentPage(ch.docFilename(name))
if page == nil { if page == nil {
fmt.Fprintf(w, "can't find %q to display a draft", name) fmt.Fprintf(w, "can't find %q to display a draft", name)
return 404, nil return fmt.Errorf("draft not found")
} }
r.URL.Path = page.RelPermalink() r.URL.Path = page.RelPermalink()
http.FileServer(aferoHTTP{afero.NewBasePathFs(docref.tmpfs, path.Join(ch.Dir, "public"))}).ServeHTTP(w, r) http.FileServer(aferoHTTP{afero.NewBasePathFs(docref.tmpfs, path.Join(ch.Dir, "public"))}).ServeHTTP(w, r)
return 200, nil return nil
} }
func printTree(fs afero.Fs, w io.Writer, dir string) { func printTree(fs afero.Fs, w io.Writer, dir string) {
@ -328,9 +307,9 @@ func (a aferoHTTP) Open(name string) (http.File, error) {
return af, err return af, err
} }
func (ch *CaddyHugo) commentsLogin(r *http.Request, w http.ResponseWriter) (int, error) { func (ch *CaddyHugo) commentsLogin(r *http.Request, w http.ResponseWriter) error {
if ch.Comments == nil { if ch.Comments == nil {
return 200, nil return nil
} }
_, ok := ch.Comments.User(r) _, ok := ch.Comments.User(r)
@ -338,10 +317,10 @@ func (ch *CaddyHugo) commentsLogin(r *http.Request, w http.ResponseWriter) (int,
w.Header().Set("WWW-Authenticate", `Basic realm="Log in with your name and the password. Ask Dan or Stephen for the password."`) w.Header().Set("WWW-Authenticate", `Basic realm="Log in with your name and the password. Ask Dan or Stephen for the password."`)
w.WriteHeader(401) w.WriteHeader(401)
fmt.Fprintf(w, "<html><body>Log in with your name and the password. Ask Dan or Stephen for the password. <a href=%q>go back</a></body></html>", r.Referer()) fmt.Fprintf(w, "<html><body>Log in with your name and the password. Ask Dan or Stephen for the password. <a href=%q>go back</a></body></html>", r.Referer())
return 200, nil return nil
} }
http.Redirect(w, r, r.Referer(), http.StatusFound) http.Redirect(w, r, r.Referer(), http.StatusFound)
return 200, nil return nil
} }

@ -0,0 +1,8 @@
.PHONY: run
run: CADDY_DEBUG=1
run:
xcaddy build --with git.stephensearles.com/stephen/caddy-hugo2@latest=./ && cd testdir && ../caddy run --config Caddyfile
.PHONY: build
build:
xcaddy build --with github.com/caddy-dns/digitalocean --with git.stephensearles.com/stephen/caddy-hugo2@latest=./

@ -18,7 +18,12 @@ func (ch *CaddyHugo) ReferencedMedia() map[string]map[page.Page]struct{} {
found := map[string]map[page.Page]struct{}{} found := map[string]map[page.Page]struct{}{}
for _, pg := range ch.HugoSites.Pages() { for _, pg := range ch.HugoSites.Pages() {
r := bytes.NewBufferString(string(pg.Render())) renderOutput, err := pg.Render()
if err != nil {
continue
}
r := bytes.NewBufferString(string(renderOutput))
doc, err := goquery.NewDocumentFromReader(r) doc, err := goquery.NewDocumentFromReader(r)
if err != nil { if err != nil {
continue continue
@ -43,16 +48,16 @@ func (ch *CaddyHugo) ReferencedMedia() map[string]map[page.Page]struct{} {
return found return found
} }
func (ch *CaddyHugo) uploadMedia(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) uploadMedia(w http.ResponseWriter, r *http.Request) error {
if ch.Media == nil { if ch.Media == nil {
http.NotFound(w, r) http.NotFound(w, r)
return 404, nil return nil
} }
mr, err := r.MultipartReader() mr, err := r.MultipartReader()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return 400, nil return nil
} }
for { for {
@ -62,7 +67,7 @@ func (ch *CaddyHugo) uploadMedia(w http.ResponseWriter, r *http.Request) (int, e
} }
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
return 400, nil return err
} }
name := part.FileName() name := part.FileName()
@ -70,18 +75,18 @@ func (ch *CaddyHugo) uploadMedia(w http.ResponseWriter, r *http.Request) (int, e
err = ch.Media.ReceiveNewMedia(name, part) err = ch.Media.ReceiveNewMedia(name, part)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return 500, nil return nil
} }
} }
} }
return 200, nil return nil
} }
func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) error {
if ch.Media == nil { if ch.Media == nil {
http.NotFound(w, r) http.NotFound(w, r)
return 404, nil return nil
} }
referenced := ch.ReferencedMedia() referenced := ch.ReferencedMedia()
@ -114,7 +119,7 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int
mm, err := ch.Media.Walk() mm, err := ch.Media.Walk()
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return 500, nil return err
} }
for _, m := range media.Set(mm).ByDate() { for _, m := range media.Set(mm).ByDate() {
@ -167,15 +172,15 @@ func (ch *CaddyHugo) serveMediaPage(w http.ResponseWriter, r *http.Request) (int
} }
} }
</script></body><html>`) </script></body><html>`)
return 200, nil return nil
} }
func (ch *CaddyHugo) serveMedia(w http.ResponseWriter, r *http.Request) (int, error) { func (ch *CaddyHugo) serveMedia(w http.ResponseWriter, r *http.Request) error {
if ch.Media == nil { if ch.Media == nil {
http.NotFound(w, r) http.NotFound(w, r)
return 404, nil return nil
} }
ch.Media.ServeHTTP(w, r) ch.Media.ServeHTTP(w, r)
return 200, nil return nil
} }

@ -5,18 +5,16 @@ import (
"html/template" "html/template"
"os" "os"
"path" "path"
"path/filepath"
"git.stephensearles.com/stephen/caddy-hugo2/comments" "git.stephensearles.com/stephen/caddy-hugo2/comments"
"git.stephensearles.com/stephen/caddy-hugo2/media" "git.stephensearles.com/stephen/caddy-hugo2/media"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
"github.com/gohugoio/hugo/hugofs" "github.com/gohugoio/hugo/hugofs"
) )
var eventHookCounter uint64 var eventHookCounter uint64
/*
func SetupCaddy(c *caddy.Controller) error { func SetupCaddy(c *caddy.Controller) error {
ch := &CaddyHugo{} ch := &CaddyHugo{}
@ -36,18 +34,13 @@ func SetupCaddy(c *caddy.Controller) error {
return err return err
} }
*/
func (ch *CaddyHugo) commentsSetting(c *caddy.Controller) { func (ch *CaddyHugo) commentsSetting() {
for c.NextLine() { if ch.Site.CommentsEnabled {
if c.Val() == "hugo" { ch.Comments = comments.WithStorage(comments.NewDiskv(path.Join(ch.Site.Root, "comments")))
for c.NextBlock() { if ch.Site.CommentsPassword != "" {
if c.Val() == "comments" { ch.Comments.Password = ch.Site.CommentsPassword
ch.Comments = comments.WithStorage(comments.NewDiskv(path.Join(ch.Site.Root, "comments")))
if c.NextArg() {
ch.Comments.Password = c.Val()
}
}
}
} }
} }
} }
@ -55,6 +48,7 @@ func (ch *CaddyHugo) commentsSetting(c *caddy.Controller) {
func (ch *CaddyHugo) Setup(dir string) error { func (ch *CaddyHugo) Setup(dir string) error {
var err error var err error
ch.log("setting up caddy-hugo in", dir)
ch.Dir = dir ch.Dir = dir
ch.docs = make(map[string]*editSession) ch.docs = make(map[string]*editSession)
ch.confirmingToClient = make(map[uint64]struct{}) ch.confirmingToClient = make(map[uint64]struct{})

@ -1,17 +1,19 @@
package caddyhugo package caddyhugo
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"html/template"
"net/http" "net/http"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"sync"
"git.stephensearles.com/stephen/acedoc" "git.stephensearles.com/stephen/acedoc"
"git.stephensearles.com/stephen/caddy-hugo2/frontend"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
) )
func (t *tmplData) Content() ([]Content, error) { func (t *tmplData) Content() ([]Content, error) {
@ -63,7 +65,7 @@ func (t *tmplData) contentTypes(dir string) ([]string, error) {
} }
type tmplData struct { type tmplData struct {
Site *httpserver.SiteConfig Site SiteConfig
R *http.Request R *http.Request
*CaddyHugo *CaddyHugo
Doc *acedoc.Document Doc *acedoc.Document
@ -88,441 +90,29 @@ func (t *tmplData) IframeSource() string {
return fmt.Sprintf("/hugo/draft/%s/%s/%s/", base64.RawURLEncoding.EncodeToString([]byte(t.docref.docname)), ctype, strings.Replace(name, " ", "-", -1)) return fmt.Sprintf("/hugo/draft/%s/%s/%s/", base64.RawURLEncoding.EncodeToString([]byte(t.docref.docname)), ctype, strings.Replace(name, " ", "-", -1))
} }
var EditPage = `<html> var EditPage = frontend.EditPage()
<head>
<script src="/hugo/simplemde.js"></script>
<style type="text/css" media="screen">
#editor-wrapper {
position: absolute;
top: 50px;
right: 0;
bottom: 150px;
left: 40%;
}
#draft {
position: absolute;
top: 50px;
right: 60%;
bottom: 0;
left: 0;
}
#draft > iframe {
height: 100%;
width: 100%;
border: none;
}
a {
text-decoration: underline;
text-decoration-style: dotted;
cursor: pointer;
}
</style>
<link rel="stylesheet" href="/hugo/simplemde.css" />
<script src="/hugo/vue.js"></script>
<script src="/hugo/moment.js"></script>
<body>
<div id="container" >
<div id="header">
<div id="lastSaved">
<span v-if="sendQueue.length > 0 || Object.keys(needConfirmation).length > 0">last saved ${ lastSaved.from(now) }, saving</span>
<span v-else>saved</span>
<span v-if="connectionError">, ${connectionError}</span>
</div>
<div>
<a id="sideview-toggle-media">media</a>
<a id="sideview-toggle-draft">draft</a>
</div>
</div>
<div id="editor-wrapper">
<textarea id="editor">{{ .LoadContent }}</textarea>
</div>
<div id="draft"><iframe src="{{ .IframeSource }}">Loading draft...</iframe></div>
</div>
<script>
var iframe = document.querySelector("#draft > iframe");
document.onclick = function (event) {
var iframe = document.querySelector("#draft > iframe");
switch (event.target.id) {
case "sideview-toggle-media":
iframe.src = "/hugo/media";
break;
case "sideview-toggle-draft":
iframe.src = "{{ .IframeSource }}";
break;
}
}
var uiBindings = {
ltime: {{ .LTime }},
serverLtime: 0,
lastSaved: moment(),
now: moment(),
connectionError: null,
sendQueue: [],
sentRecently: [],
needConfirmation: {},
};
var app = new Vue({
el: "#container",
data: uiBindings,
delimiters: ["${", "}"],
});
function getLtime() {
uiBindings.ltime++
return uiBindings.ltime
}
function observeServer(l, confirmed) {
uiBindings.serverLtime = l;
if (confirmed && confirmed.length > 0) {
confirmed.forEach(function (e) {
delete uiBindings.needConfirmation[e];
})
}
observe(l);
}
function observe(l) {
if (l > uiBindings.ltime) {
uiBindings.now = moment();
uiBindings.lastSaved = moment();
uiBindings.ltime = l;
}
}
var selectedImage;
var editorElem = document.getElementById("editor");
var editor = new SimpleMDE({
element: editorElem,
forceSync: true,
insertTexts: {
image: ["{\{% thumb filename=\"", "#url#\" width=\"200\" %}}"]
},
imageURLFn: function () {
return selectedImage;
}
});
window.onmessage = function (evt) {
selectedImage = evt.data;
}
// Create WebSocket connection.
var socket = connect();
const sawChangesBumpsTo = 10;
var sentinelSrc = 'about:blank';
var oldSrc = '';
var sawChanges = -1;
window.setInterval(function () {
if (sawChanges >= 0) {
sawChanges--;
if (sawChanges == 0) {
if (iframe.contentWindow) {
iframe.contentWindow.location.reload();
}
}
}
uiBindings.now = moment();
if (uiBindings.connectionError) {
socket = connect();
} else if (uiBindings.sendQueue.length > 0) {
var ltime = getLtime();
// record lowest pending
// ltime at the time this message
// was serialized
var lowestPending = ltime;
for (c in uiBindings.needConfirmation) {
c = parseInt(c, 10);
if (lowestPending === 0 || c < lowestPending) {
lowestPending = c;
}
}
var msg = JSON.stringify({
"deltas": uiBindings.sendQueue,
"ltime": ltime,
"lowestPending": lowestPending,
});
uiBindings.sendQueue = [];
uiBindings.needConfirmation[ltime] = msg;
}
for (ltime in uiBindings.needConfirmation) {
var msg = uiBindings.needConfirmation[ltime];
socket.send(msg);
}
}, 500);
function connect() {
const socket = new WebSocket((location.protocol == "https:" ? 'wss://' : 'ws://') + location.host + location.pathname);
// Listen for messages
socket.addEventListener('message', function (event) {
var message = JSON.parse(event.data);
observeServer(message.ltime, message.confirmed);
var deltas = [];
deltas.push.apply(deltas, message.deltas);
deltas.forEach(function(aceDelta) {
var cmDelta = aceDeltaToCM(aceDelta)
var content = ""
var to = {
line: aceDelta.start.row,
ch: aceDelta.start.column,
}
if (aceDelta.action == "insert") {
content = aceDelta.lines.join("\n");
to = null;
}
editor.codemirror.doc.replaceRange(content, cmDelta.from, to, "dontreflect");
sawChanges = sawChangesBumpsTo;
})
});
socket.addEventListener('open', function () {
uiBindings.connectionError = null;
});
socket.addEventListener('close', function () { var AdminPage = frontend.AdminPage()
if (!uiBindings.connectionError) {
getLtime();
uiBindings.connectionError = "server connection closed, reconnecting...";
}
});
socket.addEventListener('error', function (err) { var AuthorPage = frontend.AuthorPage()
if (!uiBindings.connectionError) {
uiBindings.connectionError = err;
getLtime();
}
console.log(err);
});
var uploadTmpl *template.Template
return socket; var uploadTmplOnce sync.Once
}
editor.codemirror.on("change", function (cm, cmDelta) {
if (cmDelta.origin == "dontreflect") {
return;
}
var aceDelta = cmDeltaToAce(cmDelta);
console.log(cmDelta, "=>", aceDelta)
sawChanges = sawChangesBumpsTo;
uiBindings.sendQueue.push.apply(uiBindings.sendQueue, aceDelta);
})
function cmDeltaToAce(cmDelta) {
var isRemove = (cmDelta.removed.length > 0 && cmDelta.removed[0].length > 0) || cmDelta.removed.length > 1;
var lines = isRemove ? cmDelta.removed : cmDelta.text;
var aceDelta = {
action: isRemove ? "remove" : "insert",
lines: lines,
start: {
row: cmDelta.from.line,
column: cmDelta.from.ch,
},
end: {
row: cmDelta.from.line + (isRemove ? lines.length - 1 : lines.length - 1 ),
column: lines[lines.length-1].length,
}
};
if (aceDelta.start.row == aceDelta.end.row) {
aceDelta.end.column += cmDelta.from.ch;
}
if (false && isRemove && aceDelta.start.row == aceDelta.end.row) {
var origStart = aceDelta.start;
aceDelta.start = aceDelta.end;
aceDelta.end = origStart;
aceDelta.start.column += cmDelta.from.ch;
}
if (isRemove && ((cmDelta.text.length > 0 && cmDelta.text[0].length > 0) || cmDelta.text.length > 1)) {
cmDelta.removed = [""];
var ret = [aceDelta];
ret.push.apply(ret, cmDeltaToAce(cmDelta));
return ret;
}
return [aceDelta];
}
function aceDeltaToCM(aceDelta) {
var cmDelta = {
text: [],
removed: [],
from: {
line: aceDelta.start.row,
ch: aceDelta.start.column,
},
to: {
// cm deltas are weird. to refers to the selection end, which
// with a simple blinking cursor with no selection, is always
// the same as from
line: aceDelta.start.row,
ch: aceDelta.start.column,
},
}
if (aceDelta.action == "remove") {
var origStart = aceDelta.start;
aceDelta.start = aceDelta.end;
aceDelta.end = origStart;
cmDelta.removed = aceDelta.lines
cmDelta.text = [""]
} else {
cmDelta.text = aceDelta.lines
cmDelta.removed = [""]
}
return cmDelta;
}
</script>
</body>
</html>`
var AdminPage = `<html><body>not implemented</body></html>`
var AuthorPage = `<html>
<head>
</head>
<body>
{{ $timeFormat := "Jan _2 15:04:05" }}
<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>
<table>{{ range .Content }}
<tr>
{{ if .Metadata }}
<td>
<a href="/hugo/edit/{{ .Filename }}">
{{ .Metadata.Title }}
</a>
</td>
<td>
{{ .Metadata.Date.Format $timeFormat }}
{{ if not (.Metadata.Lastmod.Equal .Metadata.Date) }}
(last modified {{.Metadata.Lastmod.Format $timeFormat }})
{{end}}
</td>
{{ else }}
<td>
<a href="/hugo/edit/{{ .Filename }}">
{{ .Filename }}
</a>
</td>
<td>(unable to load metadata)</td>
{{ end }}
</tr>
{{- end }}
</table>
</body>
</html>`
func UploadPage(elemName string) string { func UploadPage(elemName string) string {
return fmt.Sprintf(` uploadTmplOnce.Do(func() {
<input type="file" style="display: hidden;" id="%s" /> p := frontend.UploadPage()
<div id="%s_dropzone" ondrop="dropHandler(event);" ondragover="draghandler(event);" ondragenter="draghandler(event);" ondragleave="draghandler(event);" style="background-color: rgba(0,0,0,0.5); visibility: hidden; opacity:0; position: fixed; top: 0; bottom: 0; left: 0; right: 0; width: 100%; height: 100%; ; transition: visibility 175ms, opacity 175ms; z-index: 9999999;"></div> t, err := template.New("").Parse(p)
<script> if err != nil {
document.addEventListener("DOMContentLoaded", function () { panic(err)
var fileInput = document.getElementById('%s');
var dropzone = document.getElementById('%s_dropzone');
fileInput.onchange = function () {
var formData = new FormData();
fileInput.files.forEach(function (file) {
formData.append(file.name, file);
});
upload(formData);
}
var lastTarget = null;
window.addEventListener("dragenter", function(e)
{
lastTarget = e.target; // cache the last target here
// unhide our dropzone overlay
dropzone.style.visibility = "";
dropzone.style.opacity = 1;
});
window.addEventListener("dragleave", function(e)
{
// this is the magic part. when leaving the window,
// e.target happens to be exactly what we want: what we cached
// at the start, the dropzone we dragged into.
// so..if dragleave target matches our cache, we hide the dropzone.
if(e.target === lastTarget)
{
dropzone.style.visibility = "hidden";
dropzone.style.opacity = 0;
} }
}); uploadTmpl = t
})
});
function draghandler(evt) {
evt.preventDefault();
}
function dropHandler(evt) {
evt.preventDefault();
var files = evt.dataTransfer.files; var buf bytes.Buffer
var formData = new FormData(); err := uploadTmpl.Execute(&buf, struct{ ElemName string }{elemName})
if err != nil {
for (var i = 0; i < files.length; i++) { panic(err)
formData.append(files[i].name, files[i]);
}
upload(formData);
return false;
}
function upload(formData) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(e) {
if ( 4 == this.readyState ) {
window.location.reload(true);
}
}
xhr.open('POST', '/hugo/upload');
xhr.send(formData);
} }
return buf.String()
</script>
`, elemName, elemName, elemName, elemName, elemName, elemName)
} }

@ -0,0 +1,17 @@
{
order hugo last
}
http://localhost:8080 {
hugo {
comments test
}
root ./testdir/testsite
log
}
http://localhost:8081, http://localhost:8082 {
root ./testdir/testsite2
hugo
log
}

@ -1,14 +0,0 @@
localhost:8080 {
hugo {
comments test
}
root ./testsite
errors { * }
pprof
}
localhost:8081, localhost:8082 {
root ./testsite2
hugo
errors { * }
}

@ -1,4 +1,3 @@
baseURL = "http://example.org/" baseURL = "http://example.org/"
languageCode = "en-us" languageCode = "en-us"
title = "My New Hugo Site" title = "My New Hugo Site"
theme = "hugo-theme-minos"

@ -0,0 +1,3 @@
package caddyhugo
import _ "github.com/caddyserver/xcaddy"
Loading…
Cancel
Save