konstrui/fileOperations.go
fileOperations.goRaw
1package main
2
3import (
4 "bufio"
5 "encoding/json"
6 "fmt"
7 "io/ioutil"
8 "os"
9 "strings"
10
11 "github.com/fatih/color"
12)
13
14//dataEntry is the map used to create the array of maps, where the templatejson data is stored
15type dataEntry map[string]string
16
17func readFile(path string) string {
18 dat, err := ioutil.ReadFile(path)
19 check(err)
20 return string(dat)
21}
22
23func getDataFromJson(path string) []dataEntry {
24 var entries []dataEntry
25 file, err := ioutil.ReadFile(path)
26 check(err)
27 content := string(file)
28 var rawEntries []*json.RawMessage
29 json.Unmarshal([]byte(content), &rawEntries)
30 for i := 0; i < len(rawEntries); i++ {
31 rawEntryMarshaled, err := json.Marshal(rawEntries[i])
32 check(err)
33 var newDataEntry map[string]string
34 json.Unmarshal(rawEntryMarshaled, &newDataEntry)
35 entries = append(entries, newDataEntry)
36 }
37
38 return entries
39}
40func duplicateText(original string, count int) string {
41 var result string
42 for i := 0; i < count; i++ {
43 result = result + original
44 }
45 return result
46}
47func replaceEntry(templateContent string, entry dataEntry) string {
48 //var newContent string
49
50 //replace {{}} for data
51 //for i := 0; i < len(entries); i++ {
52 //var entryContent string
53 //entryContent = templateContent
54 //first, get the map keys
55 var keys []string
56 for key, _ := range entry {
57 keys = append(keys, key)
58 }
59 //now, replace the keys with the values
60 for j := 0; j < len(keys); j++ {
61 templateContent = strings.Replace(templateContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
62 //templateContent = strings.Replace(templateContent, "[[i]]", strconv.Itoa(i), -1)
63 }
64 //newContent = newContent + entryContent
65 //newContent = newContent + "\n"
66 //}
67 return templateContent
68}
69func putDataInTemplate(templateContent string, entries []dataEntry) string {
70 var newContent string
71 newContent = templateContent
72
73 //replace <konstrui-repeat>
74 if strings.Contains(newContent, "<konstrui-repeat") && strings.Contains(newContent, "</konstrui-repeat>") {
75 //repeat, _ := getTagParameters(newContent, "konstrui-repeat", "repeat", "nil")
76
77 //get content inside tags
78 //get tags, and split by tags, get the content between tags
79 extracted := extractText(newContent, "<konstrui-repeat", "</konstrui-repeat>")
80 //for each project, putDataInTemplate data:entries, template: content inside tags
81
82 //var fragment string
83 var replaced string
84 for _, entry := range entries {
85 color.Green(extracted)
86 fmt.Println(entry)
87 replaced = replaced + replaceEntry(extracted, entry)
88 }
89 fragmentLines := getLines(replaced)
90 fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeat")
91 //afegir fragment al newContent, substituint el fragment original
92 lines := getLines(templateContent)
93 p := locateStringInArray(lines, "konstrui-repeat")
94 lines = deleteLinesBetween(lines, p[0], p[1])
95 lines = addElementsToArrayPosition(lines, fragmentLines, p[0])
96 /*lines = deleteArrayElementsWithString(lines, "konstrui-repeat")
97 fmt.Println(lines)*/
98 templateContent = concatStringsWithJumps(lines)
99 fmt.Println(templateContent)
100 newContent = templateContent
101 }
102 color.Red(newContent)
103 result := templateContent
104 for _, entry := range entries {
105 result = replaceEntry(result, entry)
106 }
107 color.Blue(result)
108
109 return result
110}
111func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
112 var param1content string
113 var param2content string
114 line = strings.Replace(line, "<"+tagname+" ", "", -1)
115 line = strings.Replace(line, "></"+tagname+">", "", -1)
116 attributes := strings.Split(line, " ")
117 //fmt.Println(attributes)
118 for i := 0; i < len(attributes); i++ {
119 attSplitted := strings.Split(attributes[i], "=")
120 if attSplitted[0] == param1 {
121 param1content = strings.Replace(attSplitted[1], `"`, "", -1)
122 param1content = strings.Replace(param1content, ">", "", -1)
123 }
124 if attSplitted[0] == param2 {
125 param2content = strings.Replace(attSplitted[1], `"`, "", -1)
126 param2content = strings.Replace(param2content, ">", "", -1)
127 }
128 }
129 return param1content, param2content
130}
131
132func useTemplate(templatePath string, dataPath string) string {
133 filepath := rawFolderPath + "/" + templatePath
134 templateContent := readFile(filepath)
135 entries := getDataFromJson(rawFolderPath + "/" + dataPath)
136 generated := putDataInTemplate(templateContent, entries)
137 return generated
138}
139func useTemplateContent(templateContent string, dataPath string) string {
140 entries := getDataFromJson(rawFolderPath + "/" + dataPath)
141 generated := putDataInTemplate(templateContent, entries)
142 return generated
143}
144
145func putTemplates(folderPath string, filename string) string {
146 var fileContent string
147 f, err := os.Open(folderPath + "/" + filename)
148 check(err)
149 scanner := bufio.NewScanner(f)
150 lineCount := 1
151 for scanner.Scan() {
152 currentLine := scanner.Text()
153 if strings.Contains(currentLine, "<konstrui-template") && strings.Contains(currentLine, "</konstrui-template>") {
154 templatePath, data := getTagParameters(currentLine, "konstrui-template", "html", "data")
155 fileContent = fileContent + useTemplate(templatePath, data) + "\n"
156 } else {
157 fileContent = fileContent + currentLine + "\n"
158 }
159 lineCount++
160 }
161
162 if strings.Contains(fileContent, "<konstrui-repeat") {
163 dataPath, _ := getTagParameters(fileContent, "konstrui-repeat", "repeat", "nil")
164 dataPath = strings.Replace(dataPath, "\n", "", -1)
165 fileContent = useTemplateContent(fileContent, dataPath)
166 color.Red(fileContent)
167 }
168 return fileContent
169}
170
171func writeFile(path string, newContent string) {
172 err := ioutil.WriteFile(path, []byte(newContent), 0644)
173 check(err)
174}
175
176/*func generatePageFromTemplateAndData(templateContent string, entry dataEntry) string {
177 var entryContent string
178 entryContent = templateContent
179 //first, get the map keys
180 var keys []string
181 for key, _ := range entry {
182 keys = append(keys, key)
183 }
184 //now, replace the keys with the values
185 for j := 0; j < len(keys); j++ {
186 entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
187 }
188 return entryContent
189}*/
190func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry) {
191 templateContent := readFile(rawFolderPath + "/" + page.HtmlPage)
192 data := getDataFromJson(rawFolderPath + "/" + page.Data)
193 return templateContent, data
194}