|
|
@ -3,10 +3,13 @@ package main |
|
|
|
import ( |
|
|
|
"bufio" |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"io/ioutil" |
|
|
|
"os" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
|
|
|
|
"github.com/fatih/color" |
|
|
|
) |
|
|
|
|
|
|
|
//dataEntry is the map used to create the array of maps, where the templatejson data is stored
|
|
|
@ -35,11 +38,10 @@ func getDataFromJson(path string) []dataEntry { |
|
|
|
|
|
|
|
return entries |
|
|
|
} |
|
|
|
|
|
|
|
func generateFromTemplateAndData(templateContent string, entries []dataEntry) string { |
|
|
|
|
|
|
|
func replaceEntries(templateContent string, entries []dataEntry) string { |
|
|
|
var newContent string |
|
|
|
|
|
|
|
//replace {{}} for data
|
|
|
|
for i := 0; i < len(entries); i++ { |
|
|
|
var entryContent string |
|
|
|
entryContent = templateContent |
|
|
@ -53,35 +55,61 @@ func generateFromTemplateAndData(templateContent string, entries []dataEntry) st |
|
|
|
entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entries[i][keys[j]], -1) |
|
|
|
entryContent = strings.Replace(entryContent, "[[i]]", strconv.Itoa(i), -1) |
|
|
|
} |
|
|
|
|
|
|
|
newContent = newContent + entryContent |
|
|
|
} |
|
|
|
return newContent |
|
|
|
} |
|
|
|
func getTemplateParameters(line string) (string, string) { |
|
|
|
var templatePath string |
|
|
|
var data string |
|
|
|
line = strings.Replace(line, "<konstrui-template ", "", -1) |
|
|
|
line = strings.Replace(line, "></konstrui-template>", "", -1) |
|
|
|
func putDataInTemplate(templateContent string, entries []dataEntry) string { |
|
|
|
var newContent string |
|
|
|
newContent = templateContent |
|
|
|
|
|
|
|
//replace <konstrui-repeat>
|
|
|
|
if strings.Contains(newContent, "<konstrui-repeat") && strings.Contains(newContent, "</konstrui-repeat>") { |
|
|
|
//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, "<konstrui-repeat", "</konstrui-repeat>") |
|
|
|
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) |
|
|
|
|
|
|
|
return newContent |
|
|
|
} |
|
|
|
func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) { |
|
|
|
var param1content string |
|
|
|
var param2content string |
|
|
|
line = strings.Replace(line, "<"+tagname+" ", "", -1) |
|
|
|
line = strings.Replace(line, "></"+tagname+">", "", -1) |
|
|
|
attributes := strings.Split(line, " ") |
|
|
|
//fmt.Println(attributes)
|
|
|
|
for i := 0; i < len(attributes); i++ { |
|
|
|
attSplitted := strings.Split(attributes[i], "=") |
|
|
|
if attSplitted[0] == "html" { |
|
|
|
templatePath = strings.Replace(attSplitted[1], `"`, "", -1) |
|
|
|
if attSplitted[0] == param1 { |
|
|
|
param1content = strings.Replace(attSplitted[1], `"`, "", -1) |
|
|
|
param1content = strings.Replace(param1content, ">", "", -1) |
|
|
|
} |
|
|
|
if attSplitted[0] == "data" { |
|
|
|
data = strings.Replace(attSplitted[1], `"`, "", -1) |
|
|
|
if attSplitted[0] == param2 { |
|
|
|
param2content = strings.Replace(attSplitted[1], `"`, "", -1) |
|
|
|
param2content = strings.Replace(param2content, ">", "", -1) |
|
|
|
} |
|
|
|
} |
|
|
|
return templatePath, data |
|
|
|
return param1content, param2content |
|
|
|
} |
|
|
|
|
|
|
|
func useTemplate(templatePath string, dataPath string) string { |
|
|
|
filepath := rawFolderPath + "/" + templatePath |
|
|
|
templateContent := readFile(filepath) |
|
|
|
entries := getDataFromJson(rawFolderPath + "/" + dataPath) |
|
|
|
generated := generateFromTemplateAndData(templateContent, entries) |
|
|
|
generated := putDataInTemplate(templateContent, entries) |
|
|
|
return generated |
|
|
|
} |
|
|
|
|
|
|
@ -94,7 +122,7 @@ func putTemplates(folderPath string, filename string) string { |
|
|
|
for scanner.Scan() { |
|
|
|
currentLine := scanner.Text() |
|
|
|
if strings.Contains(currentLine, "<konstrui-template") && strings.Contains(currentLine, "</konstrui-template>") { |
|
|
|
templatePath, data := getTemplateParameters(currentLine) |
|
|
|
templatePath, data := getTagParameters(currentLine, "konstrui-template", "html", "data") |
|
|
|
fileContent = fileContent + useTemplate(templatePath, data) |
|
|
|
} else { |
|
|
|
fileContent = fileContent + currentLine |
|
|
@ -109,7 +137,7 @@ func writeFile(path string, newContent string) { |
|
|
|
check(err) |
|
|
|
} |
|
|
|
|
|
|
|
func generatePageFromTemplateAndData(templateContent string, entry dataEntry) string { |
|
|
|
/*func generatePageFromTemplateAndData(templateContent string, entry dataEntry) string { |
|
|
|
var entryContent string |
|
|
|
entryContent = templateContent |
|
|
|
//first, get the map keys
|
|
|
@ -122,7 +150,7 @@ func generatePageFromTemplateAndData(templateContent string, entry dataEntry) st |
|
|
|
entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entry[keys[j]], -1) |
|
|
|
} |
|
|
|
return entryContent |
|
|
|
} |
|
|
|
}*/ |
|
|
|
func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry) { |
|
|
|
templateContent := readFile(rawFolderPath + "/" + page.HtmlPage) |
|
|
|
data := getDataFromJson(rawFolderPath + "/" + page.Data) |
|
|
|