-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombiner.go
80 lines (63 loc) · 1.74 KB
/
combiner.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
79
80
// Combine index.html, style.css, and script.js into singlepage.html
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
const (
indexFilename = "index.html"
styleFilename = "style.css"
scriptFilename = "script.js"
outputFilename = "./singlepage.html"
styleLinkTag = "<link rel=\"stylesheet\" href=\"" + styleFilename + "\" />"
scriptTag = "<script src=\"" + scriptFilename + "\"></script>"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func concatFileContent(lines []string, filename string) []string {
fileLines, err := os.ReadFile(filename)
check(err)
lines = append(lines, strings.Split(string(fileLines), "\n")...)
return lines
}
func writeCombinedFile(lines []string) int {
combinedFile, err := os.Create(outputFilename)
check(err)
defer combinedFile.Close()
writer := bufio.NewWriter(combinedFile)
for _, line := range lines {
_, err := writer.WriteString(line + "\n")
check(err)
}
writer.Flush()
return 1
}
func main() {
indexFile, err := os.Open(indexFilename)
check(err)
defer indexFile.Close()
fileScanner := bufio.NewScanner(indexFile)
fileScanner.Split(bufio.ScanLines)
var lines []string
for fileScanner.Scan() {
line := fileScanner.Text()
if strings.Contains(string(line), styleLinkTag) {
lines = append(lines, "\t\t<style type=\"text/css\">")
lines = concatFileContent(lines, styleFilename)
lines = append(lines, "\t\t</style>")
} else if strings.Contains(string(line), scriptTag) {
lines = append(lines, "\t\t<script>")
lines = concatFileContent(lines, scriptFilename)
lines = append(lines, "\t\t</script>")
} else {
lines = append(lines, string(line))
}
}
writeCombinedFile(lines)
fmt.Printf("TimeDiff Combiner: Wrote %d lines to %s\n", len(lines), outputFilename)
}