diff --git a/.gitignore b/.gitignore index e5e5974..3d469ae 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -webInput -webOutput +/webInput +/webOutput diff --git a/README.md b/README.md index dc7449f..418f37a 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,9 @@ web templating engine for static websites, written in Go lang ## Example +#### See the full example on https://github.com/arnaucode/konstrui/tree/master/example + + - Simple project structure example: ``` diff --git a/example/konstrui b/example/konstrui new file mode 100755 index 0000000..56f1fe6 Binary files /dev/null and b/example/konstrui differ diff --git a/example/webInput/app.css b/example/webInput/app.css new file mode 100644 index 0000000..5ace24d --- /dev/null +++ b/example/webInput/app.css @@ -0,0 +1,59 @@ +.collection{ + border: 0!important; +} +.collection-item{ + background-color: rgba(0,0,0,0)!important; + border: 0px!important; +} +.collection-item:hover{ + background-color: rgba(255,255,255,0.1)!important; +} + + +/* login */ +.o_loginBackground{ + position: absolute; + + height: 100%!important; + width: 100%; + min-height: auto; + -webkit-background-size: cover; + -moz-background-size: cover; + background-size: cover; + -o-background-size: cover; + background-position: center; + background-image: url('img/loginBackground.jpg'); + text-align: center; + /*color: white;*/ + + /* Create the parallax scrolling effect */ + background-attachment: fixed; + background-position: center; + background-repeat: no-repeat; + background-size: cover; + + color: #ffffff; + +} +@font-face { + font-family: 'Otterly Adorable'; + src: url('fonts/fourHand_TRIAL.ttf'); +} +.o_loginBackground h1{ + color: #ffffff!important; + margin: 10px; + font-family: 'Otterly Adorable' + /*font-weight: bold;*/ +} + +.o_floatRight{ + float: right; +} +.o_textRight{ + text-align: right; +} +.o_badge{ + border-radius: 8px; + margin: 5px; + padding: 2px 5px; +} diff --git a/example/webInput/index.html b/example/webInput/index.html new file mode 100644 index 0000000..ad5dc14 --- /dev/null +++ b/example/webInput/index.html @@ -0,0 +1,10 @@ + + +
+My first paragraph.
+ +{{author}}
+{{description}}
+ + + diff --git a/example/webInput/projects.html b/example/webInput/projects.html new file mode 100644 index 0000000..81552eb --- /dev/null +++ b/example/webInput/projects.html @@ -0,0 +1,9 @@ + + + +User2
+This project is a botnet
+ + + diff --git a/example/webOutput/index.html b/example/webOutput/index.html new file mode 100644 index 0000000..b320339 --- /dev/null +++ b/example/webOutput/index.html @@ -0,0 +1,16 @@ +My first paragraph.
User1
+This project is a script to save time
+ + + diff --git a/example/webOutput/smartphoneAppPage.html b/example/webOutput/smartphoneAppPage.html new file mode 100644 index 0000000..d1514f0 --- /dev/null +++ b/example/webOutput/smartphoneAppPage.html @@ -0,0 +1,11 @@ + + +User1
+This is the first project
+ + + diff --git a/example/webOutput/webpagePage.html b/example/webOutput/webpagePage.html new file mode 100644 index 0000000..ce168e8 --- /dev/null +++ b/example/webOutput/webpagePage.html @@ -0,0 +1,11 @@ + + +User1
+This project is a webpage
+ + + diff --git a/fileOperations.go b/fileOperations.go index 35170cd..5e36685 100644 --- a/fileOperations.go +++ b/fileOperations.go @@ -12,8 +12,8 @@ import ( //dataEntry is the map used to create the array of maps, where the templatejson data is stored type dataEntry map[string]string -func readFile(folderPath string, filename string) string { - dat, err := ioutil.ReadFile(folderPath + "/" + filename) +func readFile(path string) string { + dat, err := ioutil.ReadFile(path) check(err) return string(dat) } @@ -78,7 +78,8 @@ func getTemplateParameters(line string) (string, string) { } func useTemplate(templatePath string, dataPath string) string { - templateContent := readFile(rawFolderPath, templatePath) + filepath := rawFolderPath + "/" + templatePath + templateContent := readFile(filepath) entries := getDataFromJson(rawFolderPath + "/" + dataPath) generated := generateFromTemplateAndData(templateContent, entries) return generated @@ -107,3 +108,23 @@ func writeFile(path string, newContent string) { err := ioutil.WriteFile(path, []byte(newContent), 0644) check(err) } + +func generatePageFromTemplateAndData(templateContent string, entry dataEntry) string { + var entryContent string + entryContent = templateContent + //first, get the map keys + var keys []string + for key, _ := range entry { + keys = append(keys, key) + } + //now, replace the keys with the values + 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) + return templateContent, data +} diff --git a/konstrui b/konstrui index 5f6daf4..56f1fe6 100755 Binary files a/konstrui and b/konstrui differ diff --git a/main.go b/main.go index 2ff5811..47bd9d7 100644 --- a/main.go +++ b/main.go @@ -20,7 +20,8 @@ func parseDir(folderPath string, newDir string) { fileContent := putTemplates(folderPath, f.Name()) writeFile(newDir+"/"+f.Name(), fileContent) } else if extension == "css" { - fileContent := readFile(folderPath, f.Name()) + path := folderPath + "/" + f.Name() + fileContent := readFile(path) writeFile(newDir+"/"+f.Name(), fileContent) } if len(fileNameSplitted) == 1 { @@ -42,10 +43,22 @@ func startTemplating(folderPath string, newDir string) { fileContent := putTemplates(folderPath, fName) writeFile(newDir+"/"+fName, fileContent) } else if extension == "css" { - fileContent := readFile(folderPath, fName) + path := folderPath + "/" + fName + fileContent := readFile(path) writeFile(newDir+"/"+fName, fileContent) } } + + 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]) + writeFile(newDir+"/"+data[j]["pageName"]+"Page.html", generatedPage) + } + } } func main() { c.Green("getting files from /webInput") diff --git a/readKonstruiConfig.go b/readKonstruiConfig.go index a5d181c..2924232 100644 --- a/readKonstruiConfig.go +++ b/readKonstruiConfig.go @@ -6,13 +6,20 @@ import ( "io/ioutil" ) +//RepeatPages is from the json config, is an array inside KonstruiConfig +type RepeatPages struct { + HtmlPage string `json:"htmlPage"` + Data string `json:"data"` +} + //KonstruiConfig is the configuration from the file konstruiConfig.json, on the folder /webInput type KonstruiConfig struct { - Title string `json:"title"` - Author string `json:"author"` - Github string `json:"github"` - Website string `json:"website"` - Files []string `json:"files"` + Title string `json:"title"` + Author string `json:"author"` + Github string `json:"github"` + Website string `json:"website"` + Files []string `json:"files"` + RepeatPages []RepeatPages } var konstruiConfig KonstruiConfig