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