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.

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