Browse Source

implemented generation of pages for each data in json

master
arnaucode 7 years ago
parent
commit
186d170ffd
23 changed files with 364 additions and 12 deletions
  1. +2
    -2
      .gitignore
  2. +3
    -0
      README.md
  3. BIN
      example/konstrui
  4. +59
    -0
      example/webInput/app.css
  5. +10
    -0
      example/webInput/index.html
  6. +15
    -0
      example/webInput/konstruiConfig.json
  7. +11
    -0
      example/webInput/projectPage.html
  8. +9
    -0
      example/webInput/projects.html
  9. +6
    -0
      example/webInput/templates/projectTemplate.html
  10. +33
    -0
      example/webInput/templates/projectTemplate.json
  11. +5
    -0
      example/webInput/templates/userTemplate.html
  12. +16
    -0
      example/webInput/templates/userTemplate.json
  13. +59
    -0
      example/webOutput/app.css
  14. +11
    -0
      example/webOutput/botnetPage.html
  15. +16
    -0
      example/webOutput/index.html
  16. +25
    -0
      example/webOutput/projects.html
  17. +11
    -0
      example/webOutput/scriptPage.html
  18. +11
    -0
      example/webOutput/smartphoneAppPage.html
  19. +11
    -0
      example/webOutput/webpagePage.html
  20. +24
    -3
      fileOperations.go
  21. BIN
      konstrui
  22. +15
    -2
      main.go
  23. +12
    -5
      readKonstruiConfig.go

+ 2
- 2
.gitignore

@ -1,2 +1,2 @@
webInput
webOutput
/webInput
/webOutput

+ 3
- 0
README.md

@ -8,6 +8,9 @@ web templating engine for static websites, written in Go lang
## Example ## Example
#### See the full example on https://github.com/arnaucode/konstrui/tree/master/example
- Simple project structure example: - Simple project structure example:
``` ```

BIN
example/konstrui


+ 59
- 0
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;
}

+ 10
- 0
example/webInput/index.html

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<konstrui-template html="templates/userTemplate.html" data="templates/userTemplate.json"></konstrui-template>
</body>
</html>

+ 15
- 0
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"
}]
}

+ 11
- 0
example/webInput/projectPage.html

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<title>{{title}} - {{author}}</title>
<body>
<h1>Project individual page</h1>
<h2>{{title}}</h2>
<p>{{author}}</p>
<p>{{description}}</p>
</body>
</html>

+ 9
- 0
example/webInput/projects.html

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<body>
<h1>Projects page</h1>
<konstrui-template html="templates/projectTemplate.html" data="templates/projectTemplate.json"></konstrui-template>
</body>
</html>

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

@ -0,0 +1,6 @@
<div class="class1">
<div id="{{idAuthor}}" class="class2">Project {{title}}</div>
<div class="classAuthor">Developed by: {{author}}</div>
<div class="class3">{{description}}</div>
<a href="{{link}}" target="_blank">View project page</a>
</div>

+ 33
- 0
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"
}
]

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

@ -0,0 +1,5 @@
<div class="class1" id="user[[i]]">
<div id="username[[i]]" class="class2">{{username}}</div>
<div id="description[[i]]" class="class2">{{description}}</div>
<div class="class2">{{phone}}</div>
</div>

+ 16
- 0
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"
}
]

+ 59
- 0
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;
}

+ 11
- 0
example/webOutput/botnetPage.html

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<title>Botnet - User2</title>
<body>
<h1>Project individual page</h1>
<h2>Botnet</h2>
<p>User2</p>
<p>This project is a botnet</p>
</body>
</html>

+ 16
- 0
example/webOutput/index.html

@ -0,0 +1,16 @@
<!DOCTYPE html><html><body><h1>My First Heading</h1><p>My first paragraph.</p><div class="class1" id="user0">
<div id="username0" class="class2">Michaela Doe</div>
<div id="description0" class="class2">Hi, I'm here to code</div>
<div class="class2">456456456</div>
</div>
<div class="class1" id="user1">
<div id="username1" class="class2">John Doe</div>
<div id="description1" class="class2">Hi, I'm here</div>
<div class="class2">123456789</div>
</div>
<div class="class1" id="user2">
<div id="username2" class="class2">Myself</div>
<div id="description2" class="class2">How are you</div>
<div class="class2">no phone</div>
</div>
</body></html>

+ 25
- 0
example/webOutput/projects.html

@ -0,0 +1,25 @@
<!DOCTYPE html><html><body><h1>Projects page</h1><div class="class1">
<div id="author1" class="class2">Project Smartphone App</div>
<div class="classAuthor">Developed by: User1</div>
<div class="class3">This is the first project</div>
<a href="https://github.com" target="_blank">View project page</a>
</div>
<div class="class1">
<div id="author2" class="class2">Project Botnet</div>
<div class="classAuthor">Developed by: User2</div>
<div class="class3">This project is a botnet</div>
<a href="https://github.com" target="_blank">View project page</a>
</div>
<div class="class1">
<div id="author3" class="class2">Project Webpage</div>
<div class="classAuthor">Developed by: User1</div>
<div class="class3">This project is a webpage</div>
<a href="https://github.com" target="_blank">View project page</a>
</div>
<div class="class1">
<div id="author4" class="class2">Project Script</div>
<div class="classAuthor">Developed by: User1</div>
<div class="class3">This project is a script to save time</div>
<a href="https://github.com" target="_blank">View project page</a>
</div>
</body></html>

+ 11
- 0
example/webOutput/scriptPage.html

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<title>Script - User1</title>
<body>
<h1>Project individual page</h1>
<h2>Script</h2>
<p>User1</p>
<p>This project is a script to save time</p>
</body>
</html>

+ 11
- 0
example/webOutput/smartphoneAppPage.html

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<title>Smartphone App - User1</title>
<body>
<h1>Project individual page</h1>
<h2>Smartphone App</h2>
<p>User1</p>
<p>This is the first project</p>
</body>
</html>

+ 11
- 0
example/webOutput/webpagePage.html

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<title>Webpage - User1</title>
<body>
<h1>Project individual page</h1>
<h2>Webpage</h2>
<p>User1</p>
<p>This project is a webpage</p>
</body>
</html>

+ 24
- 3
fileOperations.go

@ -12,8 +12,8 @@ import (
//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
type dataEntry map[string]string 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) check(err)
return string(dat) return string(dat)
} }
@ -78,7 +78,8 @@ func getTemplateParameters(line string) (string, string) {
} }
func useTemplate(templatePath string, dataPath string) string { func useTemplate(templatePath string, dataPath string) string {
templateContent := readFile(rawFolderPath, templatePath)
filepath := rawFolderPath + "/" + templatePath
templateContent := readFile(filepath)
entries := getDataFromJson(rawFolderPath + "/" + dataPath) entries := getDataFromJson(rawFolderPath + "/" + dataPath)
generated := generateFromTemplateAndData(templateContent, entries) generated := generateFromTemplateAndData(templateContent, entries)
return generated return generated
@ -107,3 +108,23 @@ func writeFile(path string, newContent string) {
err := ioutil.WriteFile(path, []byte(newContent), 0644) err := ioutil.WriteFile(path, []byte(newContent), 0644)
check(err) 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
}

BIN
konstrui


+ 15
- 2
main.go

@ -20,7 +20,8 @@ func parseDir(folderPath string, newDir string) {
fileContent := putTemplates(folderPath, f.Name()) fileContent := putTemplates(folderPath, f.Name())
writeFile(newDir+"/"+f.Name(), fileContent) writeFile(newDir+"/"+f.Name(), fileContent)
} else if extension == "css" { } else if extension == "css" {
fileContent := readFile(folderPath, f.Name())
path := folderPath + "/" + f.Name()
fileContent := readFile(path)
writeFile(newDir+"/"+f.Name(), fileContent) writeFile(newDir+"/"+f.Name(), fileContent)
} }
if len(fileNameSplitted) == 1 { if len(fileNameSplitted) == 1 {
@ -42,10 +43,22 @@ func startTemplating(folderPath string, newDir string) {
fileContent := putTemplates(folderPath, fName) fileContent := putTemplates(folderPath, fName)
writeFile(newDir+"/"+fName, fileContent) writeFile(newDir+"/"+fName, fileContent)
} else if extension == "css" { } else if extension == "css" {
fileContent := readFile(folderPath, fName)
path := folderPath + "/" + fName
fileContent := readFile(path)
writeFile(newDir+"/"+fName, fileContent) 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() { func main() {
c.Green("getting files from /webInput") c.Green("getting files from /webInput")

+ 12
- 5
readKonstruiConfig.go

@ -6,13 +6,20 @@ import (
"io/ioutil" "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 //KonstruiConfig is the configuration from the file konstruiConfig.json, on the folder /webInput
type KonstruiConfig struct { 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 var konstruiConfig KonstruiConfig

Loading…
Cancel
Save