-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathckit2html.go
More file actions
123 lines (105 loc) · 2.64 KB
/
ckit2html.go
File metadata and controls
123 lines (105 loc) · 2.64 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"bytes"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"golang.org/x/net/html"
)
var (
inFolder, outFolder string
verboseOutput bool
showVersion bool
)
var (
version string
)
func main() {
flag.StringVar(&inFolder, "in", ".", "Input folder")
flag.StringVar(&outFolder, "out", ".", "Output folder")
flag.BoolVar(&verboseOutput, "v", false, "Set to verbose output")
flag.BoolVar(&showVersion, "version", false, "Show the version")
flag.Parse()
log.SetLevel(log.InfoLevel)
if verboseOutput {
log.SetLevel(log.TraceLevel)
}
if showVersion {
fmt.Fprintf(os.Stderr, "Running ckit2html version %s\n", version)
os.Exit(0)
}
log.Debug("Transforming .kit files from folder " + inFolder + " to " + outFolder)
files := []string{}
err := filepath.Walk(inFolder,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(path, ".kit") {
files = append(files, path)
}
return nil
})
if err != nil {
log.Error(err)
}
for _, p := range files {
parseFile(p)
}
}
func parseFile(path string) {
// If filename starts with `_` then we don't write it
if strings.HasPrefix(filepath.Base(path), "_") {
return
}
l := log.WithField("file", path)
l.Debug("Parsing")
r, err := os.Open(path)
if err != nil {
l.WithError(err).Error("Error loading file")
return
}
defer r.Close()
replacedContents := processFileImports(r, path, inFolder)
outputFileName := strings.ReplaceAll(strings.ReplaceAll(path, ".kit", ".html"), inFolder, outFolder)
l2 := l.WithField("out_file", outputFileName)
outputFile, err := os.OpenFile(outputFileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
l2.WithError(err).Error("Cannot create output file")
return
}
defer outputFile.Close()
outputFile.Write(replaceVariables(replacedContents.Bytes()))
l2.Trace("Output file written")
}
func processFileImports(r io.Reader, path, basePath string) *bytes.Buffer {
l := log.WithField("base_path", basePath).WithField("file", path)
fileContent := bytes.NewBuffer([]byte{})
z := html.NewTokenizer(r)
forLoop:
for {
tt := z.Next()
switch tt {
case html.ErrorToken:
if z.Err().Error() != io.EOF.Error() {
l.WithError(z.Err()).Error("Error parsing file")
}
break forLoop
case html.CommentToken:
tn := string(z.Text())
if strings.HasPrefix(tn, " @import ") || strings.HasPrefix(tn, "@include ") {
res := processImport(basePath, tn)
fileContent.Write(res)
} else {
fileContent.Write(z.Raw())
}
default:
fileContent.Write(z.Raw())
}
}
return fileContent
}