mirror of
https://github.com/arnaucube/konstrui.git
synced 2026-02-07 03:26:43 +01:00
repeat fixed, need to clean code
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
@@ -38,26 +37,34 @@ func getDataFromJson(path string) []dataEntry {
|
|||||||
|
|
||||||
return entries
|
return entries
|
||||||
}
|
}
|
||||||
func replaceEntries(templateContent string, entries []dataEntry) string {
|
func duplicateText(original string, count int) string {
|
||||||
var newContent 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
|
//replace {{}} for data
|
||||||
for i := 0; i < len(entries); i++ {
|
//for i := 0; i < len(entries); i++ {
|
||||||
var entryContent string
|
//var entryContent string
|
||||||
entryContent = templateContent
|
//entryContent = templateContent
|
||||||
//first, get the map keys
|
//first, get the map keys
|
||||||
var keys []string
|
var keys []string
|
||||||
for key, _ := range entries[i] {
|
for key, _ := range entry {
|
||||||
keys = append(keys, key)
|
keys = append(keys, key)
|
||||||
}
|
}
|
||||||
//now, replace the keys with the values
|
//now, replace the keys with the values
|
||||||
for j := 0; j < len(keys); j++ {
|
for j := 0; j < len(keys); j++ {
|
||||||
entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entries[i][keys[j]], -1)
|
templateContent = strings.Replace(templateContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
|
||||||
entryContent = strings.Replace(entryContent, "[[i]]", strconv.Itoa(i), -1)
|
//templateContent = strings.Replace(templateContent, "[[i]]", strconv.Itoa(i), -1)
|
||||||
}
|
}
|
||||||
newContent = newContent + entryContent
|
//newContent = newContent + entryContent
|
||||||
}
|
//newContent = newContent + "\n"
|
||||||
return newContent
|
//}
|
||||||
|
return templateContent
|
||||||
}
|
}
|
||||||
func putDataInTemplate(templateContent string, entries []dataEntry) string {
|
func putDataInTemplate(templateContent string, entries []dataEntry) string {
|
||||||
var newContent string
|
var newContent string
|
||||||
@@ -66,23 +73,40 @@ func putDataInTemplate(templateContent string, entries []dataEntry) string {
|
|||||||
//replace <konstrui-repeat>
|
//replace <konstrui-repeat>
|
||||||
if strings.Contains(newContent, "<konstrui-repeat") && strings.Contains(newContent, "</konstrui-repeat>") {
|
if strings.Contains(newContent, "<konstrui-repeat") && strings.Contains(newContent, "</konstrui-repeat>") {
|
||||||
//repeat, _ := getTagParameters(newContent, "konstrui-repeat", "repeat", "nil")
|
//repeat, _ := getTagParameters(newContent, "konstrui-repeat", "repeat", "nil")
|
||||||
color.Blue("repeat data")
|
|
||||||
//fmt.Println(repeat)
|
|
||||||
|
|
||||||
//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-repeat", "</konstrui-repeat>")
|
||||||
fmt.Println(extracted)
|
|
||||||
//for each project, putDataInTemplate data:entries, template: content inside tags
|
//for each project, putDataInTemplate data:entries, template: content inside tags
|
||||||
fragment := replaceEntries(extracted, entries)
|
|
||||||
color.Blue(fragment)
|
//var fragment string
|
||||||
//afegir fragment al newContent
|
var replaced string
|
||||||
//esborrar les línies dels tags
|
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)
|
||||||
|
|
||||||
newContent = replaceEntries(templateContent, entries)
|
return result
|
||||||
|
|
||||||
return newContent
|
|
||||||
}
|
}
|
||||||
func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
|
func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
|
||||||
var param1content string
|
var param1content string
|
||||||
@@ -112,6 +136,11 @@ func useTemplate(templatePath string, dataPath string) string {
|
|||||||
generated := putDataInTemplate(templateContent, entries)
|
generated := putDataInTemplate(templateContent, entries)
|
||||||
return generated
|
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 {
|
func putTemplates(folderPath string, filename string) string {
|
||||||
var fileContent string
|
var fileContent string
|
||||||
@@ -123,12 +152,19 @@ func putTemplates(folderPath string, filename string) string {
|
|||||||
currentLine := scanner.Text()
|
currentLine := scanner.Text()
|
||||||
if strings.Contains(currentLine, "<konstrui-template") && strings.Contains(currentLine, "</konstrui-template>") {
|
if strings.Contains(currentLine, "<konstrui-template") && strings.Contains(currentLine, "</konstrui-template>") {
|
||||||
templatePath, data := getTagParameters(currentLine, "konstrui-template", "html", "data")
|
templatePath, data := getTagParameters(currentLine, "konstrui-template", "html", "data")
|
||||||
fileContent = fileContent + useTemplate(templatePath, data)
|
fileContent = fileContent + useTemplate(templatePath, data) + "\n"
|
||||||
} else {
|
} else {
|
||||||
fileContent = fileContent + currentLine
|
fileContent = fileContent + currentLine + "\n"
|
||||||
}
|
}
|
||||||
lineCount++
|
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
|
return fileContent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
23
main.go
23
main.go
@@ -11,7 +11,7 @@ const rawFolderPath = "./webInput"
|
|||||||
const newFolderPath = "./webOutput"
|
const newFolderPath = "./webOutput"
|
||||||
const konstruiConfigFile = "konstruiConfig.json"
|
const konstruiConfigFile = "konstruiConfig.json"
|
||||||
|
|
||||||
func parseDir(folderPath string, newDir string) {
|
/*func parseDir(folderPath string, newDir string) {
|
||||||
files, _ := ioutil.ReadDir(folderPath)
|
files, _ := ioutil.ReadDir(folderPath)
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
fileNameSplitted := strings.Split(f.Name(), ".")
|
fileNameSplitted := strings.Split(f.Name(), ".")
|
||||||
@@ -33,24 +33,21 @@ func parseDir(folderPath string, newDir string) {
|
|||||||
parseDir(oldDir, newDir)
|
parseDir(oldDir, newDir)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
func startTemplating(folderPath string, newDir string) {
|
func startTemplating(folderPath string, newDir string) {
|
||||||
|
//FILES
|
||||||
//do templating for each file in konstruiConfig.Files in konstruiConfig.Files
|
//do templating for each file in konstruiConfig.Files in konstruiConfig.Files
|
||||||
//konstrui-template
|
//konstrui-template
|
||||||
for i := 0; i < len(konstruiConfig.Files); i++ {
|
for i := 0; i < len(konstruiConfig.Files); i++ {
|
||||||
fName := konstruiConfig.Files[i]
|
fName := konstruiConfig.Files[i]
|
||||||
fileNameSplitted := strings.Split(fName, ".")
|
//fileNameSplitted := strings.Split(fName, ".")
|
||||||
extension := fileNameSplitted[len(fileNameSplitted)-1]
|
//extension := fileNameSplitted[len(fileNameSplitted)-1]
|
||||||
if extension == "html" {
|
|
||||||
fileContent := putTemplates(folderPath, fName)
|
fileContent := putTemplates(folderPath, fName)
|
||||||
writeFile(newDir+"/"+fName, fileContent)
|
/*fmt.Println(i)
|
||||||
} else if extension == "css" {
|
color.Red(fileContent)*/
|
||||||
path := folderPath + "/" + fName
|
|
||||||
fileContent := readFile(path)
|
|
||||||
writeFile(newDir+"/"+fName, fileContent)
|
writeFile(newDir+"/"+fName, fileContent)
|
||||||
}
|
}
|
||||||
}
|
//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++ {
|
||||||
@@ -64,7 +61,7 @@ func startTemplating(folderPath string, newDir string) {
|
|||||||
writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
|
writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//COPYRAW
|
||||||
//copy the konstruiConfig.CopyRaw files without modificate them
|
//copy the konstruiConfig.CopyRaw files without modificate them
|
||||||
for i := 0; i < len(konstruiConfig.CopyRaw); i++ {
|
for i := 0; i < len(konstruiConfig.CopyRaw); i++ {
|
||||||
fName := konstruiConfig.CopyRaw[i]
|
fName := konstruiConfig.CopyRaw[i]
|
||||||
@@ -107,7 +104,7 @@ func main() {
|
|||||||
//first reads the konstrui.Config.json
|
//first reads the konstrui.Config.json
|
||||||
readKonstruiConfig(rawFolderPath + "/" + konstruiConfigFile)
|
readKonstruiConfig(rawFolderPath + "/" + konstruiConfigFile)
|
||||||
c.Green("configuration:")
|
c.Green("configuration:")
|
||||||
fmt.Println(konstruiConfig.Files)
|
fmt.Println(konstruiConfig)
|
||||||
c.Green("templating")
|
c.Green("templating")
|
||||||
//parseDir(rawFolderPath, newFolderPath)
|
//parseDir(rawFolderPath, newFolderPath)
|
||||||
|
|
||||||
|
|||||||
58
stringManipulation.go
Normal file
58
stringManipulation.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func getLines(text string) []string {
|
||||||
|
lines := strings.Split(text, "\n")
|
||||||
|
return lines
|
||||||
|
}
|
||||||
|
|
||||||
|
func locateStringInArray(lines []string, s string) []int {
|
||||||
|
var positions []int
|
||||||
|
|
||||||
|
for k, l := range lines {
|
||||||
|
if strings.Contains(l, s) {
|
||||||
|
positions = append(positions, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return positions
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteArrayElementsWithString(lines []string, s string) []string {
|
||||||
|
var result []string
|
||||||
|
for _, l := range lines {
|
||||||
|
if !strings.Contains(l, s) {
|
||||||
|
result = append(result, l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteLinesBetween(lines []string, from int, to int) []string {
|
||||||
|
var result []string
|
||||||
|
result = append(lines[:from], lines[to+1:]...)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func addElementsToArrayPosition(lines []string, newLines []string, pos int) []string {
|
||||||
|
var result []string
|
||||||
|
result = append(result, lines[:pos]...)
|
||||||
|
result = append(result, newLines...)
|
||||||
|
result = append(result, lines[pos:]...)
|
||||||
|
/*
|
||||||
|
result = append(lines[:pos], newLines...)
|
||||||
|
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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user