-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
134 lines (110 loc) · 2.66 KB
/
main.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
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
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"encoding/json"
"log"
"os"
"reflect"
"strings"
"time"
)
const (
MainTemplatePath = "templates/main.html"
ExperienceKey = "experiences"
ExperienceItemPath = "templates/experienceItem.html"
ProjectKey = "projects"
ProjectItemPath = "templates/projectItem.html"
SkillKey = "skills"
SkillItemPath = "templates/skillItem.html"
CertificationKey = "certifications"
CertificationPath = "templates/certificationItem.html"
AwardKey = "awards"
AwardPath = "templates/awardItem.html"
DataJSONPath = "data.json"
OutputFilePath = "index.html"
)
var mappingTemplate = map[string]string{
ExperienceKey: ExperienceItemPath,
SkillKey: SkillItemPath,
ProjectKey: ProjectItemPath,
CertificationKey: CertificationPath,
AwardKey: AwardPath,
}
var (
beforeMain []byte
beforeData []byte
)
func main() {
beforeMain = []byte("")
beforeData = []byte("")
for {
templateByte := readFile(MainTemplatePath)
if !bytes.Equal(beforeMain, templateByte) {
generate()
}
templateByte = readFile(DataJSONPath)
if !bytes.Equal(beforeData, templateByte) {
generate()
}
time.Sleep(1000 * time.Millisecond)
}
}
func generate() {
dataJsonByte := readFile(DataJSONPath)
var dataJsonMap map[string]interface{}
err := json.Unmarshal(dataJsonByte, &dataJsonMap)
if err != nil {
panic(err)
}
mainTemplateByte := readFile(MainTemplatePath)
templateString := string(mainTemplateByte)
for k, v := range dataJsonMap {
t := reflect.TypeOf(v)
if t.Kind() == reflect.String {
templateString = strings.ReplaceAll(templateString, "{{"+k+"}}", v.(string))
} else if t.Kind() == reflect.Slice {
resp := sliceHandle(k, v.([]interface{}))
for k2, v2 := range resp {
templateString = strings.ReplaceAll(templateString, "{{"+k2+"}}", v2)
}
}
}
index, err := os.Create(OutputFilePath)
if err != nil {
panic(err)
}
_, err = index.WriteString(templateString)
if err != nil {
panic(err)
}
log.Println("generate() success")
beforeMain = mainTemplateByte
beforeData = dataJsonByte
}
func sliceHandle(key string, slice []interface{}) map[string]string {
filePath := ""
filePath, ok := mappingTemplate[key]
if !ok {
panic("key not found: " + key)
}
templateFile := string(readFile(filePath))
result := ""
for _, v := range slice {
item := templateFile
v2 := v.(map[string]interface{})
for k, value := range v2 {
item = strings.ReplaceAll(item, "{{"+k+"}}", value.(string))
}
result += item
}
return map[string]string{
key: result,
}
}
func readFile(temp string) []byte {
templateByte, err := os.ReadFile(temp)
if err != nil {
panic(err)
}
return templateByte
}