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	fmt.Println(children)
 47	fmt.Println(ok)
 48	fmt.Println("-")
 49	for parameter, child := range children {
 50		subchildren, _ := child.S().ChildrenMap()
 51		_, ok := child.S().Children()
 52		fmt.Println(parameter)
 53		fmt.Println(child)
 54		fmt.Println(ok)
 55		if ok != nil {
 56			color.Green("child fin")
 57			color.Blue(child.Data().(string))
 58			fmt.Println(child)
 59			templateContent = strings.Replace(templateContent, "{{"+parameter+"}}", child.Data().(string), -1)
 60		} else {
 61			for subparameter, subchild := range subchildren {
 62				color.Red(subchild.Data().(string))
 63				fmt.Println(subchild)
 64				fmt.Println(subparameter)
 65				//templateContent = strings.Replace(templateContent, "{{"+subparameter+"}}", subchild.Data().(string), -1)
 66			}
 67		}
 68
 69	}
 70	return templateContent
 71}
 72func konstruiRepeatJSONPartTwo(templateContent string, entries []dataEntry, jsonData *gabs.Container, elemName string) string {
 73	var newContent string
 74	newContent = templateContent
 75
 76	//replace <konstrui-repeatJSON>
 77	if strings.Contains(newContent, "<konstrui-repeatJSON") && strings.Contains(newContent, "</konstrui-repeatJSON>") {
 78		//get content inside tags
 79		//get tags, and split by tags, get the content between tags
 80		extracted := extractText(newContent, "<konstrui-repeatJSON", "</konstrui-repeatJSON>")
 81		//for each project, putDataInTemplate data:entries, template: content inside tags
 82		var replaced string
 83		//for _, entry := range entries {
 84		children, _ := jsonData.S().Children()
 85		for _, child := range children {
 86			var entry dataEntry
 87			color.Red("BBBBB")
 88			replaced = replaced + replaceEntry(extracted, entry, child, elemName)
 89		}
 90		fragmentLines := getLines(replaced)
 91		fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeatJSON")
 92		//afegir fragment al newContent, substituint el fragment original
 93		lines := getLines(templateContent)
 94		p := locateStringInArray(lines, "konstrui-repeatJSON")
 95		lines = deleteLinesBetween(lines, p[0], p[1])
 96		lines = addElementsToArrayPosition(lines, fragmentLines, p[0])
 97		templateContent = concatStringsWithJumps(lines)
 98	}
 99
100	return templateContent
101}
102func konstruiRepeatJSON(templateContent string) string {
103	if strings.Contains(templateContent, "<konstrui-repeatJSON") {
104		dataPath, _ := getTagParameters(templateContent, "konstrui-repeatJSON", "repeatJSON", "nil")
105		dataPath = strings.Replace(dataPath, "\n", "", -1)
106		entries, jsonData := getDataFromJson(rawFolderPath + "/" + dataPath)
107		templateContent = konstruiRepeatJSONPartTwo(templateContent, entries, jsonData, "")
108	}
109	return templateContent
110}
111func konstruiRepeatElem(templateContent string, entry dataEntry, jsonData *gabs.Container) string {
112	if strings.Contains(templateContent, "<konstrui-repeatElem") {
113		elemName, _ := getTagParameters(templateContent, "konstrui-repeatElem", "repeatElem", "nil")
114		color.Red(elemName)
115		elemEntries := getElemFromObj(entry, elemName)
116		templateContent = konstruiRepeatJSONPartTwo(templateContent, elemEntries, jsonData, elemName)
117	}
118	return templateContent
119}
120func getElemFromObj(entry dataEntry, elemName string) []dataEntry {
121	var elemEntries []dataEntry
122	fmt.Println(elemName)
123	fmt.Println(entry)
124	return elemEntries
125}
126func konstruiSimpleVars(template string, entries []dataEntry, jsonData *gabs.Container) string {
127	//now, replace simple templating variables {{vars}}
128	for _, entry := range entries {
129		template = replaceEntry(template, entry, jsonData, "")
130	}
131	return template
132}
133func konstruiTemplate(templateContent string) string {
134	var result string
135	lines := getLines(templateContent)
136	for _, line := range lines {
137		if strings.Contains(line, "<konstrui-template") && strings.Contains(line, "</konstrui-template>") {
138			templatePath, data := getTagParameters(line, "konstrui-template", "html", "data")
139			result = result + useKonstruiTemplate(templatePath, data) + "\n"
140		} else {
141			result = result + line + "\n"
142		}
143	}
144	return result
145}
146func useKonstruiTemplate(templatePath string, dataPath string) string {
147	filepath := rawFolderPath + "/" + templatePath
148	templateContent := readFile(filepath)
149	entries, jsonData := getDataFromJson(rawFolderPath + "/" + dataPath)
150	generated := konstruiRepeatJSONPartTwo(templateContent, entries, jsonData, "")
151	generated = konstruiSimpleVars(generated, entries, jsonData)
152	return generated
153}
154func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
155	var param1content string
156	var param2content string
157	line = strings.Replace(line, "<"+tagname+" ", "", -1)
158	line = strings.Replace(line, "></"+tagname+">", "", -1)
159	attributes := strings.Split(line, " ")
160	for i := 0; i < len(attributes); i++ {
161		attSplitted := strings.Split(attributes[i], "=")
162		if attSplitted[0] == param1 {
163			param1content = strings.Replace(attSplitted[1], `"`, "", -1)
164			param1content = strings.Replace(param1content, ">", "", -1)
165		}
166		if attSplitted[0] == param2 {
167			param2content = strings.Replace(attSplitted[1], `"`, "", -1)
168			param2content = strings.Replace(param2content, ">", "", -1)
169		}
170	}
171	return param1content, param2content
172}
173
174func startTemplating(folderPath string, newDir string) {
175	//FILES
176	//do templating for each file in konstruiConfig.Files
177	//konstrui-template
178	for i := 0; i < len(konstruiConfig.Files); i++ {
179		fName := konstruiConfig.Files[i]
180		fileContent := readFile(folderPath + "/" + fName)
181		fileContent = konstruiTemplate(fileContent)
182		generatedPage := konstruiRepeatJSON(fileContent)
183		writeFile(newDir+"/"+fName, generatedPage)
184	}
185	//REPEATPAGES
186	//do templating for the file pages in konstruiConfig.RepeatPages
187	c.Cyan("starting to generate Pages to repeat")
188	for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
189		pageTemplate, data, jsonData := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
190		for j := 0; j < len(data); j++ {
191			var dataArray []dataEntry
192			dataArray = append(dataArray, data[j])
193			generatedPage := konstruiRepeatJSONPartTwo(pageTemplate, dataArray, jsonData, "")
194			generatedPage = konstruiRepeatElem(generatedPage, dataArray[0], jsonData)
195			generatedPage = konstruiSimpleVars(generatedPage, dataArray, jsonData)
196			writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
197		}
198	}
199	//COPYRAW
200	//copy the konstruiConfig.CopyRaw files without modificate them
201	for i := 0; i < len(konstruiConfig.CopyRaw); i++ {
202		fName := konstruiConfig.CopyRaw[i]
203		c.Yellow(fName)
204		fileNameSplitted := strings.Split(fName, ".")
205		if len(fileNameSplitted) > 1 {
206			//is a file
207			copyFileRaw(folderPath, fName, newDir)
208		} else {
209			//is a directory
210			c.Red(folderPath + "/" + fName)
211			copyDirRaw(folderPath, fName, newDir)
212		}
213	}
214}