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