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.

242 lines
8.8 KiB

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