From e0d30abc0e49d17e5448eb8c833666f3b0510e67 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Sat, 7 Oct 2017 22:30:04 +0200 Subject: [PATCH] added gabs, and implemented the replace with json structs. now need to finish the simplevarsreplace --- extractText.go | 4 +- fileOperations.go | 21 +++++++-- templating.go | 113 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 107 insertions(+), 31 deletions(-) diff --git a/extractText.go b/extractText.go index 491bb14..695c726 100644 --- a/extractText.go +++ b/extractText.go @@ -96,8 +96,8 @@ func (rtr *ReadTillReader) Read(p []byte) (n int, err error) { func extractText(original string, init string, fin string) string { contentReader := strings.NewReader(original) - str := NewSkipTillReader(contentReader, []byte("")) + str := NewSkipTillReader(contentReader, []byte(init)) + rtr := NewReadTillReader(str, []byte(fin)) bs, err := ioutil.ReadAll(rtr) check(err) return string(bs) diff --git a/fileOperations.go b/fileOperations.go index 25b4d43..edfa7c4 100755 --- a/fileOperations.go +++ b/fileOperations.go @@ -7,6 +7,7 @@ import ( "os" "strings" + "github.com/Jeffail/gabs" "github.com/fatih/color" ) @@ -42,7 +43,7 @@ func readFile(path string) string { return string(dat) } -func getDataFromJson(path string) []dataEntry { +func getDataFromJson(path string) ([]dataEntry, *gabs.Container) { var entries []dataEntry file, err := ioutil.ReadFile(path) check(err) @@ -57,7 +58,17 @@ func getDataFromJson(path string) []dataEntry { entries = append(entries, newDataEntry) } - return entries + jsonData := jsonGabs(path) + return entries, jsonData +} + +func jsonGabs(path string) *gabs.Container { + file, err := ioutil.ReadFile(path) + check(err) + jsonParsed, err := gabs.ParseJSON(file) + img := "img" + fmt.Println(jsonParsed.S(img)) + return jsonParsed } func writeFile(path string, newContent string) { @@ -82,8 +93,8 @@ func writeFile(path string, newContent string) { } return entryContent }*/ -func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry) { +func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry, *gabs.Container) { templateContent := readFile(rawFolderPath + "/" + page.HtmlPage) - data := getDataFromJson(rawFolderPath + "/" + page.Data) - return templateContent, data + data, jsonData := getDataFromJson(rawFolderPath + "/" + page.Data) + return templateContent, data, jsonData } diff --git a/templating.go b/templating.go index 51644b0..82497c3 100644 --- a/templating.go +++ b/templating.go @@ -1,7 +1,12 @@ package main import ( + "fmt" + "strconv" "strings" + + "github.com/Jeffail/gabs" + "github.com/fatih/color" ) func duplicateText(original string, count int) string { @@ -11,7 +16,7 @@ func duplicateText(original string, count int) string { } return result } -func replaceEntry(templateContent string, entry dataEntry) string { +func replaceEntryOLD(templateContent string, entry dataEntry, jsonData *gabs.Container, elemName string) string { //replace {{}} with data var keys []string for key, _ := range entry { @@ -21,48 +26,107 @@ func replaceEntry(templateContent string, entry dataEntry) string { 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) + children, _ := jsonData.S(keys[j]).Children() + fmt.Println("-") + fmt.Println(keys[j]) + fmt.Println(children) + for key, child := range children { + fmt.Print("key: " + strconv.Itoa(key) + ", value: ") + fmt.Println(child.Data().(string)) + } } + return templateContent } -func konstruiRepeatPartTwo(templateContent string, entries []dataEntry) string { +func replaceEntry(templateContent string, entry dataEntry, jsonData *gabs.Container, elemName string) string { + fmt.Println(jsonData) + children, _ := jsonData.S().ChildrenMap() + _, ok := jsonData.S().Children() + color.Green("AAAAAAAAAA") + fmt.Println(children) + fmt.Println(ok) + fmt.Println("-") + for parameter, child := range children { + subchildren, _ := child.S().ChildrenMap() + _, ok := child.S().Children() + fmt.Println(parameter) + fmt.Println(child) + fmt.Println(ok) + if ok != nil { + color.Green("child fin") + color.Blue(child.Data().(string)) + fmt.Println(child) + templateContent = strings.Replace(templateContent, "{{"+parameter+"}}", child.Data().(string), -1) + } else { + for subparameter, subchild := range subchildren { + color.Red(subchild.Data().(string)) + fmt.Println(subchild) + fmt.Println(subparameter) + //templateContent = strings.Replace(templateContent, "{{"+subparameter+"}}", subchild.Data().(string), -1) + } + } + + } + return templateContent +} +func konstruiRepeatJSONPartTwo(templateContent string, entries []dataEntry, jsonData *gabs.Container, elemName string) string { var newContent string newContent = templateContent - //replace - if strings.Contains(newContent, "") { + //replace + if strings.Contains(newContent, "") { //get content inside tags //get tags, and split by tags, get the content between tags - extracted := extractText(newContent, "") + extracted := extractText(newContent, "") //for each project, putDataInTemplate data:entries, template: content inside tags - var replaced string - for _, entry := range entries { - replaced = replaced + replaceEntry(extracted, entry) + //for _, entry := range entries { + children, _ := jsonData.S().Children() + for _, child := range children { + var entry dataEntry + color.Red("BBBBB") + replaced = replaced + replaceEntry(extracted, entry, child, elemName) } fragmentLines := getLines(replaced) - fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeat") + fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeatJSON") //afegir fragment al newContent, substituint el fragment original lines := getLines(templateContent) - p := locateStringInArray(lines, "konstrui-repeat") + p := locateStringInArray(lines, "konstrui-repeatJSON") 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, "