mirror of
https://github.com/arnaucube/konstrui.git
synced 2026-02-06 19:16:41 +01:00
added gabs, and implemented the replace with json structs. now need to finish the simplevarsreplace
This commit is contained in:
@@ -96,8 +96,8 @@ func (rtr *ReadTillReader) Read(p []byte) (n int, err error) {
|
|||||||
|
|
||||||
func extractText(original string, init string, fin string) string {
|
func extractText(original string, init string, fin string) string {
|
||||||
contentReader := strings.NewReader(original)
|
contentReader := strings.NewReader(original)
|
||||||
str := NewSkipTillReader(contentReader, []byte("<konstrui-repeat"))
|
str := NewSkipTillReader(contentReader, []byte(init))
|
||||||
rtr := NewReadTillReader(str, []byte("</konstrui-repeat>"))
|
rtr := NewReadTillReader(str, []byte(fin))
|
||||||
bs, err := ioutil.ReadAll(rtr)
|
bs, err := ioutil.ReadAll(rtr)
|
||||||
check(err)
|
check(err)
|
||||||
return string(bs)
|
return string(bs)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Jeffail/gabs"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,7 +43,7 @@ func readFile(path string) string {
|
|||||||
return string(dat)
|
return string(dat)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDataFromJson(path string) []dataEntry {
|
func getDataFromJson(path string) ([]dataEntry, *gabs.Container) {
|
||||||
var entries []dataEntry
|
var entries []dataEntry
|
||||||
file, err := ioutil.ReadFile(path)
|
file, err := ioutil.ReadFile(path)
|
||||||
check(err)
|
check(err)
|
||||||
@@ -57,7 +58,17 @@ func getDataFromJson(path string) []dataEntry {
|
|||||||
entries = append(entries, newDataEntry)
|
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) {
|
func writeFile(path string, newContent string) {
|
||||||
@@ -82,8 +93,8 @@ func writeFile(path string, newContent string) {
|
|||||||
}
|
}
|
||||||
return entryContent
|
return entryContent
|
||||||
}*/
|
}*/
|
||||||
func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry) {
|
func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry, *gabs.Container) {
|
||||||
templateContent := readFile(rawFolderPath + "/" + page.HtmlPage)
|
templateContent := readFile(rawFolderPath + "/" + page.HtmlPage)
|
||||||
data := getDataFromJson(rawFolderPath + "/" + page.Data)
|
data, jsonData := getDataFromJson(rawFolderPath + "/" + page.Data)
|
||||||
return templateContent, data
|
return templateContent, data, jsonData
|
||||||
}
|
}
|
||||||
|
|||||||
113
templating.go
113
templating.go
@@ -1,7 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Jeffail/gabs"
|
||||||
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
func duplicateText(original string, count int) string {
|
func duplicateText(original string, count int) string {
|
||||||
@@ -11,7 +16,7 @@ func duplicateText(original string, count int) string {
|
|||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
func replaceEntry(templateContent string, entry dataEntry) string {
|
func replaceEntryOLD(templateContent string, entry dataEntry, jsonData *gabs.Container, elemName string) string {
|
||||||
//replace {{}} with data
|
//replace {{}} with data
|
||||||
var keys []string
|
var keys []string
|
||||||
for key, _ := range entry {
|
for key, _ := range entry {
|
||||||
@@ -21,48 +26,107 @@ func replaceEntry(templateContent string, entry dataEntry) string {
|
|||||||
for j := 0; j < len(keys); j++ {
|
for j := 0; j < len(keys); j++ {
|
||||||
templateContent = strings.Replace(templateContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
|
templateContent = strings.Replace(templateContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
|
||||||
//templateContent = strings.Replace(templateContent, "[[i]]", strconv.Itoa(i), -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 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
|
return templateContent
|
||||||
}
|
}
|
||||||
func konstruiRepeatPartTwo(templateContent string, entries []dataEntry) string {
|
func konstruiRepeatJSONPartTwo(templateContent string, entries []dataEntry, jsonData *gabs.Container, elemName string) string {
|
||||||
var newContent string
|
var newContent string
|
||||||
newContent = templateContent
|
newContent = templateContent
|
||||||
|
|
||||||
//replace <konstrui-repeat>
|
//replace <konstrui-repeatJSON>
|
||||||
if strings.Contains(newContent, "<konstrui-repeat") && strings.Contains(newContent, "</konstrui-repeat>") {
|
if strings.Contains(newContent, "<konstrui-repeatJSON") && strings.Contains(newContent, "</konstrui-repeatJSON>") {
|
||||||
//get content inside tags
|
//get content inside tags
|
||||||
//get tags, and split by tags, get the content between tags
|
//get tags, and split by tags, get the content between tags
|
||||||
extracted := extractText(newContent, "<konstrui-repeat", "</konstrui-repeat>")
|
extracted := extractText(newContent, "<konstrui-repeatJSON", "</konstrui-repeatJSON>")
|
||||||
//for each project, putDataInTemplate data:entries, template: content inside tags
|
//for each project, putDataInTemplate data:entries, template: content inside tags
|
||||||
|
|
||||||
var replaced string
|
var replaced string
|
||||||
for _, entry := range entries {
|
//for _, entry := range entries {
|
||||||
replaced = replaced + replaceEntry(extracted, entry)
|
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 := getLines(replaced)
|
||||||
fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeat")
|
fragmentLines = deleteArrayElementsWithString(fragmentLines, "konstrui-repeatJSON")
|
||||||
//afegir fragment al newContent, substituint el fragment original
|
//afegir fragment al newContent, substituint el fragment original
|
||||||
lines := getLines(templateContent)
|
lines := getLines(templateContent)
|
||||||
p := locateStringInArray(lines, "konstrui-repeat")
|
p := locateStringInArray(lines, "konstrui-repeatJSON")
|
||||||
lines = deleteLinesBetween(lines, p[0], p[1])
|
lines = deleteLinesBetween(lines, p[0], p[1])
|
||||||
lines = addElementsToArrayPosition(lines, fragmentLines, p[0])
|
lines = addElementsToArrayPosition(lines, fragmentLines, p[0])
|
||||||
templateContent = concatStringsWithJumps(lines)
|
templateContent = concatStringsWithJumps(lines)
|
||||||
}
|
}
|
||||||
|
|
||||||
return templateContent
|
return templateContent
|
||||||
}
|
}
|
||||||
func konstruiRepeat(templateContent string) string {
|
func konstruiRepeatJSON(templateContent string) string {
|
||||||
if strings.Contains(templateContent, "<konstrui-repeat") {
|
if strings.Contains(templateContent, "<konstrui-repeatJSON") {
|
||||||
dataPath, _ := getTagParameters(templateContent, "konstrui-repeat", "repeatJSON", "nil")
|
dataPath, _ := getTagParameters(templateContent, "konstrui-repeatJSON", "repeatJSON", "nil")
|
||||||
dataPath = strings.Replace(dataPath, "\n", "", -1)
|
dataPath = strings.Replace(dataPath, "\n", "", -1)
|
||||||
entries := getDataFromJson(rawFolderPath + "/" + dataPath)
|
entries, jsonData := getDataFromJson(rawFolderPath + "/" + dataPath)
|
||||||
templateContent = konstruiRepeatPartTwo(templateContent, entries)
|
templateContent = konstruiRepeatJSONPartTwo(templateContent, entries, jsonData, "")
|
||||||
}
|
}
|
||||||
return templateContent
|
return templateContent
|
||||||
}
|
}
|
||||||
func konstruiSimpleVars(template string, entries []dataEntry) string {
|
func konstruiRepeatElem(templateContent string, entry dataEntry, jsonData *gabs.Container) string {
|
||||||
|
if strings.Contains(templateContent, "<konstrui-repeatElem") {
|
||||||
|
elemName, _ := getTagParameters(templateContent, "konstrui-repeatElem", "repeatElem", "nil")
|
||||||
|
color.Red(elemName)
|
||||||
|
elemEntries := getElemFromObj(entry, elemName)
|
||||||
|
templateContent = konstruiRepeatJSONPartTwo(templateContent, elemEntries, jsonData, elemName)
|
||||||
|
}
|
||||||
|
return templateContent
|
||||||
|
}
|
||||||
|
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}}
|
//now, replace simple templating variables {{vars}}
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
template = replaceEntry(template, entry)
|
template = replaceEntry(template, entry, jsonData, "")
|
||||||
}
|
}
|
||||||
return template
|
return template
|
||||||
}
|
}
|
||||||
@@ -82,9 +146,9 @@ func konstruiTemplate(templateContent string) string {
|
|||||||
func useKonstruiTemplate(templatePath string, dataPath string) string {
|
func useKonstruiTemplate(templatePath string, dataPath string) string {
|
||||||
filepath := rawFolderPath + "/" + templatePath
|
filepath := rawFolderPath + "/" + templatePath
|
||||||
templateContent := readFile(filepath)
|
templateContent := readFile(filepath)
|
||||||
entries := getDataFromJson(rawFolderPath + "/" + dataPath)
|
entries, jsonData := getDataFromJson(rawFolderPath + "/" + dataPath)
|
||||||
generated := konstruiRepeatPartTwo(templateContent, entries)
|
generated := konstruiRepeatJSONPartTwo(templateContent, entries, jsonData, "")
|
||||||
generated = konstruiSimpleVars(generated, entries)
|
generated = konstruiSimpleVars(generated, entries, jsonData)
|
||||||
return generated
|
return generated
|
||||||
}
|
}
|
||||||
func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
|
func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
|
||||||
@@ -115,19 +179,20 @@ func startTemplating(folderPath string, newDir string) {
|
|||||||
fName := konstruiConfig.Files[i]
|
fName := konstruiConfig.Files[i]
|
||||||
fileContent := readFile(folderPath + "/" + fName)
|
fileContent := readFile(folderPath + "/" + fName)
|
||||||
fileContent = konstruiTemplate(fileContent)
|
fileContent = konstruiTemplate(fileContent)
|
||||||
generatedPage := konstruiRepeat(fileContent)
|
generatedPage := konstruiRepeatJSON(fileContent)
|
||||||
writeFile(newDir+"/"+fName, generatedPage)
|
writeFile(newDir+"/"+fName, generatedPage)
|
||||||
}
|
}
|
||||||
//REPEATPAGES
|
//REPEATPAGES
|
||||||
//do templating for the file pages in konstruiConfig.RepeatPages
|
//do templating for the file pages in konstruiConfig.RepeatPages
|
||||||
c.Cyan("starting to generate Pages to repeat")
|
c.Cyan("starting to generate Pages to repeat")
|
||||||
for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
|
for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
|
||||||
pageTemplate, data := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
|
pageTemplate, data, jsonData := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
|
||||||
for j := 0; j < len(data); j++ {
|
for j := 0; j < len(data); j++ {
|
||||||
var dataArray []dataEntry
|
var dataArray []dataEntry
|
||||||
dataArray = append(dataArray, data[j])
|
dataArray = append(dataArray, data[j])
|
||||||
generatedPage := konstruiRepeatPartTwo(pageTemplate, dataArray)
|
generatedPage := konstruiRepeatJSONPartTwo(pageTemplate, dataArray, jsonData, "")
|
||||||
generatedPage = konstruiSimpleVars(generatedPage, dataArray)
|
generatedPage = konstruiRepeatElem(generatedPage, dataArray[0], jsonData)
|
||||||
|
generatedPage = konstruiSimpleVars(generatedPage, dataArray, jsonData)
|
||||||
writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
|
writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user