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.

119 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
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. //konstrui-template
  37. for i := 0; i < len(konstruiConfig.Files); i++ {
  38. fName := konstruiConfig.Files[i]
  39. fileNameSplitted := strings.Split(fName, ".")
  40. extension := fileNameSplitted[len(fileNameSplitted)-1]
  41. if extension == "html" {
  42. fileContent := putTemplates(folderPath, fName)
  43. writeFile(newDir+"/"+fName, fileContent)
  44. } else if extension == "css" {
  45. path := folderPath + "/" + fName
  46. fileContent := readFile(path)
  47. writeFile(newDir+"/"+fName, fileContent)
  48. }
  49. }
  50. //do templating for the file pages in konstruiConfig.RepeatPages
  51. c.Cyan("starting to generate Pages to repeat")
  52. for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
  53. pageTemplate, data := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
  54. for j := 0; j < len(data); j++ {
  55. //fmt.Println(j)
  56. var dataArray []dataEntry
  57. dataArray = append(dataArray, data[j])
  58. generatedPage := putDataInTemplate(pageTemplate, dataArray)
  59. //fmt.Println(data[j])
  60. writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
  61. }
  62. }
  63. //copy the konstruiConfig.CopyRaw files without modificate them
  64. for i := 0; i < len(konstruiConfig.CopyRaw); i++ {
  65. fName := konstruiConfig.CopyRaw[i]
  66. c.Yellow(fName)
  67. fileNameSplitted := strings.Split(fName, ".")
  68. if len(fileNameSplitted) > 1 {
  69. //is a file
  70. copyFileRaw(folderPath, fName, newDir)
  71. } else {
  72. //is a directory
  73. c.Red(folderPath + "/" + fName)
  74. copyDirRaw(folderPath, fName, newDir)
  75. }
  76. }
  77. }
  78. func copyDirRaw(fromDir string, currentDir string, newDir string) {
  79. filesList, _ := ioutil.ReadDir("./" + fromDir + "/" + currentDir)
  80. fmt.Println(fromDir + "/" + currentDir)
  81. c.Green(newDir + "/" + currentDir)
  82. os.MkdirAll(newDir+"/"+currentDir, os.ModePerm)
  83. for _, f := range filesList {
  84. fileNameSplitted := strings.Split(f.Name(), ".")
  85. if len(fileNameSplitted) > 1 {
  86. //is a file
  87. copyFileRaw(fromDir+"/"+currentDir, f.Name(), newDir+"/"+currentDir)
  88. } else {
  89. //is a directory
  90. copyDirRaw(fromDir+"/"+currentDir, f.Name(), newDir+"/"+currentDir)
  91. }
  92. }
  93. }
  94. func copyFileRaw(fromDir string, fName string, newDir string) {
  95. c.Yellow("copying raw " + fromDir + "/" + fName)
  96. fileContent := readFile(fromDir + "/" + fName)
  97. writeFile(newDir+"/"+fName, fileContent)
  98. }
  99. func main() {
  100. c.Green("getting files from /webInput")
  101. c.Green("getting conifg from file konstruiConfig.json")
  102. //first reads the konstrui.Config.json
  103. readKonstruiConfig(rawFolderPath + "/" + konstruiConfigFile)
  104. c.Green("configuration:")
  105. fmt.Println(konstruiConfig.Files)
  106. c.Green("templating")
  107. //parseDir(rawFolderPath, newFolderPath)
  108. //create directory webOutput
  109. _ = os.Mkdir("webOutput", os.ModePerm)
  110. startTemplating(rawFolderPath, newFolderPath)
  111. c.Green("webpage finished, files at /webOutput")
  112. }