adding templating konstrui-repeat
arnaucode arnaucode@gmail.com
Tue, 26 Sep 2017 20:16:05 +0200
25 files changed,
187 insertions(+),
23 deletions(-)
jump to
M
README.md
→
README.md
@@ -123,3 +123,29 @@ </body>
</html> ``` + + + +## Features + +Import templates +```html +<konstrui-template html="template.html" data="template.json"></konstrui-template> +``` + +Load values: +```html +{{username}} +``` + +Number of iterations: +```html +User [[i]] +``` + +Subobjects: +```html +<p konstrui-repeat="users"> + {{user.username}} +</p> +```
A
extractText.go
@@ -0,0 +1,104 @@
+package main + +import ( + "bufio" + "bytes" + "io" + "io/ioutil" + "strings" +) + +type SkipTillReader struct { + rdr *bufio.Reader + delim []byte + found bool +} + +func NewSkipTillReader(reader io.Reader, delim []byte) *SkipTillReader { + return &SkipTillReader{ + rdr: bufio.NewReader(reader), + delim: delim, + found: false, + } +} + +func (str *SkipTillReader) Read(p []byte) (n int, err error) { + if str.found { + return str.rdr.Read(p) + } else { + // search byte by byte for the delimiter + outer: + for { + for i := range str.delim { + var c byte + c, err = str.rdr.ReadByte() + if err != nil { + n = 0 + return + } + // doens't match so start over + if str.delim[i] != c { + continue outer + } + } + str.found = true + // we read the delimiter so add it back + str.rdr = bufio.NewReader(io.MultiReader(bytes.NewReader(str.delim), str.rdr)) + return str.Read(p) + } + } +} + +type ReadTillReader struct { + rdr *bufio.Reader + delim []byte + found bool +} + +func NewReadTillReader(reader io.Reader, delim []byte) *ReadTillReader { + return &ReadTillReader{ + rdr: bufio.NewReader(reader), + delim: delim, + found: false, + } +} + +func (rtr *ReadTillReader) Read(p []byte) (n int, err error) { + if rtr.found { + return 0, io.EOF + } else { + outer: + for n < len(p) { + for i := range rtr.delim { + var c byte + c, err = rtr.rdr.ReadByte() + if err != nil && n > 0 { + err = nil + return + } else if err != nil { + return + } + p[n] = c + n++ + if rtr.delim[i] != c { + continue outer + } + } + rtr.found = true + break + } + if n == 0 { + err = io.EOF + } + return + } +} + +func extractText(original string, init string, fin string) string { + contentReader := strings.NewReader(original) + str := NewSkipTillReader(contentReader, []byte("<konstrui-repeat")) + rtr := NewReadTillReader(str, []byte("</konstrui-repeat>")) + bs, err := ioutil.ReadAll(rtr) + check(err) + return string(bs) +}
M
fileOperations.go
→
fileOperations.go
@@ -3,10 +3,13 @@
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 @@ }
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 @@ for j := 0; j < len(keys); j++ {
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 @@ lineCount := 1
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 @@ err := ioutil.WriteFile(path, []byte(newContent), 0644)
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 @@ for j := 0; j < len(keys); j++ {
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)
M
main.go
→
main.go
@@ -25,7 +25,7 @@ fileContent := readFile(path)
writeFile(newDir+"/"+f.Name(), fileContent) } if len(fileNameSplitted) == 1 { - newDir := newDir + "/" + f.Name() + newDir = newDir + "/" + f.Name() oldDir := rawFolderPath + "/" + f.Name() if _, err := os.Stat(newDir); os.IsNotExist(err) { _ = os.Mkdir(newDir, 0700)@@ -36,6 +36,7 @@ }
} func startTemplating(folderPath string, newDir string) { //do templating for each file in konstruiConfig.Files in konstruiConfig.Files + //konstrui-template for i := 0; i < len(konstruiConfig.Files); i++ { fName := konstruiConfig.Files[i] fileNameSplitted := strings.Split(fName, ".")@@ -55,9 +56,11 @@ c.Cyan("starting to generate Pages to repeat")
for i := 0; i < len(konstruiConfig.RepeatPages); i++ { pageTemplate, data := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i]) for j := 0; j < len(data); j++ { - fmt.Println(j) - generatedPage := generatePageFromTemplateAndData(pageTemplate, data[j]) - fmt.Println(data[j]) + //fmt.Println(j) + var dataArray []dataEntry + dataArray = append(dataArray, data[j]) + generatedPage := putDataInTemplate(pageTemplate, dataArray) + //fmt.Println(data[j]) writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage) } }@@ -94,7 +97,7 @@ }
} } func copyFileRaw(fromDir string, fName string, newDir string) { - c.Yellow("copying raw " + fromDir + "//" + fName) + c.Yellow("copying raw " + fromDir + "/" + fName) fileContent := readFile(fromDir + "/" + fName) writeFile(newDir+"/"+fName, fileContent) }@@ -107,6 +110,9 @@ c.Green("configuration:")
fmt.Println(konstruiConfig.Files) c.Green("templating") //parseDir(rawFolderPath, newFolderPath) + + //create directory webOutput + _ = os.Mkdir("webOutput", os.ModePerm) startTemplating(rawFolderPath, newFolderPath) c.Green("webpage finished, files at /webOutput")