arnaucube
arnaucube/konstruiPublic Self-hosted repository
konstrui/fileOperations.go
fileOperations.goRaw
  1package main
  2
  3import (
  4	"bufio"
  5	"encoding/json"
  6	"fmt"
  7	"io/ioutil"
  8	"os"
  9	"strconv"
 10	"strings"
 11
 12	"github.com/fatih/color"
 13)
 14
 15//dataEntry is the map used to create the array of maps, where the templatejson data is stored
 16type dataEntry map[string]string
 17
 18func readFile(path string) string {
 19	dat, err := ioutil.ReadFile(path)
 20	check(err)
 21	return string(dat)
 22}
 23
 24func getDataFromJson(path string) []dataEntry {
 25	var entries []dataEntry
 26	file, err := ioutil.ReadFile(path)
 27	check(err)
 28	content := string(file)
 29	var rawEntries []*json.RawMessage
 30	json.Unmarshal([]byte(content), &rawEntries)
 31	for i := 0; i < len(rawEntries); i++ {
 32		rawEntryMarshaled, err := json.Marshal(rawEntries[i])
 33		check(err)
 34		var newDataEntry map[string]string
 35		json.Unmarshal(rawEntryMarshaled, &newDataEntry)
 36		entries = append(entries, newDataEntry)
 37	}
 38
 39	return entries
 40}
 41func replaceEntries(templateContent string, entries []dataEntry) string {
 42	var newContent string
 43
 44	//replace {{}} for data
 45	for i := 0; i < len(entries); i++ {
 46		var entryContent string
 47		entryContent = templateContent
 48		//first, get the map keys
 49		var keys []string
 50		for key, _ := range entries[i] {
 51			keys = append(keys, key)
 52		}
 53		//now, replace the keys with the values
 54		for j := 0; j < len(keys); j++ {
 55			entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entries[i][keys[j]], -1)
 56			entryContent = strings.Replace(entryContent, "[[i]]", strconv.Itoa(i), -1)
 57		}
 58		newContent = newContent + entryContent
 59	}
 60	return newContent
 61}
 62func putDataInTemplate(templateContent string, entries []dataEntry) string {
 63	var newContent string
 64	newContent = templateContent
 65
 66	//replace <konstrui-repeat>
 67	if strings.Contains(newContent, "<konstrui-repeat") && strings.Contains(newContent, "</konstrui-repeat>") {
 68		//repeat, _ := getTagParameters(newContent, "konstrui-repeat", "repeat", "nil")
 69		color.Blue("repeat data")
 70		//fmt.Println(repeat)
 71
 72		//get content inside tags
 73		//get tags, and split by tags, get the content between tags
 74		extracted := extractText(newContent, "<konstrui-repeat", "</konstrui-repeat>")
 75		fmt.Println(extracted)
 76		//for each project, putDataInTemplate data:entries, template: content inside tags
 77		fragment := replaceEntries(extracted, entries)
 78		color.Blue(fragment)
 79		//afegir fragment al newContent
 80		//esborrar les línies dels tags
 81	}
 82
 83	newContent = replaceEntries(templateContent, entries)
 84
 85	return newContent
 86}
 87func getTagParameters(line string, tagname string, param1 string, param2 string) (string, string) {
 88	var param1content string
 89	var param2content string
 90	line = strings.Replace(line, "<"+tagname+" ", "", -1)
 91	line = strings.Replace(line, "></"+tagname+">", "", -1)
 92	attributes := strings.Split(line, " ")
 93	//fmt.Println(attributes)
 94	for i := 0; i < len(attributes); i++ {
 95		attSplitted := strings.Split(attributes[i], "=")
 96		if attSplitted[0] == param1 {
 97			param1content = strings.Replace(attSplitted[1], `"`, "", -1)
 98			param1content = strings.Replace(param1content, ">", "", -1)
 99		}
100		if attSplitted[0] == param2 {
101			param2content = strings.Replace(attSplitted[1], `"`, "", -1)
102			param2content = strings.Replace(param2content, ">", "", -1)
103		}
104	}
105	return param1content, param2content
106}
107
108func useTemplate(templatePath string, dataPath string) string {
109	filepath := rawFolderPath + "/" + templatePath
110	templateContent := readFile(filepath)
111	entries := getDataFromJson(rawFolderPath + "/" + dataPath)
112	generated := putDataInTemplate(templateContent, entries)
113	return generated
114}
115
116func putTemplates(folderPath string, filename string) string {
117	var fileContent string
118	f, err := os.Open(folderPath + "/" + filename)
119	check(err)
120	scanner := bufio.NewScanner(f)
121	lineCount := 1
122	for scanner.Scan() {
123		currentLine := scanner.Text()
124		if strings.Contains(currentLine, "<konstrui-template") && strings.Contains(currentLine, "</konstrui-template>") {
125			templatePath, data := getTagParameters(currentLine, "konstrui-template", "html", "data")
126			fileContent = fileContent + useTemplate(templatePath, data)
127		} else {
128			fileContent = fileContent + currentLine
129		}
130		lineCount++
131	}
132	return fileContent
133}
134
135func writeFile(path string, newContent string) {
136	err := ioutil.WriteFile(path, []byte(newContent), 0644)
137	check(err)
138}
139
140/*func generatePageFromTemplateAndData(templateContent string, entry dataEntry) string {
141	var entryContent string
142	entryContent = templateContent
143	//first, get the map keys
144	var keys []string
145	for key, _ := range entry {
146		keys = append(keys, key)
147	}
148	//now, replace the keys with the values
149	for j := 0; j < len(keys); j++ {
150		entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
151	}
152	return entryContent
153}*/
154func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry) {
155	templateContent := readFile(rawFolderPath + "/" + page.HtmlPage)
156	data := getDataFromJson(rawFolderPath + "/" + page.Data)
157	return templateContent, data
158}