mirror of
https://github.com/arnaucube/konstrui.git
synced 2026-02-06 19:16:41 +01:00
adding templating konstrui-repeat
This commit is contained in:
0
.gitignore
vendored
Normal file → Executable file
0
.gitignore
vendored
Normal file → Executable file
26
README.md
Normal file → Executable file
26
README.md
Normal file → Executable file
@@ -123,3 +123,29 @@ webInput/
|
|||||||
|
|
||||||
</html>
|
</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>
|
||||||
|
```
|
||||||
|
|||||||
BIN
example/konstrui
BIN
example/konstrui
Binary file not shown.
0
example/webInput/css/app.css
Normal file → Executable file
0
example/webInput/css/app.css
Normal file → Executable file
0
example/webInput/css/colors.css
Normal file → Executable file
0
example/webInput/css/colors.css
Normal file → Executable file
0
example/webInput/index.html
Normal file → Executable file
0
example/webInput/index.html
Normal file → Executable file
0
example/webInput/js/scripts1.js
Normal file → Executable file
0
example/webInput/js/scripts1.js
Normal file → Executable file
0
example/webInput/js/scripts2.js
Normal file → Executable file
0
example/webInput/js/scripts2.js
Normal file → Executable file
0
example/webInput/konstruiConfig.json
Normal file → Executable file
0
example/webInput/konstruiConfig.json
Normal file → Executable file
0
example/webInput/projectPage.html
Normal file → Executable file
0
example/webInput/projectPage.html
Normal file → Executable file
0
example/webInput/projects.html
Normal file → Executable file
0
example/webInput/projects.html
Normal file → Executable file
0
example/webInput/templates/projectTemplate.html
Normal file → Executable file
0
example/webInput/templates/projectTemplate.html
Normal file → Executable file
0
example/webInput/templates/projectTemplate.json
Normal file → Executable file
0
example/webInput/templates/projectTemplate.json
Normal file → Executable file
0
example/webInput/templates/userTemplate.html
Normal file → Executable file
0
example/webInput/templates/userTemplate.html
Normal file → Executable file
0
example/webInput/templates/userTemplate.json
Normal file → Executable file
0
example/webInput/templates/userTemplate.json
Normal file → Executable file
104
extractText.go
Normal file
104
extractText.go
Normal file
@@ -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)
|
||||||
|
}
|
||||||
64
fileOperations.go
Normal file → Executable file
64
fileOperations.go
Normal file → Executable file
@@ -3,10 +3,13 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
//dataEntry is the map used to create the array of maps, where the templatejson data is stored
|
//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
|
return entries
|
||||||
}
|
}
|
||||||
|
func replaceEntries(templateContent string, entries []dataEntry) string {
|
||||||
func generateFromTemplateAndData(templateContent string, entries []dataEntry) string {
|
|
||||||
|
|
||||||
var newContent string
|
var newContent string
|
||||||
|
|
||||||
|
//replace {{}} for data
|
||||||
for i := 0; i < len(entries); i++ {
|
for i := 0; i < len(entries); i++ {
|
||||||
var entryContent string
|
var entryContent string
|
||||||
entryContent = templateContent
|
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, "{{"+keys[j]+"}}", entries[i][keys[j]], -1)
|
||||||
entryContent = strings.Replace(entryContent, "[[i]]", strconv.Itoa(i), -1)
|
entryContent = strings.Replace(entryContent, "[[i]]", strconv.Itoa(i), -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
newContent = newContent + entryContent
|
newContent = newContent + entryContent
|
||||||
}
|
}
|
||||||
return newContent
|
return newContent
|
||||||
}
|
}
|
||||||
func getTemplateParameters(line string) (string, string) {
|
func putDataInTemplate(templateContent string, entries []dataEntry) string {
|
||||||
var templatePath string
|
var newContent string
|
||||||
var data string
|
newContent = templateContent
|
||||||
line = strings.Replace(line, "<konstrui-template ", "", -1)
|
|
||||||
line = strings.Replace(line, "></konstrui-template>", "", -1)
|
//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, " ")
|
attributes := strings.Split(line, " ")
|
||||||
//fmt.Println(attributes)
|
//fmt.Println(attributes)
|
||||||
for i := 0; i < len(attributes); i++ {
|
for i := 0; i < len(attributes); i++ {
|
||||||
attSplitted := strings.Split(attributes[i], "=")
|
attSplitted := strings.Split(attributes[i], "=")
|
||||||
if attSplitted[0] == "html" {
|
if attSplitted[0] == param1 {
|
||||||
templatePath = strings.Replace(attSplitted[1], `"`, "", -1)
|
param1content = strings.Replace(attSplitted[1], `"`, "", -1)
|
||||||
|
param1content = strings.Replace(param1content, ">", "", -1)
|
||||||
}
|
}
|
||||||
if attSplitted[0] == "data" {
|
if attSplitted[0] == param2 {
|
||||||
data = strings.Replace(attSplitted[1], `"`, "", -1)
|
param2content = strings.Replace(attSplitted[1], `"`, "", -1)
|
||||||
|
param2content = strings.Replace(param2content, ">", "", -1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return templatePath, data
|
return param1content, param2content
|
||||||
}
|
}
|
||||||
|
|
||||||
func useTemplate(templatePath string, dataPath string) string {
|
func useTemplate(templatePath string, dataPath string) string {
|
||||||
filepath := rawFolderPath + "/" + templatePath
|
filepath := rawFolderPath + "/" + templatePath
|
||||||
templateContent := readFile(filepath)
|
templateContent := readFile(filepath)
|
||||||
entries := getDataFromJson(rawFolderPath + "/" + dataPath)
|
entries := getDataFromJson(rawFolderPath + "/" + dataPath)
|
||||||
generated := generateFromTemplateAndData(templateContent, entries)
|
generated := putDataInTemplate(templateContent, entries)
|
||||||
return generated
|
return generated
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,7 +122,7 @@ func putTemplates(folderPath string, filename string) string {
|
|||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
currentLine := scanner.Text()
|
currentLine := scanner.Text()
|
||||||
if strings.Contains(currentLine, "<konstrui-template") && strings.Contains(currentLine, "</konstrui-template>") {
|
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)
|
fileContent = fileContent + useTemplate(templatePath, data)
|
||||||
} else {
|
} else {
|
||||||
fileContent = fileContent + currentLine
|
fileContent = fileContent + currentLine
|
||||||
@@ -109,7 +137,7 @@ func writeFile(path string, newContent string) {
|
|||||||
check(err)
|
check(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func generatePageFromTemplateAndData(templateContent string, entry dataEntry) string {
|
/*func generatePageFromTemplateAndData(templateContent string, entry dataEntry) string {
|
||||||
var entryContent string
|
var entryContent string
|
||||||
entryContent = templateContent
|
entryContent = templateContent
|
||||||
//first, get the map keys
|
//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)
|
entryContent = strings.Replace(entryContent, "{{"+keys[j]+"}}", entry[keys[j]], -1)
|
||||||
}
|
}
|
||||||
return entryContent
|
return entryContent
|
||||||
}
|
}*/
|
||||||
func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry) {
|
func getHtmlAndDataFromRepeatPages(page RepeatPages) (string, []dataEntry) {
|
||||||
templateContent := readFile(rawFolderPath + "/" + page.HtmlPage)
|
templateContent := readFile(rawFolderPath + "/" + page.HtmlPage)
|
||||||
data := getDataFromJson(rawFolderPath + "/" + page.Data)
|
data := getDataFromJson(rawFolderPath + "/" + page.Data)
|
||||||
|
|||||||
16
main.go
Normal file → Executable file
16
main.go
Normal file → Executable file
@@ -25,7 +25,7 @@ func parseDir(folderPath string, newDir string) {
|
|||||||
writeFile(newDir+"/"+f.Name(), fileContent)
|
writeFile(newDir+"/"+f.Name(), fileContent)
|
||||||
}
|
}
|
||||||
if len(fileNameSplitted) == 1 {
|
if len(fileNameSplitted) == 1 {
|
||||||
newDir := newDir + "/" + f.Name()
|
newDir = newDir + "/" + f.Name()
|
||||||
oldDir := rawFolderPath + "/" + f.Name()
|
oldDir := rawFolderPath + "/" + f.Name()
|
||||||
if _, err := os.Stat(newDir); os.IsNotExist(err) {
|
if _, err := os.Stat(newDir); os.IsNotExist(err) {
|
||||||
_ = os.Mkdir(newDir, 0700)
|
_ = os.Mkdir(newDir, 0700)
|
||||||
@@ -36,6 +36,7 @@ func parseDir(folderPath string, newDir string) {
|
|||||||
}
|
}
|
||||||
func startTemplating(folderPath string, newDir string) {
|
func startTemplating(folderPath string, newDir string) {
|
||||||
//do templating for each file in konstruiConfig.Files in konstruiConfig.Files
|
//do templating for each file in konstruiConfig.Files in konstruiConfig.Files
|
||||||
|
//konstrui-template
|
||||||
for i := 0; i < len(konstruiConfig.Files); i++ {
|
for i := 0; i < len(konstruiConfig.Files); i++ {
|
||||||
fName := konstruiConfig.Files[i]
|
fName := konstruiConfig.Files[i]
|
||||||
fileNameSplitted := strings.Split(fName, ".")
|
fileNameSplitted := strings.Split(fName, ".")
|
||||||
@@ -55,9 +56,11 @@ func startTemplating(folderPath string, newDir string) {
|
|||||||
for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
|
for i := 0; i < len(konstruiConfig.RepeatPages); i++ {
|
||||||
pageTemplate, data := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
|
pageTemplate, data := getHtmlAndDataFromRepeatPages(konstruiConfig.RepeatPages[i])
|
||||||
for j := 0; j < len(data); j++ {
|
for j := 0; j < len(data); j++ {
|
||||||
fmt.Println(j)
|
//fmt.Println(j)
|
||||||
generatedPage := generatePageFromTemplateAndData(pageTemplate, data[j])
|
var dataArray []dataEntry
|
||||||
fmt.Println(data[j])
|
dataArray = append(dataArray, data[j])
|
||||||
|
generatedPage := putDataInTemplate(pageTemplate, dataArray)
|
||||||
|
//fmt.Println(data[j])
|
||||||
writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage)
|
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) {
|
func copyFileRaw(fromDir string, fName string, newDir string) {
|
||||||
c.Yellow("copying raw " + fromDir + "//" + fName)
|
c.Yellow("copying raw " + fromDir + "/" + fName)
|
||||||
fileContent := readFile(fromDir + "/" + fName)
|
fileContent := readFile(fromDir + "/" + fName)
|
||||||
writeFile(newDir+"/"+fName, fileContent)
|
writeFile(newDir+"/"+fName, fileContent)
|
||||||
}
|
}
|
||||||
@@ -108,6 +111,9 @@ func main() {
|
|||||||
c.Green("templating")
|
c.Green("templating")
|
||||||
//parseDir(rawFolderPath, newFolderPath)
|
//parseDir(rawFolderPath, newFolderPath)
|
||||||
|
|
||||||
|
//create directory webOutput
|
||||||
|
_ = os.Mkdir("webOutput", os.ModePerm)
|
||||||
|
|
||||||
startTemplating(rawFolderPath, newFolderPath)
|
startTemplating(rawFolderPath, newFolderPath)
|
||||||
c.Green("webpage finished, files at /webOutput")
|
c.Green("webpage finished, files at /webOutput")
|
||||||
}
|
}
|
||||||
|
|||||||
0
media/example.png
Normal file → Executable file
0
media/example.png
Normal file → Executable file
|
Before Width: | Height: | Size: 110 KiB After Width: | Height: | Size: 110 KiB |
0
media/konstrui.png
Normal file → Executable file
0
media/konstrui.png
Normal file → Executable file
|
Before Width: | Height: | Size: 72 KiB After Width: | Height: | Size: 72 KiB |
0
readKonstruiConfig.go
Normal file → Executable file
0
readKonstruiConfig.go
Normal file → Executable file
Reference in New Issue
Block a user