konstrui/fileOperations.go
fileOperations.goRaw
1package main
2
3import (
4 "bufio"
5 "encoding/json"
6 "io/ioutil"
7 "os"
8 "strconv"
9 "strings"
10)
11
12//dataEntry is the map used to create the array of maps, where the templatejson data is stored
13type dataEntry map[string]string
14
15func readFile(folderPath string, filename string) string {
16 dat, err := ioutil.ReadFile(folderPath + "/" + filename)
17 check(err)
18 return string(dat)
19}
20
21func getDataFromJson(path string) []dataEntry {
22 var entries []dataEntry
23 file, err := ioutil.ReadFile(path)
24 check(err)
25 content := string(file)
26 var rawEntries []*json.RawMessage
27 json.Unmarshal([]byte(content), &rawEntries)
28 for i := 0; i < len(rawEntries); i++ {
29 rawEntryMarshaled, err := json.Marshal(rawEntries[i])
30 check(err)
31 var newDataEntry map[string]string
32 json.Unmarshal(rawEntryMarshaled, &newDataEntry)
33 entries = append(entries, newDataEntry)
34 }
35
36 return entries
37}
38
39func generateFromTemplateAndData(templateContent string, entries []dataEntry) string {
40
41 var newContent string
42
43 for i := 0; i < len(entries); i++ {
44 var entryContent string
45 entryContent = templateContent
46 //first, get the map keys
47 var keys []string
48 for key, _ := range entries[i] {
49 keys = append(keys, key)
50 }
51 //now, replace the keys with the values
52 for j := 0; j < len(keys); j++ {
53 entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entries[i][keys[j]], -1)
54 entryContent = strings.Replace(entryContent, "[[i]]", strconv.Itoa(i), -1)
55 }
56
57 newContent = newContent + entryContent
58 }
59 return newContent
60}
61func getTemplateParameters(line string) (string, string) {
62 var templatePath string
63 var data string
64 line = strings.Replace(line, "<konstrui-template ", "", -1)
65 line = strings.Replace(line, "></konstrui-template>", "", -1)
66 attributes := strings.Split(line, " ")
67 //fmt.Println(attributes)
68 for i := 0; i < len(attributes); i++ {
69 attSplitted := strings.Split(attributes[i], "=")
70 if attSplitted[0] == "html" {
71 templatePath = strings.Replace(attSplitted[1], `"`, "", -1)
72 }
73 if attSplitted[0] == "data" {
74 data = strings.Replace(attSplitted[1], `"`, "", -1)
75 }
76 }
77 return templatePath, data
78}
79
80func useTemplate(templatePath string, dataPath string) string {
81 templateContent := readFile(rawFolderPath, templatePath)
82 entries := getDataFromJson(rawFolderPath + "/" + dataPath)
83 generated := generateFromTemplateAndData(templateContent, entries)
84 return generated
85}
86
87func putTemplates(folderPath string, filename string) string {
88 var fileContent string
89 f, err := os.Open(folderPath + "/" + filename)
90 check(err)
91 scanner := bufio.NewScanner(f)
92 lineCount := 1
93 for scanner.Scan() {
94 currentLine := scanner.Text()
95 if strings.Contains(currentLine, "<konstrui-template") && strings.Contains(currentLine, "</konstrui-template>") {
96 templatePath, data := getTemplateParameters(currentLine)
97 fileContent = fileContent + useTemplate(templatePath, data)
98 } else {
99 fileContent = fileContent + currentLine
100 }
101 lineCount++
102 }
103 return fileContent
104}
105
106func writeFile(path string, newContent string) {
107 err := ioutil.WriteFile(path, []byte(newContent), 0644)
108 check(err)
109}