diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 418f37a..6b8228a --- a/README.md +++ b/README.md @@ -123,3 +123,29 @@ webInput/ ``` + + + +## Features + +Import templates +```html + +``` + +Load values: +```html +{{username}} +``` + +Number of iterations: +```html +User [[i]] +``` + +Subobjects: +```html +

+ {{user.username}} +

+``` diff --git a/color.go b/color.go old mode 100644 new mode 100755 diff --git a/errors.go b/errors.go old mode 100644 new mode 100755 diff --git a/example/konstrui b/example/konstrui index 56f1fe6..0502e50 100755 Binary files a/example/konstrui and b/example/konstrui differ diff --git a/example/webInput/css/app.css b/example/webInput/css/app.css old mode 100644 new mode 100755 diff --git a/example/webInput/css/colors.css b/example/webInput/css/colors.css old mode 100644 new mode 100755 diff --git a/example/webInput/index.html b/example/webInput/index.html old mode 100644 new mode 100755 diff --git a/example/webInput/js/scripts1.js b/example/webInput/js/scripts1.js old mode 100644 new mode 100755 diff --git a/example/webInput/js/scripts2.js b/example/webInput/js/scripts2.js old mode 100644 new mode 100755 diff --git a/example/webInput/konstruiConfig.json b/example/webInput/konstruiConfig.json old mode 100644 new mode 100755 diff --git a/example/webInput/projectPage.html b/example/webInput/projectPage.html old mode 100644 new mode 100755 diff --git a/example/webInput/projects.html b/example/webInput/projects.html old mode 100644 new mode 100755 diff --git a/example/webInput/templates/projectTemplate.html b/example/webInput/templates/projectTemplate.html old mode 100644 new mode 100755 diff --git a/example/webInput/templates/projectTemplate.json b/example/webInput/templates/projectTemplate.json old mode 100644 new mode 100755 diff --git a/example/webInput/templates/userTemplate.html b/example/webInput/templates/userTemplate.html old mode 100644 new mode 100755 diff --git a/example/webInput/templates/userTemplate.json b/example/webInput/templates/userTemplate.json old mode 100644 new mode 100755 diff --git a/extractText.go b/extractText.go new file mode 100644 index 0000000..491bb14 --- /dev/null +++ b/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("")) + bs, err := ioutil.ReadAll(rtr) + check(err) + return string(bs) +} diff --git a/fileOperations.go b/fileOperations.go old mode 100644 new mode 100755 index 5e36685..d615add --- a/fileOperations.go +++ b/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, "", "", -1) +func putDataInTemplate(templateContent string, entries []dataEntry) string { + var newContent string + newContent = templateContent + + //replace + if strings.Contains(newContent, "") { + //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, "") + 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, ">", "", -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, "") { - 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) diff --git a/konstrui b/konstrui index 56f1fe6..0502e50 100755 Binary files a/konstrui and b/konstrui differ diff --git a/main.go b/main.go old mode 100644 new mode 100755 index 215a008..176d726 --- a/main.go +++ b/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") } diff --git a/media/example.png b/media/example.png old mode 100644 new mode 100755 diff --git a/media/konstrui.png b/media/konstrui.png old mode 100644 new mode 100755 diff --git a/readKonstruiConfig.go b/readKonstruiConfig.go old mode 100644 new mode 100755