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