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.

259 lines
9.5 KiB

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