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.

113 lines
3.6 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. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "strings"
  7. )
  8. const rawFolderPath = "./webInput"
  9. const newFolderPath = "./webOutput"
  10. const konstruiConfigFile = "konstruiConfig.json"
  11. func parseDir(folderPath string, newDir string) {
  12. files, _ := ioutil.ReadDir(folderPath)
  13. for _, f := range files {
  14. fileNameSplitted := strings.Split(f.Name(), ".")
  15. extension := fileNameSplitted[len(fileNameSplitted)-1]
  16. if extension == "html" {
  17. fileContent := putTemplates(folderPath, f.Name())
  18. writeFile(newDir+"/"+f.Name(), fileContent)
  19. } else if extension == "css" {
  20. path := folderPath + "/" + f.Name()
  21. fileContent := readFile(path)
  22. writeFile(newDir+"/"+f.Name(), fileContent)
  23. }
  24. if len(fileNameSplitted) == 1 {
  25. newDir := newDir + "/" + f.Name()
  26. oldDir := rawFolderPath + "/" + f.Name()
  27. if _, err := os.Stat(newDir); os.IsNotExist(err) {
  28. _ = os.Mkdir(newDir, 0700)
  29. }
  30. parseDir(oldDir, newDir)
  31. }
  32. }
  33. }
  34. func startTemplating(folderPath string, newDir string) {
  35. //do templating for each file in konstruiConfig.Files in konstruiConfig.Files
  36. for i := 0; i < len(konstruiConfig.Files); i++ {
  37. fName := konstruiConfig.Files[i]
  38. fileNameSplitted := strings.Split(fName, ".")
  39. extension := fileNameSplitted[len(fileNameSplitted)-1]
  40. if extension == "html" {
  41. fileContent := putTemplates(folderPath, fName)
  42. writeFile(newDir+"/"+fName, fileContent)
  43. } else if extension == "css" {
  44. path := folderPath + "/" + fName
  45. fileContent := readFile(path)
  46. writeFile(newDir+"/"+fName, fileContent)
  47. }
  48. }
  49. //do templating for the file pages in konstruiConfig.RepeatPages
  50. c.Cyan("starting to generate Pages to repeat")
  51. for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
  52. pageTemplate, data := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
  53. for j := 0; j < len(data); j++ {
  54. fmt.Println(j)
  55. generatedPage := generatePageFromTemplateAndData(pageTemplate, data[j])
  56. fmt.Println(data[j])
  57. writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
  58. }
  59. }
  60. //copy the konstruiConfig.CopyRaw files without modificate them
  61. for i := 0; i < len(konstruiConfig.CopyRaw); i++ {
  62. fName := konstruiConfig.CopyRaw[i]
  63. c.Yellow(fName)
  64. fileNameSplitted := strings.Split(fName, ".")
  65. if len(fileNameSplitted) > 1 {
  66. //is a file
  67. copyFileRaw(folderPath, fName, newDir)
  68. } else {
  69. //is a directory
  70. c.Red(folderPath + "/" + fName)
  71. copyDirRaw(folderPath, fName, newDir)
  72. }
  73. }
  74. }
  75. func copyDirRaw(fromDir string, currentDir string, newDir string) {
  76. filesList, _ := ioutil.ReadDir("./" + fromDir + "/" + currentDir)
  77. fmt.Println(fromDir + "/" + currentDir)
  78. c.Green(newDir + "/" + currentDir)
  79. os.MkdirAll(newDir+"/"+currentDir, os.ModePerm)
  80. for _, f := range filesList {
  81. fileNameSplitted := strings.Split(f.Name(), ".")
  82. if len(fileNameSplitted) > 1 {
  83. //is a file
  84. copyFileRaw(fromDir+"/"+currentDir, f.Name(), newDir+"/"+currentDir)
  85. } else {
  86. //is a directory
  87. copyDirRaw(fromDir+"/"+currentDir, f.Name(), newDir+"/"+currentDir)
  88. }
  89. }
  90. }
  91. func copyFileRaw(fromDir string, fName string, newDir string) {
  92. c.Yellow("copying raw " + fromDir + "//" + fName)
  93. fileContent := readFile(fromDir + "/" + fName)
  94. writeFile(newDir+"/"+fName, fileContent)
  95. }
  96. func main() {
  97. c.Green("getting files from /webInput")
  98. c.Green("getting conifg from file konstruiConfig.json")
  99. //first reads the konstrui.Config.json
  100. readKonstruiConfig(rawFolderPath + "/" + konstruiConfigFile)
  101. c.Green("configuration:")
  102. fmt.Println(konstruiConfig.Files)
  103. c.Green("templating")
  104. //parseDir(rawFolderPath, newFolderPath)
  105. startTemplating(rawFolderPath, newFolderPath)
  106. c.Green("webpage finished, files at /webOutput")
  107. }