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.

264 lines
9.7 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. if strings.Contains(templateContent, "<konstrui-repeatJSON") {
  84. dataPath, _ := getTagParameters(templateContent, "konstrui-repeatJSON", "repeatJSON", "nil")
  85. dataPath = strings.Replace(dataPath, "\n", "", -1)
  86. entries, jsonData := getDataFromJson(rawFolderPath + "/" + dataPath)
  87. templateContent = konstruiRepeatJSONPartTwo(templateContent, entries, jsonData, "")
  88. }
  89. return templateContent
  90. }
  91. func konstruiRepeatElem(templateContent string, entry dataEntry, jsonData *gabs.Container) string {
  92. for strings.Contains(templateContent, "<konstrui-repeatElem") {
  93. tlines := getLines(templateContent)
  94. tp := locateStringInArray(tlines, "konstrui-repeatElem")
  95. elemName, _ := getTagParameters(tlines[tp[0]], "konstrui-repeatElem", "repeatElem", "nil")
  96. extracted := extractText(templateContent, "<konstrui-repeatElem", "</konstrui-repeatElem>")
  97. fragmentLines := getLines(extracted)
  98. fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeatElem")
  99. f := concatStringsWithJumps(fragmentLines)
  100. children, _ := jsonData.S(elemName).Children()
  101. var replaced string
  102. for _, child := range children {
  103. fmt.Println(child)
  104. fmt.Println(child.Data().(string))
  105. replacedElem := strings.Replace(f, "{{"+elemName+"}}", child.Data().(string), -1)
  106. replaced = replaced + replacedElem
  107. }
  108. fragmentLines = getLines(replaced)
  109. fmt.Println(replaced)
  110. /*lines := getLines(templateContent)
  111. p := locateStringInArray(lines, "konstrui-repeatElem")*/
  112. tlines = deleteLinesBetween(tlines, tp[0], tp[1])
  113. tlines = addElementsToArrayPosition(tlines, fragmentLines, tp[0])
  114. templateContent = concatStringsWithJumps(tlines)
  115. }
  116. return templateContent
  117. }
  118. func konstruiSimpleVars(template string, entries []dataEntry, jsonData *gabs.Container) string {
  119. //now, replace simple templating variables {{vars}}
  120. /*for _, entry := range entries {
  121. template = replaceEntry(template, entry, jsonData, "")
  122. }*/
  123. /*children, _ := jsonData.S().Children()
  124. for _, child := range children {
  125. var entry dataEntry
  126. fmt.Println("aaaaa")
  127. fmt.Println(child)
  128. template = replaceEntry(template, entry, child, "")
  129. }
  130. */
  131. var entry dataEntry
  132. template = replaceEntry(template, entry, jsonData, "")
  133. return template
  134. }
  135. func konstruiInclude(content string) string {
  136. var result string
  137. if strings.Contains(content, "<konstrui-include") {
  138. lines := getLines(content)
  139. for _, line := range lines {
  140. if strings.Contains(line, "<konstrui-include") {
  141. dataPath, _ := getTagParameters(line, "<konstrui-include", "html", "nil")
  142. dataPath = strings.Replace(dataPath, "\n", "", -1)
  143. htmlInclude := readFile(rawFolderPath + "/" + dataPath)
  144. result = result + htmlInclude
  145. } else {
  146. result = result + line + "\n"
  147. }
  148. }
  149. /*dataPath, _ := getTagParameters(content, "<konstrui-include", "html", "nil")
  150. dataPath = strings.Replace(dataPath, "\n", "", -1)
  151. htmlInclude := readFile(rawFolderPath + "/" + dataPath)
  152. htmlIncludeLines := getLines(htmlInclude)
  153. contentLines := getLines(content)
  154. p := locateStringInArray(contentLines, "<konstrui-include")
  155. fmt.Println(p)
  156. contentLines = deleteLinesBetween(contentLines, p[0], p[0])
  157. contentLines = addElementsToArrayPosition(contentLines, htmlIncludeLines, p[0])
  158. content = concatStringsWithJumps(contentLines)*/
  159. } else {
  160. result = content
  161. }
  162. return result
  163. }
  164. func konstruiTemplate(templateContent string) string {
  165. var result string
  166. lines := getLines(templateContent)
  167. for _, line := range lines {
  168. if strings.Contains(line, "<konstrui-template") && strings.Contains(line, "</konstrui-template>") {
  169. templatePath, data := getTagParameters(line, "konstrui-template", "html", "data")
  170. result = result + useKonstruiTemplate(templatePath, data) + "\n"
  171. } else {
  172. result = result + line + "\n"
  173. }
  174. }
  175. return result
  176. }
  177. func useKonstruiTemplate(templatePath string, dataPath string) string {
  178. filepath := rawFolderPath + "/" + templatePath
  179. templateContent := readFile(filepath)
  180. entries, jsonData := getDataFromJson(rawFolderPath + "/" + dataPath)
  181. generated := konstruiRepeatJSONPartTwo(templateContent, entries, jsonData, "")
  182. generated = konstruiSimpleVars(generated, entries, jsonData)
  183. return generated
  184. }
  185. func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
  186. var param1content string
  187. var param2content string
  188. line = strings.Replace(line, "<"+tagname+" ", "", -1)
  189. line = strings.Replace(line, "></"+tagname+">", "", -1)
  190. attributes := strings.Split(line, " ")
  191. for i := 0; i < len(attributes); i++ {
  192. attSplitted := strings.Split(attributes[i], "=")
  193. if attSplitted[0] == param1 {
  194. param1content = strings.Replace(attSplitted[1], `"`, "", -1)
  195. param1content = strings.Replace(param1content, ">", "", -1)
  196. }
  197. if attSplitted[0] == param2 {
  198. param2content = strings.Replace(attSplitted[1], `"`, "", -1)
  199. param2content = strings.Replace(param2content, ">", "", -1)
  200. }
  201. }
  202. return param1content, param2content
  203. }
  204. func startTemplating(folderPath string, newDir string) {
  205. //FILES
  206. //do templating for each file in konstruiConfig.Files
  207. //konstrui-template
  208. for i := 0; i < len(konstruiConfig.Files); i++ {
  209. fName := konstruiConfig.Files[i]
  210. fileContent := readFile(folderPath + "/" + fName)
  211. fileContent = konstruiTemplate(fileContent)
  212. generatedPage := konstruiRepeatJSON(fileContent)
  213. generatedPage = konstruiInclude(generatedPage)
  214. writeFile(newDir+"/"+fName, generatedPage)
  215. }
  216. //REPEATPAGES
  217. //do templating for the file pages in konstruiConfig.RepeatPages
  218. c.Cyan("starting to generate Pages to repeat")
  219. for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
  220. pageTemplate, _, jsonData := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
  221. //for j := 0; j < len(data); j++ {
  222. children, _ := jsonData.S().Children()
  223. for _, child := range children {
  224. var dataArray []dataEntry
  225. var dat dataEntry
  226. //dataArray = append(dataArray, data[j])
  227. generatedPage := konstruiRepeatJSONPartTwo(pageTemplate, dataArray, child, "")
  228. generatedPage = konstruiRepeatElem(generatedPage, dat, child)
  229. generatedPage = konstruiSimpleVars(generatedPage, dataArray, child)
  230. generatedPage = konstruiInclude(generatedPage)
  231. //writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
  232. pageName, _ := child.Path("pageName").Data().(string)
  233. writeFile(newDir+"/"+pageName+"Page.html", generatedPage)
  234. }
  235. }
  236. //COPYRAW
  237. //copy the konstruiConfig.CopyRaw files without modificate them
  238. for i := 0; i < len(konstruiConfig.CopyRaw); i++ {
  239. fName := konstruiConfig.CopyRaw[i]
  240. c.Yellow(fName)
  241. fileNameSplitted := strings.Split(fName, ".")
  242. if len(fileNameSplitted) > 1 {
  243. //is a file
  244. copyFileRaw(folderPath, fName, newDir)
  245. } else {
  246. //is a directory
  247. c.Red(folderPath + "/" + fName)
  248. copyDirRaw(folderPath, fName, newDir)
  249. }
  250. }
  251. }