konstrui/templating.go
templating.goRaw
1package main
2
3import (
4 "fmt"
5 "strconv"
6 "strings"
7
8 "github.com/Jeffail/gabs"
9)
10
11func duplicateText(original string, count int) string {
12 var result string
13 for i := 0; i < count; i++ {
14 result = result + original
15 }
16 return result
17}
18func replaceEntryOLD(templateContent string, entry dataEntry, jsonData *gabs.Container, elemName string) string {
19 //replace {{}} with data
20 var keys []string
21 for key, _ := range entry {
22 keys = append(keys, key)
23 }
24 //now, replace the keys with the values
25 for j := 0; j < len(keys); j++ {
26 templateContent = strings.Replace(templateContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
27 //templateContent = strings.Replace(templateContent, "[[i]]", strconv.Itoa(i), -1)
28 children, _ := jsonData.S(keys[j]).Children()
29 fmt.Println("-")
30 fmt.Println(keys[j])
31 fmt.Println(children)
32 for key, child := range children {
33 fmt.Print("key: " + strconv.Itoa(key) + ", value: ")
34 fmt.Println(child.Data().(string))
35 }
36 }
37
38 return templateContent
39}
40func replaceEntry(templateContent string, entry dataEntry, jsonData *gabs.Container, elemName string) string {
41 //fmt.Println(jsonData)
42 children, _ := jsonData.S().ChildrenMap()
43 //_, ok := jsonData.S().Children()
44 for parameter, child := range children {
45 //subchildren, _ := child.S().ChildrenMap()
46 _, ok := child.S().Children()
47 if ok != nil {
48 templateContent = strings.Replace(templateContent, "{{"+parameter+"}}", child.Data().(string), -1)
49 } else {
50 /*for subparameter, subchild := range subchildren {
51 color.Red(subchild.Data().(string))
52 fmt.Println(subchild)
53 fmt.Println(subparameter)
54 //templateContent = strings.Replace(templateContent, "{{"+subparameter+"}}", subchild.Data().(string), -1)
55 }*/
56 }
57
58 }
59 return templateContent
60}
61func konstruiRepeatJSONPartTwo(templateContent string, entries []dataEntry, jsonData *gabs.Container, elemName string) string {
62 var newContent string
63 newContent = templateContent
64
65 //replace <konstrui-repeatJSON>
66 if strings.Contains(newContent, "<konstrui-repeatJSON") && strings.Contains(newContent, "</konstrui-repeatJSON>") {
67 //get content inside tags
68 //get tags, and split by tags, get the content between tags
69 extracted := extractText(newContent, "<konstrui-repeatJSON", "</konstrui-repeatJSON>")
70 //for each project, putDataInTemplate data:entries, template: content inside tags
71 var replaced string
72 //for _, entry := range entries {
73 children, _ := jsonData.S().Children()
74 for _, child := range children {
75 var entry dataEntry
76 replaced = replaced + replaceEntry(extracted, entry, child, elemName)
77 }
78 fragmentLines := getLines(replaced)
79 fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeatJSON")
80 //afegir fragment al newContent, substituint el fragment original
81 lines := getLines(templateContent)
82 p := locateStringInArray(lines, "konstrui-repeatJSON")
83 lines = deleteLinesBetween(lines, p[0], p[1])
84 lines = addElementsToArrayPosition(lines, fragmentLines, p[0])
85 templateContent = concatStringsWithJumps(lines)
86 }
87
88 return templateContent
89}
90func konstruiRepeatJSON(templateContent string) string {
91 if strings.Contains(templateContent, "<konstrui-repeatJSON") {
92 dataPath, _ := getTagParameters(templateContent, "konstrui-repeatJSON", "repeatJSON", "nil")
93 dataPath = strings.Replace(dataPath, "\n", "", -1)
94 entries, jsonData := getDataFromJson(rawFolderPath + "/" + dataPath)
95 templateContent = konstruiRepeatJSONPartTwo(templateContent, entries, jsonData, "")
96 }
97 return templateContent
98}
99func konstruiRepeatElem(templateContent string, entry dataEntry, jsonData *gabs.Container) string {
100 if strings.Contains(templateContent, "<konstrui-repeatElem") {
101 elemName, _ := getTagParameters(templateContent, "konstrui-repeatElem", "repeatElem", "nil")
102 //elemEntries := getElemFromObj(entry, elemName)
103 //templateContent = konstruiRepeatJSONPartTwo(templateContent, elemEntries, jsonData, elemName)
104 extracted := extractText(templateContent, "<konstrui-repeatElem", "</konstrui-repeatElem>")
105 children, _ := jsonData.S(elemName).Children()
106 /*for _, child := range children {
107 //var entry dataEntry
108 fmt.Println(child)
109 //replaced = replaced + replaceEntry(extracted, entry, child, elemName)
110 replaced = replaced + extracted
111 }*/
112 fragmentLines := getLines(extracted)
113 fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeatElem")
114 f := concatStringsWithJumps(fragmentLines)
115
116 var replaced string
117 for _, child := range children {
118 replacedElem := strings.Replace(f, "{{"+elemName+"}}", child.Data().(string), -1)
119 replaced = replaced + replacedElem
120 }
121 fragmentLines = getLines(replaced)
122
123 lines := getLines(templateContent)
124 p := locateStringInArray(lines, "konstrui-repeatElem")
125 lines = deleteLinesBetween(lines, p[0], p[1])
126 lines = addElementsToArrayPosition(lines, fragmentLines, p[0])
127 templateContent = concatStringsWithJumps(lines)
128
129 }
130 return templateContent
131}
132
133/*func getElemFromObj(entry dataEntry, elemName string) []dataEntry {
134 var elemEntries []dataEntry
135 fmt.Println(elemName)
136 fmt.Println(entry)
137 return elemEntries
138}*/
139func konstruiSimpleVars(template string, entries []dataEntry, jsonData *gabs.Container) string {
140 //now, replace simple templating variables {{vars}}
141 /*for _, entry := range entries {
142 template = replaceEntry(template, entry, jsonData, "")
143 }*/
144 /*children, _ := jsonData.S().Children()
145 for _, child := range children {
146 var entry dataEntry
147 fmt.Println("aaaaa")
148 fmt.Println(child)
149 template = replaceEntry(template, entry, child, "")
150 }
151 */
152 var entry dataEntry
153 template = replaceEntry(template, entry, jsonData, "")
154 return template
155}
156func konstruiTemplate(templateContent string) string {
157 var result string
158 lines := getLines(templateContent)
159 for _, line := range lines {
160 if strings.Contains(line, "<konstrui-template") && strings.Contains(line, "</konstrui-template>") {
161 templatePath, data := getTagParameters(line, "konstrui-template", "html", "data")
162 result = result + useKonstruiTemplate(templatePath, data) + "\n"
163 } else {
164 result = result + line + "\n"
165 }
166 }
167 return result
168}
169func useKonstruiTemplate(templatePath string, dataPath string) string {
170 filepath := rawFolderPath + "/" + templatePath
171 templateContent := readFile(filepath)
172 entries, jsonData := getDataFromJson(rawFolderPath + "/" + dataPath)
173 generated := konstruiRepeatJSONPartTwo(templateContent, entries, jsonData, "")
174 generated = konstruiSimpleVars(generated, entries, jsonData)
175 return generated
176}
177func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
178 var param1content string
179 var param2content string
180 line = strings.Replace(line, "<"+tagname+" ", "", -1)
181 line = strings.Replace(line, "></"+tagname+">", "", -1)
182 attributes := strings.Split(line, " ")
183 for i := 0; i < len(attributes); i++ {
184 attSplitted := strings.Split(attributes[i], "=")
185 if attSplitted[0] == param1 {
186 param1content = strings.Replace(attSplitted[1], `"`, "", -1)
187 param1content = strings.Replace(param1content, ">", "", -1)
188 }
189 if attSplitted[0] == param2 {
190 param2content = strings.Replace(attSplitted[1], `"`, "", -1)
191 param2content = strings.Replace(param2content, ">", "", -1)
192 }
193 }
194 return param1content, param2content
195}
196
197func startTemplating(folderPath string, newDir string) {
198 //FILES
199 //do templating for each file in konstruiConfig.Files
200 //konstrui-template
201 for i := 0; i < len(konstruiConfig.Files); i++ {
202 fName := konstruiConfig.Files[i]
203 fileContent := readFile(folderPath + "/" + fName)
204 fileContent = konstruiTemplate(fileContent)
205 generatedPage := konstruiRepeatJSON(fileContent)
206 writeFile(newDir+"/"+fName, generatedPage)
207 }
208 //REPEATPAGES
209 //do templating for the file pages in konstruiConfig.RepeatPages
210 c.Cyan("starting to generate Pages to repeat")
211 for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
212 pageTemplate, _, jsonData := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
213 //for j := 0; j < len(data); j++ {
214 children, _ := jsonData.S().Children()
215 for _, child := range children {
216 var dataArray []dataEntry
217 var dat dataEntry
218 //dataArray = append(dataArray, data[j])
219 generatedPage := konstruiRepeatJSONPartTwo(pageTemplate, dataArray, child, "")
220 generatedPage = konstruiRepeatElem(generatedPage, dat, child)
221 generatedPage = konstruiSimpleVars(generatedPage, dataArray, child)
222 //writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
223 pageName, _ := child.Path("pageName").Data().(string)
224 writeFile(newDir+"/"+pageName+"Page.html", generatedPage)
225 }
226 }
227 //COPYRAW
228 //copy the konstruiConfig.CopyRaw files without modificate them
229 for i := 0; i < len(konstruiConfig.CopyRaw); i++ {
230 fName := konstruiConfig.CopyRaw[i]
231 c.Yellow(fName)
232 fileNameSplitted := strings.Split(fName, ".")
233 if len(fileNameSplitted) > 1 {
234 //is a file
235 copyFileRaw(folderPath, fName, newDir)
236 } else {
237 //is a directory
238 c.Red(folderPath + "/" + fName)
239 copyDirRaw(folderPath, fName, newDir)
240 }
241 }
242}