diff --git a/fileOperations.go b/fileOperations.go index d615add..5976520 100755 --- a/fileOperations.go +++ b/fileOperations.go @@ -6,7 +6,6 @@ import ( "fmt" "io/ioutil" "os" - "strconv" "strings" "github.com/fatih/color" @@ -38,26 +37,34 @@ func getDataFromJson(path string) []dataEntry { return entries } -func replaceEntries(templateContent string, entries []dataEntry) string { - var newContent string +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 entries[i] { - keys = append(keys, key) - } - //now, replace the keys with the values - for j := 0; j < len(keys); j++ { - entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entries[i][keys[j]], -1) - entryContent = strings.Replace(entryContent, "[[i]]", strconv.Itoa(i), -1) - } - newContent = newContent + entryContent + //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) } - return newContent + //newContent = newContent + entryContent + //newContent = newContent + "\n" + //} + return templateContent } func putDataInTemplate(templateContent string, entries []dataEntry) string { var newContent string @@ -66,23 +73,40 @@ func putDataInTemplate(templateContent string, entries []dataEntry) string { //replace if strings.Contains(newContent, "") { //repeat, _ := getTagParameters(newContent, "konstrui-repeat", "repeat", "nil") - color.Blue("repeat data") - //fmt.Println(repeat) //get content inside tags //get tags, and split by tags, get the content between tags extracted := extractText(newContent, "") - fmt.Println(extracted) //for each project, putDataInTemplate data:entries, template: content inside tags - fragment := replaceEntries(extracted, entries) - color.Blue(fragment) - //afegir fragment al newContent - //esborrar les lĂ­nies dels tags - } - newContent = replaceEntries(templateContent, entries) + //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 newContent + return result } func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) { var param1content string @@ -112,6 +136,11 @@ func useTemplate(templatePath string, dataPath string) string { 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 @@ -123,12 +152,19 @@ func putTemplates(folderPath string, filename string) string { currentLine := scanner.Text() if strings.Contains(currentLine, "") { templatePath, data := getTagParameters(currentLine, "konstrui-template", "html", "data") - fileContent = fileContent + useTemplate(templatePath, data) + fileContent = fileContent + useTemplate(templatePath, data) + "\n" } else { - fileContent = fileContent + currentLine + fileContent = fileContent + currentLine + "\n" } lineCount++ } + + if strings.Contains(fileContent, "