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 extracted := extractText(templateContent, "<konstrui-repeatElem", "</konstrui-repeatElem>")
103 fragmentLines := getLines(extracted)
104 fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeatElem")
105 f := concatStringsWithJumps(fragmentLines)
106 children, _ := jsonData.S(elemName).Children()
107 var replaced string
108 for _, child := range children {
109 fmt.Println(child.Data().(string))
110 replacedElem := strings.Replace(f, "{{"+elemName+"}}", child.Data().(string), -1)
111 replaced = replaced + replacedElem
112 }
113 fragmentLines = getLines(replaced)
114
115 lines := getLines(templateContent)
116 p := locateStringInArray(lines, "konstrui-repeatElem")
117 lines = deleteLinesBetween(lines, p[0], p[1])
118 lines = addElementsToArrayPosition(lines, fragmentLines, p[0])
119 templateContent = concatStringsWithJumps(lines)
120
121 }
122 return templateContent
123}
124
125func konstruiSimpleVars(template string, entries []dataEntry, jsonData *gabs.Container) string {
126 //now, replace simple templating variables {{vars}}
127 /*for _, entry := range entries {
128 template = replaceEntry(template, entry, jsonData, "")
129 }*/
130 /*children, _ := jsonData.S().Children()
131 for _, child := range children {
132 var entry dataEntry
133 fmt.Println("aaaaa")
134 fmt.Println(child)
135 template = replaceEntry(template, entry, child, "")
136 }
137 */
138 var entry dataEntry
139 template = replaceEntry(template, entry, jsonData, "")
140 return template
141}
142func konstruiInclude(content string) string {
143 var result string
144 if strings.Contains(content, "<konstrui-include") {
145 lines := getLines(content)
146 for _, line := range lines {
147 if strings.Contains(line, "<konstrui-include") {
148 dataPath, _ := getTagParameters(line, "<konstrui-include", "html", "nil")
149 dataPath = strings.Replace(dataPath, "\n", "", -1)
150 htmlInclude := readFile(rawFolderPath + "/" + dataPath)
151 result = result + htmlInclude
152 } else {
153 result = result + line + "\n"
154 }
155 }
156
157 /*dataPath, _ := getTagParameters(content, "<konstrui-include", "html", "nil")
158 dataPath = strings.Replace(dataPath, "\n", "", -1)
159 htmlInclude := readFile(rawFolderPath + "/" + dataPath)
160 htmlIncludeLines := getLines(htmlInclude)
161 contentLines := getLines(content)
162 p := locateStringInArray(contentLines, "<konstrui-include")
163 fmt.Println(p)
164 contentLines = deleteLinesBetween(contentLines, p[0], p[0])
165 contentLines = addElementsToArrayPosition(contentLines, htmlIncludeLines, p[0])
166 content = concatStringsWithJumps(contentLines)*/
167 } else {
168 result = content
169 }
170 return result
171}
172func konstruiTemplate(templateContent string) string {
173 var result string
174 lines := getLines(templateContent)
175 for _, line := range lines {
176 if strings.Contains(line, "<konstrui-template") && strings.Contains(line, "</konstrui-template>") {
177 templatePath, data := getTagParameters(line, "konstrui-template", "html", "data")
178 result = result + useKonstruiTemplate(templatePath, data) + "\n"
179 } else {
180 result = result + line + "\n"
181 }
182 }
183 return result
184}
185func useKonstruiTemplate(templatePath string, dataPath string) string {
186 filepath := rawFolderPath + "/" + templatePath
187 templateContent := readFile(filepath)
188 entries, jsonData := getDataFromJson(rawFolderPath + "/" + dataPath)
189 generated := konstruiRepeatJSONPartTwo(templateContent, entries, jsonData, "")
190 generated = konstruiSimpleVars(generated, entries, jsonData)
191 return generated
192}
193func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
194 var param1content string
195 var param2content string
196 line = strings.Replace(line, "<"+tagname+" ", "", -1)
197 line = strings.Replace(line, "></"+tagname+">", "", -1)
198 attributes := strings.Split(line, " ")
199 for i := 0; i < len(attributes); i++ {
200 attSplitted := strings.Split(attributes[i], "=")
201 if attSplitted[0] == param1 {
202 param1content = strings.Replace(attSplitted[1], `"`, "", -1)
203 param1content = strings.Replace(param1content, ">", "", -1)
204 }
205 if attSplitted[0] == param2 {
206 param2content = strings.Replace(attSplitted[1], `"`, "", -1)
207 param2content = strings.Replace(param2content, ">", "", -1)
208 }
209 }
210 return param1content, param2content
211}
212
213func startTemplating(folderPath string, newDir string) {
214 //FILES
215 //do templating for each file in konstruiConfig.Files
216 //konstrui-template
217 for i := 0; i < len(konstruiConfig.Files); i++ {
218 fName := konstruiConfig.Files[i]
219 fileContent := readFile(folderPath + "/" + fName)
220 fileContent = konstruiTemplate(fileContent)
221 generatedPage := konstruiRepeatJSON(fileContent)
222 generatedPage = konstruiInclude(generatedPage)
223 writeFile(newDir+"/"+fName, generatedPage)
224 }
225 //REPEATPAGES
226 //do templating for the file pages in konstruiConfig.RepeatPages
227 c.Cyan("starting to generate Pages to repeat")
228 for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
229 pageTemplate, _, jsonData := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
230 //for j := 0; j < len(data); j++ {
231 children, _ := jsonData.S().Children()
232 for _, child := range children {
233 var dataArray []dataEntry
234 var dat dataEntry
235 //dataArray = append(dataArray, data[j])
236 generatedPage := konstruiRepeatJSONPartTwo(pageTemplate, dataArray, child, "")
237 generatedPage = konstruiRepeatElem(generatedPage, dat, child)
238 generatedPage = konstruiSimpleVars(generatedPage, dataArray, child)
239 generatedPage = konstruiInclude(generatedPage)
240 //writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
241 pageName, _ := child.Path("pageName").Data().(string)
242 writeFile(newDir+"/"+pageName+"Page.html", generatedPage)
243 }
244 }
245 //COPYRAW
246 //copy the konstruiConfig.CopyRaw files without modificate them
247 for i := 0; i < len(konstruiConfig.CopyRaw); i++ {
248 fName := konstruiConfig.CopyRaw[i]
249 c.Yellow(fName)
250 fileNameSplitted := strings.Split(fName, ".")
251 if len(fileNameSplitted) > 1 {
252 //is a file
253 copyFileRaw(folderPath, fName, newDir)
254 } else {
255 //is a directory
256 c.Red(folderPath + "/" + fName)
257 copyDirRaw(folderPath, fName, newDir)
258 }
259 }
260}