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.

100 lines
2.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. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "strings"
  8. "github.com/Jeffail/gabs"
  9. "github.com/fatih/color"
  10. )
  11. //dataEntry is the map used to create the array of maps, where the templatejson data is stored
  12. type dataEntry map[string]string
  13. func copyDirRaw(fromDir string, currentDir string, newDir string) {
  14. filesList, _ := ioutil.ReadDir("./" + fromDir + "/" + currentDir)
  15. fmt.Println(fromDir + "/" + currentDir)
  16. c.Green(newDir + "/" + currentDir)
  17. os.MkdirAll(newDir+"/"+currentDir, os.ModePerm)
  18. for _, f := range filesList {
  19. fileNameSplitted := strings.Split(f.Name(), ".")
  20. if len(fileNameSplitted) > 1 {
  21. //is a file
  22. copyFileRaw(fromDir+"/"+currentDir, f.Name(), newDir+"/"+currentDir)
  23. } else {
  24. //is a directory
  25. copyDirRaw(fromDir+"/"+currentDir, f.Name(), newDir+"/"+currentDir)
  26. }
  27. }
  28. }
  29. func copyFileRaw(fromDir string, fName string, newDir string) {
  30. c.Yellow("copying raw " + fromDir + "/" + fName)
  31. fileContent := readFile(fromDir + "/" + fName)
  32. writeFile(newDir+"/"+fName, fileContent)
  33. }
  34. func readFile(path string) string {
  35. dat, err := ioutil.ReadFile(path)
  36. check(err)
  37. return string(dat)
  38. }
  39. func getDataFromJson(path string) ([]dataEntry, *gabs.Container) {
  40. var entries []dataEntry
  41. file, err := ioutil.ReadFile(path)
  42. check(err)
  43. content := string(file)
  44. var rawEntries []*json.RawMessage
  45. json.Unmarshal([]byte(content), &rawEntries)
  46. for i := 0; i < len(rawEntries); i++ {
  47. rawEntryMarshaled, err := json.Marshal(rawEntries[i])
  48. check(err)
  49. var newDataEntry map[string]string
  50. json.Unmarshal(rawEntryMarshaled, &newDataEntry)
  51. entries = append(entries, newDataEntry)
  52. }
  53. jsonData := jsonGabs(path)
  54. return entries, jsonData
  55. }
  56. func jsonGabs(path string) *gabs.Container {
  57. file, err := ioutil.ReadFile(path)
  58. check(err)
  59. jsonParsed, err := gabs.ParseJSON(file)
  60. //img := "img"
  61. //fmt.Println(jsonParsed.S(img))
  62. return jsonParsed
  63. }
  64. func writeFile(path string, newContent string) {
  65. err := ioutil.WriteFile(path, []byte(newContent), 0644)
  66. check(err)
  67. color.Green(path + ":")
  68. color.Blue(newContent)
  69. }
  70. /*func generatePageFromTemplateAndData(templateContent string, entry dataEntry) string {
  71. var entryContent string
  72. entryContent = templateContent
  73. //first, get the map keys
  74. var keys []string
  75. for key, _ := range entry {
  76. keys = append(keys, key)
  77. }
  78. //now, replace the keys with the values
  79. for j := 0; j < len(keys); j++ {
  80. entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
  81. }
  82. return entryContent
  83. }*/
  84. func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry, *gabs.Container) {
  85. templateContent := readFile(rawFolderPath + "/" + page.HtmlPage)
  86. data, jsonData := getDataFromJson(rawFolderPath + "/" + page.Data)
  87. return templateContent, data, jsonData
  88. }