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