Browse Source

adding templating konstrui-repeat

master
arnaucode 6 years ago
parent
commit
18477616ab
25 changed files with 187 additions and 23 deletions
  1. +0
    -0
      .gitignore
  2. +0
    -0
      LICENSE
  3. +26
    -0
      README.md
  4. +0
    -0
      color.go
  5. +0
    -0
      errors.go
  6. BIN
      example/konstrui
  7. +0
    -0
      example/webInput/css/app.css
  8. +0
    -0
      example/webInput/css/colors.css
  9. +0
    -0
      example/webInput/index.html
  10. +0
    -0
      example/webInput/js/scripts1.js
  11. +0
    -0
      example/webInput/js/scripts2.js
  12. +0
    -0
      example/webInput/konstruiConfig.json
  13. +0
    -0
      example/webInput/projectPage.html
  14. +0
    -0
      example/webInput/projects.html
  15. +0
    -0
      example/webInput/templates/projectTemplate.html
  16. +0
    -0
      example/webInput/templates/projectTemplate.json
  17. +0
    -0
      example/webInput/templates/userTemplate.html
  18. +0
    -0
      example/webInput/templates/userTemplate.json
  19. +104
    -0
      extractText.go
  20. +46
    -18
      fileOperations.go
  21. BIN
      konstrui
  22. +11
    -5
      main.go
  23. +0
    -0
      media/example.png
  24. +0
    -0
      media/konstrui.png
  25. +0
    -0
      readKonstruiConfig.go

+ 0
- 0
.gitignore


+ 0
- 0
LICENSE


+ 26
- 0
README.md

@ -123,3 +123,29 @@ webInput/
</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>
```

+ 0
- 0
color.go


+ 0
- 0
errors.go


BIN
example/konstrui


+ 0
- 0
example/webInput/css/app.css


+ 0
- 0
example/webInput/css/colors.css


+ 0
- 0
example/webInput/index.html


+ 0
- 0
example/webInput/js/scripts1.js


+ 0
- 0
example/webInput/js/scripts2.js


+ 0
- 0
example/webInput/konstruiConfig.json


+ 0
- 0
example/webInput/projectPage.html


+ 0
- 0
example/webInput/projects.html


+ 0
- 0
example/webInput/templates/projectTemplate.html


+ 0
- 0
example/webInput/templates/projectTemplate.json


+ 0
- 0
example/webInput/templates/userTemplate.html


+ 0
- 0
example/webInput/templates/userTemplate.json


+ 104
- 0
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)
}

+ 46
- 18
fileOperations.go

@ -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)

BIN
konstrui


+ 11
- 5
main.go

@ -25,7 +25,7 @@ func parseDir(folderPath string, newDir string) {
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 parseDir(folderPath string, newDir string) {
}
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 @@ func startTemplating(folderPath string, newDir string) {
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 copyDirRaw(fromDir string, currentDir string, newDir string) {
}
}
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)
}
@ -108,6 +111,9 @@ func main() {
c.Green("templating")
//parseDir(rawFolderPath, newFolderPath)
//create directory webOutput
_ = os.Mkdir("webOutput", os.ModePerm)
startTemplating(rawFolderPath, newFolderPath)
c.Green("webpage finished, files at /webOutput")
}

+ 0
- 0
media/example.png

Before After
Width: 1004  |  Height: 649  |  Size: 110 KiB Width: 1004  |  Height: 649  |  Size: 110 KiB

+ 0
- 0
media/konstrui.png

Before After
Width: 244  |  Height: 387  |  Size: 72 KiB Width: 244  |  Height: 387  |  Size: 72 KiB

+ 0
- 0
readKonstruiConfig.go


Loading…
Cancel
Save