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 Heading

+

My first paragraph.

+ + + + + diff --git a/example/webInput/konstruiConfig.json b/example/webInput/konstruiConfig.json new file mode 100644 index 0000000..9790d42 --- /dev/null +++ b/example/webInput/konstruiConfig.json @@ -0,0 +1,15 @@ +{ + "title": "Prova web", + "author": "arnaucode", + "github": "github.com/arnaucode", + "website": "arnaucode.com", + "files": [ + "index.html", + "projects.html", + "app.css" + ], + "repeatPages": [{ + "htmlPage": "projectPage.html", + "data": "templates/projectTemplate.json" + }] +} diff --git a/example/webInput/projectPage.html b/example/webInput/projectPage.html new file mode 100644 index 0000000..ac8c245 --- /dev/null +++ b/example/webInput/projectPage.html @@ -0,0 +1,11 @@ + + +{{title}} - {{author}} + +

Project individual page

+

{{title}}

+

{{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 @@ + + + +

Projects page

+ + + + + diff --git a/example/webInput/templates/projectTemplate.html b/example/webInput/templates/projectTemplate.html new file mode 100644 index 0000000..a66fe21 --- /dev/null +++ b/example/webInput/templates/projectTemplate.html @@ -0,0 +1,6 @@ +
+
Project {{title}}
+
Developed by: {{author}}
+
{{description}}
+ View project page +
diff --git a/example/webInput/templates/projectTemplate.json b/example/webInput/templates/projectTemplate.json new file mode 100644 index 0000000..944137c --- /dev/null +++ b/example/webInput/templates/projectTemplate.json @@ -0,0 +1,33 @@ +[{ + "title": "Smartphone App", + "idAuthor": "author1", + "author": "User1", + "description": "This is the first project", + "link": "https://github.com", + "pageName": "smartphoneApp" + }, + { + "title": "Botnet", + "idAuthor": "author2", + "author": "User2", + "description": "This project is a botnet", + "link": "https://github.com", + "pageName": "botnet" + }, + { + "title": "Webpage", + "idAuthor": "author3", + "author": "User1", + "description": "This project is a webpage", + "link": "https://github.com", + "pageName": "webpage" + }, + { + "title": "Script", + "idAuthor": "author4", + "author": "User1", + "description": "This project is a script to save time", + "link": "https://github.com", + "pageName": "script" + } +] diff --git a/example/webInput/templates/userTemplate.html b/example/webInput/templates/userTemplate.html new file mode 100644 index 0000000..db8542c --- /dev/null +++ b/example/webInput/templates/userTemplate.html @@ -0,0 +1,5 @@ +
+
{{username}}
+
{{description}}
+
{{phone}}
+
diff --git a/example/webInput/templates/userTemplate.json b/example/webInput/templates/userTemplate.json new file mode 100644 index 0000000..438c66c --- /dev/null +++ b/example/webInput/templates/userTemplate.json @@ -0,0 +1,16 @@ +[{ + "username": "Michaela Doe", + "description": "Hi, I'm here to code", + "phone": "456456456" + }, + { + "username": "John Doe", + "description": "Hi, I'm here", + "phone": "123456789" + }, + { + "username": "Myself", + "description": "How are you", + "phone": "no phone" + } +] diff --git a/example/webOutput/app.css b/example/webOutput/app.css new file mode 100644 index 0000000..5ace24d --- /dev/null +++ b/example/webOutput/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/webOutput/botnetPage.html b/example/webOutput/botnetPage.html new file mode 100644 index 0000000..32877d8 --- /dev/null +++ b/example/webOutput/botnetPage.html @@ -0,0 +1,11 @@ + + +Botnet - User2 + +

Project individual page

+

Botnet

+

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 Heading

My first paragraph.

+
Michaela Doe
+
Hi, I'm here to code
+
456456456
+
+
+
John Doe
+
Hi, I'm here
+
123456789
+
+
+
Myself
+
How are you
+
no phone
+
+ \ No newline at end of file diff --git a/example/webOutput/projects.html b/example/webOutput/projects.html new file mode 100644 index 0000000..c7c03b0 --- /dev/null +++ b/example/webOutput/projects.html @@ -0,0 +1,25 @@ +

Projects page

+
Project Smartphone App
+
Developed by: User1
+
This is the first project
+ View project page +
+
+
Project Botnet
+
Developed by: User2
+
This project is a botnet
+ View project page +
+
+
Project Webpage
+
Developed by: User1
+
This project is a webpage
+ View project page +
+
+
Project Script
+
Developed by: User1
+
This project is a script to save time
+ View project page +
+ \ No newline at end of file diff --git a/example/webOutput/scriptPage.html b/example/webOutput/scriptPage.html new file mode 100644 index 0000000..8b01999 --- /dev/null +++ b/example/webOutput/scriptPage.html @@ -0,0 +1,11 @@ + + +Script - User1 + +

Project individual page

+

Script

+

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 @@ + + +Smartphone App - User1 + +

Project individual page

+

Smartphone App

+

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 @@ + + +Webpage - User1 + +

Project individual page

+

Webpage

+

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