-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw.go
78 lines (62 loc) · 1.57 KB
/
raw.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bytes"
"errors"
"io"
"io/fs"
"os"
"path"
"github.com/yosssi/gohtml"
)
func CopyRaw(config *Config) {
root := os.DirFS(config.RawSrcDir)
err := fs.WalkDir(root, ".", config.copyRawWalk)
if err != nil && errors.Is(err, fs.ErrNotExist) {
logger.Warnf("No raw directory found (%v)", config.RawSrcDir)
return
}
}
func (config *Config) copyRawWalk(path_ string, d fs.DirEntry, err error) error {
excludeMatch, excludeIndex := PathMatch(path_, config.ExcludePaths)
if excludeMatch != nil {
logger.Debugf("Excluding path (%v): rule %v", *excludeMatch, excludeIndex+1)
if d.IsDir() {
return fs.SkipDir
} else {
return nil
}
}
check(err)
srcPath := path.Join(config.RawSrcDir, path_)
dstPath := path.Join(config.DstDir, path_)
if d.IsDir() {
logger.Debugf("Copying directory (%v -> %v)", srcPath, dstPath)
err := os.Mkdir(dstPath, config.DstMode.FileMode)
if err != nil && !errors.Is(err, os.ErrExist) {
check(err)
}
} else {
logger.Debugf("Copying file (%v -> %v)", srcPath, dstPath)
src, err := os.Open(srcPath)
check(err)
defer src.Close()
dst, err := os.Create(dstPath)
check(err)
defer dst.Close()
err = dst.Chmod(config.DstMode.FileMode)
check(err)
if config.FmtRawHTML && path.Ext(d.Name()) == ".html" {
logger.Debug("Formatting HTML file")
preformat := new(bytes.Buffer)
_, err := io.Copy(preformat, src)
check(err)
formatted := gohtml.FormatBytes(preformat.Bytes())
_, err = dst.Write(formatted)
check(err)
} else {
_, err := io.Copy(dst, src)
check(err)
}
}
return nil
}