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