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.

296 lines
11 KiB

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