From 484d84ce639e04b2b7076dee717391a9bdfd7810 Mon Sep 17 00:00:00 2001 From: arnaucode Date: Mon, 9 Oct 2017 13:00:30 +0200 Subject: [PATCH] implemented konstruiRepeatElem, need to repeat multi repeat --- fileOperations.go | 4 ++-- templating.go | 41 +++++++++++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/fileOperations.go b/fileOperations.go index edfa7c4..2fff6c5 100755 --- a/fileOperations.go +++ b/fileOperations.go @@ -66,8 +66,8 @@ 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)) + //img := "img" + //fmt.Println(jsonParsed.S(img)) return jsonParsed } diff --git a/templating.go b/templating.go index 613230e..6fa8116 100644 --- a/templating.go +++ b/templating.go @@ -6,7 +6,6 @@ import ( "strings" "github.com/Jeffail/gabs" - "github.com/fatih/color" ) func duplicateText(original string, count int) string { @@ -39,15 +38,13 @@ func replaceEntryOLD(templateContent string, entry dataEntry, jsonData *gabs.Con return templateContent } func replaceEntry(templateContent string, entry dataEntry, jsonData *gabs.Container, elemName string) string { - fmt.Println(jsonData) + //fmt.Println(jsonData) children, _ := jsonData.S().ChildrenMap() //_, ok := jsonData.S().Children() - color.Green("AAAAAAAAAA") for parameter, child := range children { //subchildren, _ := child.S().ChildrenMap() _, ok := child.S().Children() if ok != nil { - color.Blue(child.Data().(string)) templateContent = strings.Replace(templateContent, "{{"+parameter+"}}", child.Data().(string), -1) } else { /*for subparameter, subchild := range subchildren { @@ -76,7 +73,6 @@ func konstruiRepeatJSONPartTwo(templateContent string, entries []dataEntry, json children, _ := jsonData.S().Children() for _, child := range children { var entry dataEntry - color.Red("BBBBB") replaced = replaced + replaceEntry(extracted, entry, child, elemName) } fragmentLines := getLines(replaced) @@ -103,18 +99,43 @@ func konstruiRepeatJSON(templateContent string) string { func konstruiRepeatElem(templateContent string, entry dataEntry, jsonData *gabs.Container) string { if strings.Contains(templateContent, "") + children, _ := jsonData.S(elemName).Children() + /*for _, child := range children { + //var entry dataEntry + fmt.Println(child) + //replaced = replaced + replaceEntry(extracted, entry, child, elemName) + replaced = replaced + extracted + }*/ + fragmentLines := getLines(extracted) + fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeatElem") + f := concatStringsWithJumps(fragmentLines) + + var replaced string + for _, child := range children { + replacedElem := strings.Replace(f, "{{"+elemName+"}}", child.Data().(string), -1) + replaced = replaced + replacedElem + } + fragmentLines = getLines(replaced) + + lines := getLines(templateContent) + p := locateStringInArray(lines, "konstrui-repeatElem") + lines = deleteLinesBetween(lines, p[0], p[1]) + lines = addElementsToArrayPosition(lines, fragmentLines, p[0]) + templateContent = concatStringsWithJumps(lines) + } return templateContent } -func getElemFromObj(entry dataEntry, elemName string) []dataEntry { + +/*func getElemFromObj(entry dataEntry, elemName string) []dataEntry { var elemEntries []dataEntry fmt.Println(elemName) fmt.Println(entry) return elemEntries -} +}*/ func konstruiSimpleVars(template string, entries []dataEntry, jsonData *gabs.Container) string { //now, replace simple templating variables {{vars}} /*for _, entry := range entries {