konstrui/templating.go
templating.goRaw
1package main
2
3import (
4 "strings"
5)
6
7func duplicateText(original string, count int) string {
8 var result string
9 for i := 0; i < count; i++ {
10 result = result + original
11 }
12 return result
13}
14func replaceEntry(templateContent string, entry dataEntry) string {
15 //replace {{}} with data
16 var keys []string
17 for key, _ := range entry {
18 keys = append(keys, key)
19 }
20 //now, replace the keys with the values
21 for j := 0; j < len(keys); j++ {
22 templateContent = strings.Replace(templateContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
23 //templateContent = strings.Replace(templateContent, "[[i]]", strconv.Itoa(i), -1)
24 }
25 return templateContent
26}
27func konstruiRepeatPartTwo(templateContent string, entries []dataEntry) string {
28 var newContent string
29 newContent = templateContent
30
31 //replace <konstrui-repeat>
32 if strings.Contains(newContent, "<konstrui-repeat") && strings.Contains(newContent, "</konstrui-repeat>") {
33 //get content inside tags
34 //get tags, and split by tags, get the content between tags
35 extracted := extractText(newContent, "<konstrui-repeat", "</konstrui-repeat>")
36 //for each project, putDataInTemplate data:entries, template: content inside tags
37
38 var replaced string
39 for _, entry := range entries {
40 replaced = replaced + replaceEntry(extracted, entry)
41 }
42 fragmentLines := getLines(replaced)
43 fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeat")
44 //afegir fragment al newContent, substituint el fragment original
45 lines := getLines(templateContent)
46 p := locateStringInArray(lines, "konstrui-repeat")
47 lines = deleteLinesBetween(lines, p[0], p[1])
48 lines = addElementsToArrayPosition(lines, fragmentLines, p[0])
49 templateContent = concatStringsWithJumps(lines)
50 }
51 return templateContent
52}
53func konstruiRepeat(templateContent string) string {
54 if strings.Contains(templateContent, "<konstrui-repeat") {
55 dataPath, _ := getTagParameters(templateContent, "konstrui-repeat", "repeatJSON", "nil")
56 dataPath = strings.Replace(dataPath, "\n", "", -1)
57 entries := getDataFromJson(rawFolderPath + "/" + dataPath)
58 templateContent = konstruiRepeatPartTwo(templateContent, entries)
59 }
60 return templateContent
61}
62func konstruiSimpleVars(template string, entries []dataEntry) string {
63 //now, replace simple templating variables {{vars}}
64 for _, entry := range entries {
65 template = replaceEntry(template, entry)
66 }
67 return template
68}
69func konstruiTemplate(templateContent string) string {
70 var result string
71 lines := getLines(templateContent)
72 for _, line := range lines {
73 if strings.Contains(line, "<konstrui-template") && strings.Contains(line, "</konstrui-template>") {
74 templatePath, data := getTagParameters(line, "konstrui-template", "html", "data")
75 result = result + useKonstruiTemplate(templatePath, data) + "\n"
76 } else {
77 result = result + line + "\n"
78 }
79 }
80 return result
81}
82func useKonstruiTemplate(templatePath string, dataPath string) string {
83 filepath := rawFolderPath + "/" + templatePath
84 templateContent := readFile(filepath)
85 entries := getDataFromJson(rawFolderPath + "/" + dataPath)
86 generated := konstruiRepeatPartTwo(templateContent, entries)
87 generated = konstruiSimpleVars(generated, entries)
88 return generated
89}
90func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
91 var param1content string
92 var param2content string
93 line = strings.Replace(line, "<"+tagname+" ", "", -1)
94 line = strings.Replace(line, "></"+tagname+">", "", -1)
95 attributes := strings.Split(line, " ")
96 for i := 0; i < len(attributes); i++ {
97 attSplitted := strings.Split(attributes[i], "=")
98 if attSplitted[0] == param1 {
99 param1content = strings.Replace(attSplitted[1], `"`, "", -1)
100 param1content = strings.Replace(param1content, ">", "", -1)
101 }
102 if attSplitted[0] == param2 {
103 param2content = strings.Replace(attSplitted[1], `"`, "", -1)
104 param2content = strings.Replace(param2content, ">", "", -1)
105 }
106 }
107 return param1content, param2content
108}
109
110func startTemplating(folderPath string, newDir string) {
111 //FILES
112 //do templating for each file in konstruiConfig.Files
113 //konstrui-template
114 for i := 0; i < len(konstruiConfig.Files); i++ {
115 fName := konstruiConfig.Files[i]
116 fileContent := readFile(folderPath + "/" + fName)
117 fileContent = konstruiTemplate(fileContent)
118 generatedPage := konstruiRepeat(fileContent)
119 writeFile(newDir+"/"+fName, generatedPage)
120 }
121 //REPEATPAGES
122 //do templating for the file pages in konstruiConfig.RepeatPages
123 c.Cyan("starting to generate Pages to repeat")
124 for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
125 pageTemplate, data := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
126 for j := 0; j < len(data); j++ {
127 var dataArray []dataEntry
128 dataArray = append(dataArray, data[j])
129 generatedPage := konstruiRepeatPartTwo(pageTemplate, dataArray)
130 generatedPage = konstruiSimpleVars(generatedPage, dataArray)
131 writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
132 }
133 }
134 //COPYRAW
135 //copy the konstruiConfig.CopyRaw files without modificate them
136 for i := 0; i < len(konstruiConfig.CopyRaw); i++ {
137 fName := konstruiConfig.CopyRaw[i]
138 c.Yellow(fName)
139 fileNameSplitted := strings.Split(fName, ".")
140 if len(fileNameSplitted) > 1 {
141 //is a file
142 copyFileRaw(folderPath, fName, newDir)
143 } else {
144 //is a directory
145 c.Red(folderPath + "/" + fName)
146 copyDirRaw(folderPath, fName, newDir)
147 }
148 }
149}