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
 14/*func 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	//FILES
 39	//do templating for each file in konstruiConfig.Files in konstruiConfig.Files
 40	//konstrui-template
 41	for i := 0; i < len(konstruiConfig.Files); i++ {
 42		fName := konstruiConfig.Files[i]
 43		//fileNameSplitted := strings.Split(fName, ".")
 44		//extension := fileNameSplitted[len(fileNameSplitted)-1]
 45		fileContent := putTemplates(folderPath, fName)
 46		/*fmt.Println(i)
 47		color.Red(fileContent)*/
 48		writeFile(newDir+"/"+fName, fileContent)
 49	}
 50	//REPEATPAGES
 51	//do templating for the file pages in konstruiConfig.RepeatPages
 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			var dataArray []dataEntry
 58			dataArray = append(dataArray, data[j])
 59			generatedPage := putDataInTemplate(pageTemplate, dataArray)
 60			//fmt.Println(data[j])
 61			writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
 62		}
 63	}
 64	//COPYRAW
 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)
108	c.Green("templating")
109	//parseDir(rawFolderPath, newFolderPath)
110
111	//create directory webOutput
112	_ = os.Mkdir("webOutput", os.ModePerm)
113
114	startTemplating(rawFolderPath, newFolderPath)
115	c.Green("webpage finished, files at /webOutput")
116}