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