arnaucube
arnaucube/konstruiPublic Self-hosted repository
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 replaceVariable(templateContent string, variable string, value string) string {
 40	templateContent = strings.Replace(templateContent, "{{"+variable+"}}", value, -1)
 41	return templateContent
 42}
 43func replaceEntry(templateContent string, entry dataEntry, jsonData *gabs.Container, elemName string) string {
 44	//fmt.Println(jsonData)
 45	children, _ := jsonData.S().ChildrenMap()
 46	//_, ok := jsonData.S().Children()
 47	for parameter, child := range children {
 48		//subchildren, _ := child.S().ChildrenMap()
 49		_, ok := child.S().Children()
 50		if ok != nil {
 51			templateContent = strings.Replace(templateContent, "{{"+parameter+"}}", child.Data().(string), -1)
 52		} else {
 53			/*for subparameter, subchild := range subchildren {
 54				color.Red(subchild.Data().(string))
 55				fmt.Println(subchild)
 56				fmt.Println(subparameter)
 57				//templateContent = strings.Replace(templateContent, "{{"+subparameter+"}}", subchild.Data().(string), -1)
 58			}*/
 59		}
 60
 61	}
 62	return templateContent
 63}
 64func konstruiRepeatArray(templateContent string) string {
 65	for strings.Contains(templateContent, "<konstrui-repeatArray") {
 66		tlines := getLines(templateContent)
 67		tp := locateStringInArray(tlines, "konstrui-repeatArray")
 68		dataPath, _ := getTagParameters(tlines[tp[0]], "konstrui-repeatArray", "repeatArray", "nil")
 69		dataPath = strings.Replace(dataPath, "\n", "", -1)
 70		_, jsonData := getDataFromJson(rawFolderPath + "/" + dataPath)
 71		//get the part of the html where is the konstrui-repeatArray
 72		extracted := extractText(templateContent, "<konstrui-repeatArray", "</konstrui-repeatArray>")
 73		var replaced string
 74		children, _ := jsonData.S().Children()
 75		for _, child := range children {
 76			replaced = replaced + replaceVariable(extracted, "var", child.Data().(string))
 77		}
 78
 79		fragmentLines := getLines(replaced)
 80		fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeatArray")
 81		//afegir fragment al newContent, substituint el fragment original
 82		lines := getLines(templateContent)
 83		p := locateStringInArray(lines, "konstrui-repeatArray")
 84		lines = deleteLinesBetween(lines, p[0], p[1])
 85		lines = addElementsToArrayPosition(lines, fragmentLines, p[0])
 86		templateContent = concatStringsWithJumps(lines)
 87	}
 88	return templateContent
 89}
 90func konstruiRepeatJSONPartTwo(templateContent string, entries []dataEntry, jsonData *gabs.Container, elemName string) string {
 91	var newContent string
 92	newContent = templateContent
 93
 94	//replace <konstrui-repeatJSON>
 95	if strings.Contains(newContent, "<konstrui-repeatJSON") && strings.Contains(newContent, "</konstrui-repeatJSON>") {
 96		//get content inside tags
 97		//get tags, and split by tags, get the content between tags
 98		extracted := extractText(newContent, "<konstrui-repeatJSON", "</konstrui-repeatJSON>")
 99		//for each project, putDataInTemplate data:entries, template: content inside tags
100		var replaced string
101		//for _, entry := range entries {
102		children, _ := jsonData.S().Children()
103		for _, child := range children {
104			var entry dataEntry
105			replaced = replaced + replaceEntry(extracted, entry, child, elemName)
106		}
107		fragmentLines := getLines(replaced)
108		fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeatJSON")
109		//afegir fragment al newContent, substituint el fragment original
110		lines := getLines(templateContent)
111		p := locateStringInArray(lines, "konstrui-repeatJSON")
112		lines = deleteLinesBetween(lines, p[0], p[1])
113		lines = addElementsToArrayPosition(lines, fragmentLines, p[0])
114		templateContent = concatStringsWithJumps(lines)
115	}
116
117	return templateContent
118}
119func konstruiRepeatJSON(templateContent string) string {
120	for strings.Contains(templateContent, "<konstrui-repeatJSON") {
121		tlines := getLines(templateContent)
122		tp := locateStringInArray(tlines, "konstrui-repeatJSON")
123		dataPath, _ := getTagParameters(tlines[tp[0]], "konstrui-repeatJSON", "repeatJSON", "nil")
124		dataPath = strings.Replace(dataPath, "\n", "", -1)
125		entries, jsonData := getDataFromJson(rawFolderPath + "/" + dataPath)
126		templateContent = konstruiRepeatJSONPartTwo(templateContent, entries, jsonData, "")
127	}
128	return templateContent
129}
130func konstruiRepeatElem(templateContent string, entry dataEntry, jsonData *gabs.Container) string {
131	for strings.Contains(templateContent, "<konstrui-repeatElem") {
132		tlines := getLines(templateContent)
133		tp := locateStringInArray(tlines, "konstrui-repeatElem")
134		elemName, _ := getTagParameters(tlines[tp[0]], "konstrui-repeatElem", "repeatElem", "nil")
135
136		extracted := extractText(templateContent, "<konstrui-repeatElem", "</konstrui-repeatElem>")
137		fragmentLines := getLines(extracted)
138		fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeatElem")
139		f := concatStringsWithJumps(fragmentLines)
140		children, _ := jsonData.S(elemName).Children()
141		var replaced string
142		for _, child := range children {
143			//fmt.Println(child)
144			//fmt.Println(child.Data().(string))
145			replacedElem := strings.Replace(f, "{{"+elemName+"}}", child.Data().(string), -1)
146			replaced = replaced + replacedElem
147		}
148		fragmentLines = getLines(replaced)
149		//fmt.Println(replaced)
150
151		/*lines := getLines(templateContent)
152		p := locateStringInArray(lines, "konstrui-repeatElem")*/
153		tlines = deleteLinesBetween(tlines, tp[0], tp[1])
154		tlines = addElementsToArrayPosition(tlines, fragmentLines, tp[0])
155		templateContent = concatStringsWithJumps(tlines)
156	}
157	return templateContent
158}
159
160func konstruiSimpleVars(template string, entries []dataEntry, jsonData *gabs.Container) string {
161	//now, replace simple templating variables {{vars}}
162	/*for _, entry := range entries {
163		template = replaceEntry(template, entry, jsonData, "")
164	}*/
165	/*children, _ := jsonData.S().Children()
166	for _, child := range children {
167		var entry dataEntry
168		fmt.Println("aaaaa")
169		fmt.Println(child)
170		template = replaceEntry(template, entry, child, "")
171	}
172	*/
173	var entry dataEntry
174	template = replaceEntry(template, entry, jsonData, "")
175	return template
176}
177func konstruiInclude(content string) string {
178	var result string
179	if strings.Contains(content, "<konstrui-include") {
180		lines := getLines(content)
181		for _, line := range lines {
182			if strings.Contains(line, "<konstrui-include") {
183				dataPath, _ := getTagParameters(line, "<konstrui-include", "html", "nil")
184				dataPath = strings.Replace(dataPath, "\n", "", -1)
185				htmlInclude := readFile(rawFolderPath + "/" + dataPath)
186				result = result + htmlInclude
187			} else {
188				result = result + line + "\n"
189			}
190		}
191
192		/*dataPath, _ := getTagParameters(content, "<konstrui-include", "html", "nil")
193		dataPath = strings.Replace(dataPath, "\n", "", -1)
194		htmlInclude := readFile(rawFolderPath + "/" + dataPath)
195		htmlIncludeLines := getLines(htmlInclude)
196		contentLines := getLines(content)
197		p := locateStringInArray(contentLines, "<konstrui-include")
198		fmt.Println(p)
199		contentLines = deleteLinesBetween(contentLines, p[0], p[0])
200		contentLines = addElementsToArrayPosition(contentLines, htmlIncludeLines, p[0])
201		content = concatStringsWithJumps(contentLines)*/
202	} else {
203		result = content
204	}
205	return result
206}
207func konstruiTemplate(templateContent string) string {
208	var result string
209	lines := getLines(templateContent)
210	for _, line := range lines {
211		if strings.Contains(line, "<konstrui-template") && strings.Contains(line, "</konstrui-template>") {
212			templatePath, data := getTagParameters(line, "konstrui-template", "html", "data")
213			result = result + useKonstruiTemplate(templatePath, data) + "\n"
214		} else {
215			result = result + line + "\n"
216		}
217	}
218	return result
219}
220func useKonstruiTemplate(templatePath string, dataPath string) string {
221	filepath := rawFolderPath + "/" + templatePath
222	templateContent := readFile(filepath)
223	entries, jsonData := getDataFromJson(rawFolderPath + "/" + dataPath)
224	generated := konstruiRepeatJSONPartTwo(templateContent, entries, jsonData, "")
225	generated = konstruiSimpleVars(generated, entries, jsonData)
226	return generated
227}
228func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
229	var param1content string
230	var param2content string
231	line = strings.Replace(line, "<"+tagname+" ", "", -1)
232	line = strings.Replace(line, "></"+tagname+">", "", -1)
233	attributes := strings.Split(line, " ")
234	for i := 0; i < len(attributes); i++ {
235		attSplitted := strings.Split(attributes[i], "=")
236		if attSplitted[0] == param1 {
237			param1content = strings.Replace(attSplitted[1], `"`, "", -1)
238			param1content = strings.Replace(param1content, ">", "", -1)
239		}
240		if attSplitted[0] == param2 {
241			param2content = strings.Replace(attSplitted[1], `"`, "", -1)
242			param2content = strings.Replace(param2content, ">", "", -1)
243		}
244	}
245	return param1content, param2content
246}
247
248func startTemplating(folderPath string, newDir string) {
249	//FILES
250	//do templating for each file in konstruiConfig.Files
251	//konstrui-template
252	for i := 0; i < len(konstruiConfig.Files); i++ {
253		fName := konstruiConfig.Files[i]
254		fileContent := readFile(folderPath + "/" + fName)
255		fileContent = konstruiTemplate(fileContent)
256		generatedPage := konstruiRepeatJSON(fileContent)
257		generatedPage = konstruiInclude(generatedPage)
258		generatedPage = konstruiRepeatArray(generatedPage)
259		writeFile(newDir+"/"+fName, generatedPage)
260	}
261	//REPEATPAGES
262	//do templating for the file pages in konstruiConfig.RepeatPages
263	c.Cyan("starting to generate Pages to repeat")
264	for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
265		pageTemplate, _, jsonData := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
266		//for j := 0; j < len(data); j++ {
267		children, _ := jsonData.S().Children()
268		for _, child := range children {
269			var dataArray []dataEntry
270			var dat dataEntry
271			//dataArray = append(dataArray, data[j])
272			generatedPage := konstruiRepeatJSONPartTwo(pageTemplate, dataArray, child, "")
273			generatedPage = konstruiRepeatElem(generatedPage, dat, child)
274			generatedPage = konstruiSimpleVars(generatedPage, dataArray, child)
275			generatedPage = konstruiInclude(generatedPage)
276			//writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
277			pageName, _ := child.Path("pageName").Data().(string)
278			writeFile(newDir+"/"+pageName+"Page.html", generatedPage)
279		}
280	}
281	//COPYRAW
282	//copy the konstruiConfig.CopyRaw files without modificate them
283	for i := 0; i < len(konstruiConfig.CopyRaw); i++ {
284		fName := konstruiConfig.CopyRaw[i]
285		c.Yellow(fName)
286		fileNameSplitted := strings.Split(fName, ".")
287		if len(fileNameSplitted) > 1 {
288			//is a file
289			copyFileRaw(folderPath, fName, newDir)
290		} else {
291			//is a directory
292			c.Red(folderPath + "/" + fName)
293			copyDirRaw(folderPath, fName, newDir)
294		}
295	}
296}