Browse Source

copying theme addition files onto the hugo directories. fixes #6

Stephen Searles 1 year ago
parent
commit
ff1033dfb4
6 changed files with 463 additions and 4 deletions
  1. 3
    1
      client.go
  2. 69
    0
      http.go
  3. 40
    2
      hugo.go
  4. 11
    1
      setup.go
  5. 3
    0
      theme-additions/assets.go
  6. 337
    0
      theme-additions/bindata.go

+ 3
- 1
client.go View File

@@ -38,11 +38,13 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) {
38 38
 		return nil, err
39 39
 	}
40 40
 
41
+	tmpfs := afero.NewCopyOnWriteFs(afero.NewOsFs(), afero.NewMemMapFs())
42
+
41 43
 	es := &editSession{
42 44
 		docname:  docName,
43 45
 		filename: filename,
44 46
 		doc:      acedoc.NewString(string(contents)),
45
-		tmpfs:    afero.NewCopyOnWriteFs(afero.NewOsFs(), afero.NewMemMapFs()),
47
+		tmpfs:    tmpfs,
46 48
 	}
47 49
 
48 50
 	err = es.doc.LogToFile(path.Join(ch.Dir, "logs", docName))

+ 69
- 0
http.go View File

@@ -3,6 +3,7 @@ package caddyhugo
3 3
 import (
4 4
 	"encoding/base64"
5 5
 	"fmt"
6
+	"io"
6 7
 	"net"
7 8
 	"net/http"
8 9
 	"os"
@@ -88,6 +89,10 @@ func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, w http.ResponseW
88 89
 	if strings.HasPrefix(r.URL.Path, "/media/") {
89 90
 		return ch.serveMedia(w, r)
90 91
 	}
92
+	if strings.HasPrefix(r.URL.Path, "/hugo/fs/") {
93
+		printTree(afero.NewOsFs(), w, ch.Dir)
94
+		return 200, nil
95
+	}
91 96
 
92 97
 	return next.ServeHTTP(w, r)
93 98
 }
@@ -244,6 +249,70 @@ func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, er
244 249
 	return 200, nil
245 250
 }
246 251
 
252
+func printTree(fs afero.Fs, w io.Writer, dir string) {
253
+	const (
254
+		Line  = "  │ "
255
+		Tab   = "    "
256
+		Elbow = "  └─"
257
+		Tee   = "  ├─"
258
+	)
259
+
260
+	wd, _ := os.Getwd()
261
+	fmt.Fprintln(w, wd)
262
+
263
+	if dir == "" {
264
+		dir = "/"
265
+	}
266
+
267
+	openDirs := map[string]bool{}
268
+	lastFiles := map[string]string{}
269
+
270
+	afero.Walk(fs, dir, filepath.WalkFunc(func(p string, info os.FileInfo, err error) error {
271
+		if strings.HasPrefix(p, "./") {
272
+			p = p[2:]
273
+		}
274
+
275
+		openDirs[filepath.Dir(p)] = true
276
+		lastFiles[filepath.Dir(p)] = filepath.Base(p)
277
+		return nil
278
+	}))
279
+
280
+	afero.Walk(fs, dir, filepath.WalkFunc(func(p string, info os.FileInfo, err error) error {
281
+		if err != nil {
282
+			return err
283
+		}
284
+
285
+		if strings.HasPrefix(p, "./") {
286
+			p = p[2:]
287
+		}
288
+
289
+		if filepath.Base(p)[0] == '.' && info.IsDir() {
290
+			return filepath.SkipDir
291
+		}
292
+
293
+		entry := Tee
294
+
295
+		if lastFiles[filepath.Dir(p)] == filepath.Base(p) {
296
+			openDirs[filepath.Dir(p)] = false
297
+			entry = Elbow
298
+		}
299
+
300
+		indent := ""
301
+		dirs := strings.Split(p, string(filepath.Separator))
302
+		dirs = dirs[:len(dirs)-1]
303
+		for i := range dirs {
304
+			if openDirs[filepath.Join(dirs[:i]...)] {
305
+				indent += Line
306
+			} else {
307
+				indent += Tab
308
+			}
309
+		}
310
+
311
+		fmt.Fprintf(w, "%s%s %s (%s)\n", indent, entry, filepath.Base(p), p)
312
+		return nil
313
+	}))
314
+}
315
+
247 316
 type aferoHTTP struct {
248 317
 	afero.Fs
249 318
 }

+ 40
- 2
hugo.go View File

@@ -2,13 +2,18 @@ package caddyhugo
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"os"
5 6
 	"path"
7
+	"path/filepath"
6 8
 	"time"
7 9
 
8 10
 	"github.com/gohugoio/hugo/deps"
11
+	"github.com/gohugoio/hugo/hugofs"
9 12
 	"github.com/gohugoio/hugo/hugolib"
10 13
 	"github.com/spf13/afero"
14
+	"github.com/spf13/viper"
11 15
 
16
+	"git.stephensearles.com/stephen/caddy-hugo2/theme-additions"
12 17
 	"git.stephensearles.com/stephen/idleshut"
13 18
 )
14 19
 
@@ -41,9 +46,42 @@ func buildSite(sites *hugolib.HugoSites) error {
41 46
 	return err
42 47
 }
43 48
 
49
+func writeThemeFiles(dir string) error {
50
+	for _, asset := range themeadditions.AssetNames() {
51
+		err := os.MkdirAll(path.Join(dir, filepath.Dir(asset)), 0755)
52
+		if err != nil {
53
+			return err
54
+		}
55
+		fmt.Println("writing", path.Join(dir, asset))
56
+		f, err := os.Create(path.Join(dir, asset))
57
+		if err != nil {
58
+			return err
59
+		}
60
+		b, err := themeadditions.Asset(asset)
61
+		if err != nil {
62
+			return err
63
+		}
64
+
65
+		_, err = f.Write(b)
66
+		if err != nil {
67
+			return err
68
+		}
69
+
70
+		err = f.Close()
71
+		if err != nil {
72
+			return err
73
+		}
74
+	}
75
+
76
+	return nil
77
+}
78
+
44 79
 func (ch *CaddyHugo) configWithFs(fs afero.Fs) (*hugolib.HugoSites, *deps.DepsCfg, error) {
45 80
 	var err error
46
-	cfg := &deps.DepsCfg{}
81
+
82
+	hfs := hugofs.NewFrom(fs, &viper.Viper{})
83
+	cfg := &deps.DepsCfg{Fs: hfs}
84
+
47 85
 	cfgPath := path.Join(ch.Dir, "config.toml")
48 86
 	cfg.Cfg, err = hugolib.LoadConfig(fs, "", cfgPath)
49 87
 	if err != nil {
@@ -90,7 +128,7 @@ func HugoInternalProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (
90 128
 				return err
91 129
 			}
92 130
 
93
-			err = hugoSites.Build(hugolib.BuildCfg{ResetState: true})
131
+			err = buildSite(hugoSites)
94 132
 			if err != nil {
95 133
 				return err
96 134
 			}

+ 11
- 1
setup.go View File

@@ -5,6 +5,7 @@ import (
5 5
 	"html/template"
6 6
 	"os"
7 7
 	"path"
8
+	"path/filepath"
8 9
 
9 10
 	"git.stephensearles.com/stephen/caddy-hugo2/comments"
10 11
 	"git.stephensearles.com/stephen/caddy-hugo2/media"
@@ -20,7 +21,11 @@ func SetupCaddy(c *caddy.Controller) error {
20 21
 	ch := &CaddyHugo{}
21 22
 
22 23
 	ch.Site = httpserver.GetConfig(c)
23
-	err := ch.Setup(ch.Site.Root)
24
+	root, err := filepath.Abs(ch.Site.Root)
25
+	if err != nil {
26
+		return err
27
+	}
28
+	err = ch.Setup(root)
24 29
 
25 30
 	c.OnShutdown(func() error {
26 31
 		return ch.persistAllEdits()
@@ -89,6 +94,11 @@ func (ch *CaddyHugo) Setup(dir string) error {
89 94
 		return fmt.Errorf("couldn't initialize media: %v", err)
90 95
 	}
91 96
 
97
+	err = writeThemeFiles(ch.Dir)
98
+	if err != nil {
99
+		fmt.Println("error installing theme files:", err)
100
+	}
101
+
92 102
 	err = ch.Publish()
93 103
 	if err != nil {
94 104
 		fmt.Println("error with initial publish of hugo site:", err)

+ 3
- 0
theme-additions/assets.go View File

@@ -0,0 +1,3 @@
1
+//go:generate go-bindata -ignore .go -pkg themeadditions ./...
2
+
3
+package themeadditions

+ 337
- 0
theme-additions/bindata.go
File diff suppressed because it is too large
View File


Loading…
Cancel
Save