You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 lines
7.7 KiB

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