arnaucube
arnaucube/konstruiPublic Self-hosted repository
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	for i := 0; i < len(konstruiConfig.Files); i++ {
39		fName := konstruiConfig.Files[i]
40		fileNameSplitted := strings.Split(fName, ".")
41		extension := fileNameSplitted[len(fileNameSplitted)-1]
42		if extension == "html" {
43			fileContent := putTemplates(folderPath, fName)
44			writeFile(newDir+"/"+fName, fileContent)
45		} else if extension == "css" {
46			path := folderPath + "/" + fName
47			fileContent := readFile(path)
48			writeFile(newDir+"/"+fName, fileContent)
49		}
50	}
51
52	c.Cyan("starting to generate Pages to repeat")
53	for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
54		pageTemplate, data := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
55		for j := 0; j < len(data); j++ {
56			fmt.Println(j)
57			generatedPage := generatePageFromTemplateAndData(pageTemplate, data[j])
58			fmt.Println(data[j])
59			writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
60		}
61	}
62}
63func main() {
64	c.Green("getting files from /webInput")
65	c.Green("getting conifg from file konstruiConfig.json")
66	readKonstruiConfig(rawFolderPath + "/" + konstruiConfigFile)
67	c.Green("configuration:")
68	fmt.Println(konstruiConfig.Files)
69	c.Green("templating")
70	//parseDir(rawFolderPath, newFolderPath)
71	startTemplating(rawFolderPath, newFolderPath)
72	c.Green("webpage finished, wiles at /webOutput")
73}