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.

266 lines
9.8 KiB

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