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.

130 lines
3.7 KiB

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. "io/ioutil"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. //dataEntry is the map used to create the array of maps, where the templatejson data is stored
  11. type dataEntry map[string]string
  12. func readFile(path string) string {
  13. dat, err := ioutil.ReadFile(path)
  14. check(err)
  15. return string(dat)
  16. }
  17. func getDataFromJson(path string) []dataEntry {
  18. var entries []dataEntry
  19. file, err := ioutil.ReadFile(path)
  20. check(err)
  21. content := string(file)
  22. var rawEntries []*json.RawMessage
  23. json.Unmarshal([]byte(content), &rawEntries)
  24. for i := 0; i < len(rawEntries); i++ {
  25. rawEntryMarshaled, err := json.Marshal(rawEntries[i])
  26. check(err)
  27. var newDataEntry map[string]string
  28. json.Unmarshal(rawEntryMarshaled, &newDataEntry)
  29. entries = append(entries, newDataEntry)
  30. }
  31. return entries
  32. }
  33. func generateFromTemplateAndData(templateContent string, entries []dataEntry) string {
  34. var newContent string
  35. for i := 0; i < len(entries); i++ {
  36. var entryContent string
  37. entryContent = templateContent
  38. //first, get the map keys
  39. var keys []string
  40. for key, _ := range entries[i] {
  41. keys = append(keys, key)
  42. }
  43. //now, replace the keys with the values
  44. for j := 0; j < len(keys); j++ {
  45. entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entries[i][keys[j]], -1)
  46. entryContent = strings.Replace(entryContent, "[[i]]", strconv.Itoa(i), -1)
  47. }
  48. newContent = newContent + entryContent
  49. }
  50. return newContent
  51. }
  52. func getTemplateParameters(line string) (string, string) {
  53. var templatePath string
  54. var data string
  55. line = strings.Replace(line, "<konstrui-template ", "", -1)
  56. line = strings.Replace(line, "></konstrui-template>", "", -1)
  57. attributes := strings.Split(line, " ")
  58. //fmt.Println(attributes)
  59. for i := 0; i < len(attributes); i++ {
  60. attSplitted := strings.Split(attributes[i], "=")
  61. if attSplitted[0] == "html" {
  62. templatePath = strings.Replace(attSplitted[1], `"`, "", -1)
  63. }
  64. if attSplitted[0] == "data" {
  65. data = strings.Replace(attSplitted[1], `"`, "", -1)
  66. }
  67. }
  68. return templatePath, data
  69. }
  70. func useTemplate(templatePath string, dataPath string) string {
  71. filepath := rawFolderPath + "/" + templatePath
  72. templateContent := readFile(filepath)
  73. entries := getDataFromJson(rawFolderPath + "/" + dataPath)
  74. generated := generateFromTemplateAndData(templateContent, entries)
  75. return generated
  76. }
  77. func putTemplates(folderPath string, filename string) string {
  78. var fileContent string
  79. f, err := os.Open(folderPath + "/" + filename)
  80. check(err)
  81. scanner := bufio.NewScanner(f)
  82. lineCount := 1
  83. for scanner.Scan() {
  84. currentLine := scanner.Text()
  85. if strings.Contains(currentLine, "<konstrui-template") && strings.Contains(currentLine, "</konstrui-template>") {
  86. templatePath, data := getTemplateParameters(currentLine)
  87. fileContent = fileContent + useTemplate(templatePath, data)
  88. } else {
  89. fileContent = fileContent + currentLine
  90. }
  91. lineCount++
  92. }
  93. return fileContent
  94. }
  95. func writeFile(path string, newContent string) {
  96. err := ioutil.WriteFile(path, []byte(newContent), 0644)
  97. check(err)
  98. }
  99. func generatePageFromTemplateAndData(templateContent string, entry dataEntry) string {
  100. var entryContent string
  101. entryContent = templateContent
  102. //first, get the map keys
  103. var keys []string
  104. for key, _ := range entry {
  105. keys = append(keys, key)
  106. }
  107. //now, replace the keys with the values
  108. for j := 0; j < len(keys); j++ {
  109. entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
  110. }
  111. return entryContent
  112. }
  113. func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry) {
  114. templateContent := readFile(rawFolderPath + "/" + page.HtmlPage)
  115. data := getDataFromJson(rawFolderPath + "/" + page.Data)
  116. return templateContent, data
  117. }