code cleaned and re organized
arnaucode arnaucode@gmail.com
Mon, 02 Oct 2017 18:11:52 +0200
5 files changed,
186 insertions(+),
230 deletions(-)
M
fileOperations.go
→
fileOperations.go
@@ -1,7 +1,6 @@
package main import ( - "bufio" "encoding/json" "fmt" "io/ioutil"@@ -14,6 +13,29 @@
//dataEntry is the map used to create the array of maps, where the templatejson data is stored type dataEntry map[string]string +func copyDirRaw(fromDir string, currentDir string, newDir string) { + filesList, _ := ioutil.ReadDir("./" + fromDir + "/" + currentDir) + fmt.Println(fromDir + "/" + currentDir) + c.Green(newDir + "/" + currentDir) + os.MkdirAll(newDir+"/"+currentDir, os.ModePerm) + for _, f := range filesList { + fileNameSplitted := strings.Split(f.Name(), ".") + if len(fileNameSplitted) > 1 { + //is a file + copyFileRaw(fromDir+"/"+currentDir, f.Name(), newDir+"/"+currentDir) + } else { + //is a directory + copyDirRaw(fromDir+"/"+currentDir, f.Name(), newDir+"/"+currentDir) + } + } +} + +func copyFileRaw(fromDir string, fName string, newDir string) { + c.Yellow("copying raw " + fromDir + "/" + fName) + fileContent := readFile(fromDir + "/" + fName) + writeFile(newDir+"/"+fName, fileContent) +} + func readFile(path string) string { dat, err := ioutil.ReadFile(path) check(err)@@ -37,140 +59,13 @@ }
return entries } -func duplicateText(original string, count int) string { - var result string - for i := 0; i < count; i++ { - result = result + original - } - return result -} -func replaceEntry(templateContent string, entry dataEntry) string { - //var newContent string - - //replace {{}} for data - //for i := 0; i < len(entries); i++ { - //var entryContent string - //entryContent = templateContent - //first, get the map keys - var keys []string - for key, _ := range entry { - keys = append(keys, key) - } - //now, replace the keys with the values - for j := 0; j < len(keys); j++ { - templateContent = strings.Replace(templateContent, "{{"+keys[j]+"}}", entry[keys[j]], -1) - //templateContent = strings.Replace(templateContent, "[[i]]", strconv.Itoa(i), -1) - } - //newContent = newContent + entryContent - //newContent = newContent + "\n" - //} - return templateContent -} -func putDataInTemplate(templateContent string, entries []dataEntry) string { - var newContent string - newContent = templateContent - - //replace <konstrui-repeat> - if strings.Contains(newContent, "<konstrui-repeat") && strings.Contains(newContent, "</konstrui-repeat>") { - //repeat, _ := getTagParameters(newContent, "konstrui-repeat", "repeat", "nil") - - //get content inside tags - //get tags, and split by tags, get the content between tags - extracted := extractText(newContent, "<konstrui-repeat", "</konstrui-repeat>") - //for each project, putDataInTemplate data:entries, template: content inside tags - - //var fragment string - var replaced string - for _, entry := range entries { - color.Green(extracted) - fmt.Println(entry) - replaced = replaced + replaceEntry(extracted, entry) - } - fragmentLines := getLines(replaced) - fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeat") - //afegir fragment al newContent, substituint el fragment original - lines := getLines(templateContent) - p := locateStringInArray(lines, "konstrui-repeat") - lines = deleteLinesBetween(lines, p[0], p[1]) - lines = addElementsToArrayPosition(lines, fragmentLines, p[0]) - /*lines = deleteArrayElementsWithString(lines, "konstrui-repeat") - fmt.Println(lines)*/ - templateContent = concatStringsWithJumps(lines) - fmt.Println(templateContent) - newContent = templateContent - } - color.Red(newContent) - result := templateContent - for _, entry := range entries { - result = replaceEntry(result, entry) - } - color.Blue(result) - - return result -} -func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) { - var param1content string - var param2content string - line = strings.Replace(line, "<"+tagname+" ", "", -1) - line = strings.Replace(line, "></"+tagname+">", "", -1) - attributes := strings.Split(line, " ") - //fmt.Println(attributes) - for i := 0; i < len(attributes); i++ { - attSplitted := strings.Split(attributes[i], "=") - if attSplitted[0] == param1 { - param1content = strings.Replace(attSplitted[1], `"`, "", -1) - param1content = strings.Replace(param1content, ">", "", -1) - } - if attSplitted[0] == param2 { - param2content = strings.Replace(attSplitted[1], `"`, "", -1) - param2content = strings.Replace(param2content, ">", "", -1) - } - } - return param1content, param2content -} - -func useTemplate(templatePath string, dataPath string) string { - filepath := rawFolderPath + "/" + templatePath - templateContent := readFile(filepath) - entries := getDataFromJson(rawFolderPath + "/" + dataPath) - generated := putDataInTemplate(templateContent, entries) - return generated -} -func useTemplateContent(templateContent string, dataPath string) string { - entries := getDataFromJson(rawFolderPath + "/" + dataPath) - generated := putDataInTemplate(templateContent, entries) - return generated -} - -func putTemplates(folderPath string, filename string) string { - var fileContent string - f, err := os.Open(folderPath + "/" + filename) - check(err) - scanner := bufio.NewScanner(f) - lineCount := 1 - for scanner.Scan() { - currentLine := scanner.Text() - if strings.Contains(currentLine, "<konstrui-template") && strings.Contains(currentLine, "</konstrui-template>") { - templatePath, data := getTagParameters(currentLine, "konstrui-template", "html", "data") - fileContent = fileContent + useTemplate(templatePath, data) + "\n" - } else { - fileContent = fileContent + currentLine + "\n" - } - lineCount++ - } - - if strings.Contains(fileContent, "<konstrui-repeat") { - dataPath, _ := getTagParameters(fileContent, "konstrui-repeat", "repeat", "nil") - dataPath = strings.Replace(dataPath, "\n", "", -1) - fileContent = useTemplateContent(fileContent, dataPath) - color.Red(fileContent) - } - return fileContent -} func writeFile(path string, newContent string) { err := ioutil.WriteFile(path, []byte(newContent), 0644) check(err) + + color.Green(path + ":") + color.Blue(newContent) } /*func generatePageFromTemplateAndData(templateContent string, entry dataEntry) string {
M
main.go
→
main.go
@@ -2,115 +2,27 @@ package main
import ( "fmt" - "io/ioutil" "os" - "strings" ) const rawFolderPath = "./webInput" const newFolderPath = "./webOutput" const konstruiConfigFile = "konstruiConfig.json" -/*func parseDir(folderPath string, newDir string) { - files, _ := ioutil.ReadDir(folderPath) - for _, f := range files { - fileNameSplitted := strings.Split(f.Name(), ".") - extension := fileNameSplitted[len(fileNameSplitted)-1] - if extension == "html" { - fileContent := putTemplates(folderPath, f.Name()) - writeFile(newDir+"/"+f.Name(), fileContent) - } else if extension == "css" { - path := folderPath + "/" + f.Name() - fileContent := readFile(path) - writeFile(newDir+"/"+f.Name(), fileContent) - } - if len(fileNameSplitted) == 1 { - newDir = newDir + "/" + f.Name() - oldDir := rawFolderPath + "/" + f.Name() - if _, err := os.Stat(newDir); os.IsNotExist(err) { - _ = os.Mkdir(newDir, 0700) - } - parseDir(oldDir, newDir) - } - } -}*/ -func startTemplating(folderPath string, newDir string) { - //FILES - //do templating for each file in konstruiConfig.Files in konstruiConfig.Files - //konstrui-template - for i := 0; i < len(konstruiConfig.Files); i++ { - fName := konstruiConfig.Files[i] - //fileNameSplitted := strings.Split(fName, ".") - //extension := fileNameSplitted[len(fileNameSplitted)-1] - fileContent := putTemplates(folderPath, fName) - /*fmt.Println(i) - color.Red(fileContent)*/ - writeFile(newDir+"/"+fName, fileContent) - } - //REPEATPAGES - //do templating for the file pages in konstruiConfig.RepeatPages - c.Cyan("starting to generate Pages to repeat") - for i := 0; i < len(konstruiConfig.RepeatPages); i++ { - pageTemplate, data := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i]) - for j := 0; j < len(data); j++ { - //fmt.Println(j) - var dataArray []dataEntry - dataArray = append(dataArray, data[j]) - generatedPage := putDataInTemplate(pageTemplate, dataArray) - //fmt.Println(data[j]) - writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage) - } - } - //COPYRAW - //copy the konstruiConfig.CopyRaw files without modificate them - for i := 0; i < len(konstruiConfig.CopyRaw); i++ { - fName := konstruiConfig.CopyRaw[i] - c.Yellow(fName) - fileNameSplitted := strings.Split(fName, ".") - if len(fileNameSplitted) > 1 { - //is a file - copyFileRaw(folderPath, fName, newDir) - } else { - //is a directory - c.Red(folderPath + "/" + fName) - copyDirRaw(folderPath, fName, newDir) - } - } -} -func copyDirRaw(fromDir string, currentDir string, newDir string) { - filesList, _ := ioutil.ReadDir("./" + fromDir + "/" + currentDir) - fmt.Println(fromDir + "/" + currentDir) - c.Green(newDir + "/" + currentDir) - os.MkdirAll(newDir+"/"+currentDir, os.ModePerm) - for _, f := range filesList { - fileNameSplitted := strings.Split(f.Name(), ".") - if len(fileNameSplitted) > 1 { - //is a file - copyFileRaw(fromDir+"/"+currentDir, f.Name(), newDir+"/"+currentDir) - } else { - //is a directory - copyDirRaw(fromDir+"/"+currentDir, f.Name(), newDir+"/"+currentDir) - } - } -} -func copyFileRaw(fromDir string, fName string, newDir string) { - c.Yellow("copying raw " + fromDir + "/" + fName) - fileContent := readFile(fromDir + "/" + fName) - writeFile(newDir+"/"+fName, fileContent) -} func main() { c.Green("getting files from /webInput") c.Green("getting conifg from file konstruiConfig.json") - //first reads the konstrui.Config.json + + //READ CONFIG: konstruiConfig.json readKonstruiConfig(rawFolderPath + "/" + konstruiConfigFile) c.Green("configuration:") fmt.Println(konstruiConfig) c.Green("templating") - //parseDir(rawFolderPath, newFolderPath) //create directory webOutput _ = os.Mkdir("webOutput", os.ModePerm) + //DO TEMPLATING startTemplating(rawFolderPath, newFolderPath) c.Green("webpage finished, files at /webOutput") }
M
stringManipulation.go
→
stringManipulation.go
@@ -9,6 +9,14 @@ lines := strings.Split(text, "\n")
return lines } +func concatStringsWithJumps(lines []string) string { + var r string + for _, l := range lines { + r = r + l + "\n" + } + return r +} + func locateStringInArray(lines []string, s string) []int { var positions []int@@ -48,11 +56,3 @@ result = append(result, lines[pos:]...)
*/ return result } - -func concatStringsWithJumps(lines []string) string { - var r string - for _, l := range lines { - r = r + l + "\n" - } - return r -}
A
templating.go
@@ -0,0 +1,149 @@
+package main + +import ( + "strings" +) + +func duplicateText(original string, count int) string { + var result string + for i := 0; i < count; i++ { + result = result + original + } + return result +} +func replaceEntry(templateContent string, entry dataEntry) string { + //replace {{}} with data + var keys []string + for key, _ := range entry { + keys = append(keys, key) + } + //now, replace the keys with the values + for j := 0; j < len(keys); j++ { + templateContent = strings.Replace(templateContent, "{{"+keys[j]+"}}", entry[keys[j]], -1) + //templateContent = strings.Replace(templateContent, "[[i]]", strconv.Itoa(i), -1) + } + return templateContent +} +func konstruiRepeatPartTwo(templateContent string, entries []dataEntry) string { + var newContent string + newContent = templateContent + + //replace <konstrui-repeat> + if strings.Contains(newContent, "<konstrui-repeat") && strings.Contains(newContent, "</konstrui-repeat>") { + //get content inside tags + //get tags, and split by tags, get the content between tags + extracted := extractText(newContent, "<konstrui-repeat", "</konstrui-repeat>") + //for each project, putDataInTemplate data:entries, template: content inside tags + + var replaced string + for _, entry := range entries { + replaced = replaced + replaceEntry(extracted, entry) + } + fragmentLines := getLines(replaced) + fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeat") + //afegir fragment al newContent, substituint el fragment original + lines := getLines(templateContent) + p := locateStringInArray(lines, "konstrui-repeat") + lines = deleteLinesBetween(lines, p[0], p[1]) + lines = addElementsToArrayPosition(lines, fragmentLines, p[0]) + templateContent = concatStringsWithJumps(lines) + } + return templateContent +} +func konstruiRepeat(templateContent string) string { + if strings.Contains(templateContent, "<konstrui-repeat") { + dataPath, _ := getTagParameters(templateContent, "konstrui-repeat", "repeatJSON", "nil") + dataPath = strings.Replace(dataPath, "\n", "", -1) + entries := getDataFromJson(rawFolderPath + "/" + dataPath) + templateContent = konstruiRepeatPartTwo(templateContent, entries) + } + return templateContent +} +func konstruiSimpleVars(template string, entries []dataEntry) string { + //now, replace simple templating variables {{vars}} + for _, entry := range entries { + template = replaceEntry(template, entry) + } + return template +} +func konstruiTemplate(templateContent string) string { + var result string + lines := getLines(templateContent) + for _, line := range lines { + if strings.Contains(line, "<konstrui-template") && strings.Contains(line, "</konstrui-template>") { + templatePath, data := getTagParameters(line, "konstrui-template", "html", "data") + result = result + useKonstruiTemplate(templatePath, data) + "\n" + } else { + result = result + line + "\n" + } + } + return result +} +func useKonstruiTemplate(templatePath string, dataPath string) string { + filepath := rawFolderPath + "/" + templatePath + templateContent := readFile(filepath) + entries := getDataFromJson(rawFolderPath + "/" + dataPath) + generated := konstruiRepeatPartTwo(templateContent, entries) + generated = konstruiSimpleVars(generated, entries) + return generated +} +func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) { + var param1content string + var param2content string + line = strings.Replace(line, "<"+tagname+" ", "", -1) + line = strings.Replace(line, "></"+tagname+">", "", -1) + attributes := strings.Split(line, " ") + for i := 0; i < len(attributes); i++ { + attSplitted := strings.Split(attributes[i], "=") + if attSplitted[0] == param1 { + param1content = strings.Replace(attSplitted[1], `"`, "", -1) + param1content = strings.Replace(param1content, ">", "", -1) + } + if attSplitted[0] == param2 { + param2content = strings.Replace(attSplitted[1], `"`, "", -1) + param2content = strings.Replace(param2content, ">", "", -1) + } + } + return param1content, param2content +} + +func startTemplating(folderPath string, newDir string) { + //FILES + //do templating for each file in konstruiConfig.Files + //konstrui-template + for i := 0; i < len(konstruiConfig.Files); i++ { + fName := konstruiConfig.Files[i] + fileContent := readFile(folderPath + "/" + fName) + fileContent = konstruiTemplate(fileContent) + generatedPage := konstruiRepeat(fileContent) + writeFile(newDir+"/"+fName, generatedPage) + } + //REPEATPAGES + //do templating for the file pages in konstruiConfig.RepeatPages + c.Cyan("starting to generate Pages to repeat") + for i := 0; i < len(konstruiConfig.RepeatPages); i++ { + pageTemplate, data := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i]) + for j := 0; j < len(data); j++ { + var dataArray []dataEntry + dataArray = append(dataArray, data[j]) + generatedPage := konstruiRepeatPartTwo(pageTemplate, dataArray) + generatedPage = konstruiSimpleVars(generatedPage, dataArray) + writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage) + } + } + //COPYRAW + //copy the konstruiConfig.CopyRaw files without modificate them + for i := 0; i < len(konstruiConfig.CopyRaw); i++ { + fName := konstruiConfig.CopyRaw[i] + c.Yellow(fName) + fileNameSplitted := strings.Split(fName, ".") + if len(fileNameSplitted) > 1 { + //is a file + copyFileRaw(folderPath, fName, newDir) + } else { + //is a directory + c.Red(folderPath + "/" + fName) + copyDirRaw(folderPath, fName, newDir) + } + } +}