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.

158 lines
4.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package main
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "github.com/fatih/color"
  11. )
  12. //dataEntry is the map used to create the array of maps, where the templatejson data is stored
  13. type dataEntry map[string]string
  14. func readFile(path string) string {
  15. dat, err := ioutil.ReadFile(path)
  16. check(err)
  17. return string(dat)
  18. }
  19. func getDataFromJson(path string) []dataEntry {
  20. var entries []dataEntry
  21. file, err := ioutil.ReadFile(path)
  22. check(err)
  23. content := string(file)
  24. var rawEntries []*json.RawMessage
  25. json.Unmarshal([]byte(content), &rawEntries)
  26. for i := 0; i < len(rawEntries); i++ {
  27. rawEntryMarshaled, err := json.Marshal(rawEntries[i])
  28. check(err)
  29. var newDataEntry map[string]string
  30. json.Unmarshal(rawEntryMarshaled, &newDataEntry)
  31. entries = append(entries, newDataEntry)
  32. }
  33. return entries
  34. }
  35. func replaceEntries(templateContent string, entries []dataEntry) string {
  36. var newContent string
  37. //replace {{}} for data
  38. for i := 0; i < len(entries); i++ {
  39. var entryContent string
  40. entryContent = templateContent
  41. //first, get the map keys
  42. var keys []string
  43. for key, _ := range entries[i] {
  44. keys = append(keys, key)
  45. }
  46. //now, replace the keys with the values
  47. for j := 0; j < len(keys); j++ {
  48. entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entries[i][keys[j]], -1)
  49. entryContent = strings.Replace(entryContent, "[[i]]", strconv.Itoa(i), -1)
  50. }
  51. newContent = newContent + entryContent
  52. }
  53. return newContent
  54. }
  55. func putDataInTemplate(templateContent string, entries []dataEntry) string {
  56. var newContent string
  57. newContent = templateContent
  58. //replace <konstrui-repeat>
  59. if strings.Contains(newContent, "<konstrui-repeat") && strings.Contains(newContent, "</konstrui-repeat>") {
  60. //repeat, _ := getTagParameters(newContent, "konstrui-repeat", "repeat", "nil")
  61. color.Blue("repeat data")
  62. //fmt.Println(repeat)
  63. //get content inside tags
  64. //get tags, and split by tags, get the content between tags
  65. extracted := extractText(newContent, "<konstrui-repeat", "</konstrui-repeat>")
  66. fmt.Println(extracted)
  67. //for each project, putDataInTemplate data:entries, template: content inside tags
  68. fragment := replaceEntries(extracted, entries)
  69. color.Blue(fragment)
  70. //afegir fragment al newContent
  71. //esborrar les línies dels tags
  72. }
  73. newContent = replaceEntries(templateContent, entries)
  74. return newContent
  75. }
  76. func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
  77. var param1content string
  78. var param2content string
  79. line = strings.Replace(line, "<"+tagname+" ", "", -1)
  80. line = strings.Replace(line, "></"+tagname+">", "", -1)
  81. attributes := strings.Split(line, " ")
  82. //fmt.Println(attributes)
  83. for i := 0; i < len(attributes); i++ {
  84. attSplitted := strings.Split(attributes[i], "=")
  85. if attSplitted[0] == param1 {
  86. param1content = strings.Replace(attSplitted[1], `"`, "", -1)
  87. param1content = strings.Replace(param1content, ">", "", -1)
  88. }
  89. if attSplitted[0] == param2 {
  90. param2content = strings.Replace(attSplitted[1], `"`, "", -1)
  91. param2content = strings.Replace(param2content, ">", "", -1)
  92. }
  93. }
  94. return param1content, param2content
  95. }
  96. func useTemplate(templatePath string, dataPath string) string {
  97. filepath := rawFolderPath + "/" + templatePath
  98. templateContent := readFile(filepath)
  99. entries := getDataFromJson(rawFolderPath + "/" + dataPath)
  100. generated := putDataInTemplate(templateContent, entries)
  101. return generated
  102. }
  103. func putTemplates(folderPath string, filename string) string {
  104. var fileContent string
  105. f, err := os.Open(folderPath + "/" + filename)
  106. check(err)
  107. scanner := bufio.NewScanner(f)
  108. lineCount := 1
  109. for scanner.Scan() {
  110. currentLine := scanner.Text()
  111. if strings.Contains(currentLine, "<konstrui-template") && strings.Contains(currentLine, "</konstrui-template>") {
  112. templatePath, data := getTagParameters(currentLine, "konstrui-template", "html", "data")
  113. fileContent = fileContent + useTemplate(templatePath, data)
  114. } else {
  115. fileContent = fileContent + currentLine
  116. }
  117. lineCount++
  118. }
  119. return fileContent
  120. }
  121. func writeFile(path string, newContent string) {
  122. err := ioutil.WriteFile(path, []byte(newContent), 0644)
  123. check(err)
  124. }
  125. /*func generatePageFromTemplateAndData(templateContent string, entry dataEntry) string {
  126. var entryContent string
  127. entryContent = templateContent
  128. //first, get the map keys
  129. var keys []string
  130. for key, _ := range entry {
  131. keys = append(keys, key)
  132. }
  133. //now, replace the keys with the values
  134. for j := 0; j < len(keys); j++ {
  135. entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
  136. }
  137. return entryContent
  138. }*/
  139. func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry) {
  140. templateContent := readFile(rawFolderPath + "/" + page.HtmlPage)
  141. data := getDataFromJson(rawFolderPath + "/" + page.Data)
  142. return templateContent, data
  143. }