arnaucube
arnaucube/konstruiPublic Self-hosted repository
implemented functionallity to get the files to apply templating from the json file of config
arnaucode idoctnef@openmailbox.org
Sun, 21 May 2017 18:23:17 +0200
commit

11f2b576a5bce3abb81ba35a33c77b44021c6413

parent

c77af453f45392f2100b419760b9f2f290c6ea12

4 files changed, 66 insertions(+), 1 deletions(-)

jump to
M README.md → README.md

@@ -68,6 +68,23 @@ }

] ``` +- Set the configuration file timmyConfig.json in the webInput directory: + +```json +{ + "title": "Web example", + "author": "arnaucode", + "github": "github.com/arnaucode", + "website": "arnaucode.com", + "files": [ + "index.html", + "projects.html", + "app.css" + ] +} +``` + + - Execute Timmy ```
M main.go → main.go

@@ -1,6 +1,7 @@

package main import ( + "fmt" "io/ioutil" "os" "strings"

@@ -8,6 +9,7 @@ )

const rawFolderPath = "./webInput" const newFolderPath = "./webOutput" +const timmyConfigFile = "timmyConfig.json" func parseDir(folderPath string, newDir string) { files, _ := ioutil.ReadDir(folderPath)

@@ -31,9 +33,28 @@ parseDir(oldDir, newDir)

} } } +func startTemplating(folderPath string, newDir string) { + for i := 0; i < len(timmyConfig.Files); i++ { + fName := timmyConfig.Files[i] + fileNameSplitted := strings.Split(fName, ".") + extension := fileNameSplitted[len(fileNameSplitted)-1] + if extension == "html" { + fileContent := putTemplates(folderPath, fName) + writeFile(newDir+"/"+fName, fileContent) + } else if extension == "css" { + fileContent := readFile(folderPath, fName) + writeFile(newDir+"/"+fName, fileContent) + } + } +} func main() { c.Green("getting files from /webInput") + c.Green("getting conifg from file timmyConfig.json") + readTimmyConfig(rawFolderPath + "/" + timmyConfigFile) + c.Green("configuration:") + fmt.Println(timmyConfig.Files) c.Green("templating") - parseDir(rawFolderPath, newFolderPath) + //parseDir(rawFolderPath, newFolderPath) + startTemplating(rawFolderPath, newFolderPath) c.Green("webpage finished, wiles at /webOutput") }
A readTimmyConfig.go

@@ -0,0 +1,27 @@

+package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" +) + +//TimmyConfig is the configuration from the file timmyConfig.json, on the folder /webInput +type TimmyConfig struct { + Title string `json:"title"` + Author string `json:"author"` + Github string `json:"github"` + Website string `json:"website"` + Files []string `json:"files"` +} + +var timmyConfig TimmyConfig + +func readTimmyConfig(path string) { + file, e := ioutil.ReadFile(path) + if e != nil { + fmt.Println("error:", e) + } + content := string(file) + json.Unmarshal([]byte(content), &timmyConfig) +}