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