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.

107 lines
2.9 KiB

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