diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..116a0dc --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.html +input +css +js diff --git a/README.md b/README.md new file mode 100644 index 0000000..f34bf52 --- /dev/null +++ b/README.md @@ -0,0 +1,81 @@ +# Blogo +Static blog generator, templating engine from markdown and html templates + +Types of blogo tags: +- index.html +``` +[blogo-title] +[blogo-index] +``` +- postTemplate.html +``` +[blogo-post-title] +[blogo-post-md] +``` + + +Example of config.json: +```json +{ + "title": "my blog", + "indexTemplate": "index.html", + "indexPostTemplate": "indexPostTemplate.html", + "postTemplate": "postTemplate.html", + "posts": [ + { + "title": "Post 01", + "thumb": "post01thumb.md", + "md": "post01.md" + }, + { + "title": "Post 02", + "thumb": "post02thumb.md", + "md": "post02.md" + } + ] +} + +``` + + +Example of input files: +- index.html + +```html + + + + [blogo-title] + + + +[blogo-index] + + + +``` + +- indexPostTemplate.html + +```html +
+ [blogo-index-post-template] +
+ +``` + +- postTemplate.html + +```html + + + + [blogo-post-title] + + + +[blogo-post-content] + + + +``` diff --git a/blogo b/blogo new file mode 100755 index 0000000..3e8f7b6 Binary files /dev/null and b/blogo differ diff --git a/errors.go b/errors.go new file mode 100755 index 0000000..b3cf6b2 --- /dev/null +++ b/errors.go @@ -0,0 +1,15 @@ +package main + +import ( + "log" + "runtime" +) + +func check(err error) { + if err != nil { + _, fn, line, _ := runtime.Caller(1) + log.Println(line) + log.Println(fn) + log.Println(err) + } +} diff --git a/files.go b/files.go new file mode 100644 index 0000000..550cd1e --- /dev/null +++ b/files.go @@ -0,0 +1,45 @@ +package main + +import ( + "io/ioutil" + "os/exec" + "strings" + + "github.com/fatih/color" +) + +func readFile(path string) string { + dat, err := ioutil.ReadFile(path) + if err != nil { + color.Red(path) + } + check(err) + return string(dat) +} + +func writeFile(path string, newContent string) { + err := ioutil.WriteFile(path, []byte(newContent), 0644) + check(err) + + color.Green(path) + //color.Blue(newContent) +} + +func getLines(text string) []string { + lines := strings.Split(text, "\n") + return lines +} + +func concatStringsWithJumps(lines []string) string { + var r string + for _, l := range lines { + r = r + l + "\n" + } + return r +} + +func copyRaw(original string, destination string) { + color.Green(destination) + _, err := exec.Command("cp", "-r", original, destination).Output() + check(err) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..dbd0a5a --- /dev/null +++ b/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "strings" + + blackfriday "gopkg.in/russross/blackfriday.v2" +) + +const directory = "input" + +func main() { + readConfig("input/config.json") + fmt.Println(config) + + // generate index page + indexTemplate := readFile(directory + "/" + config.IndexTemplate) + indexPostTemplate := readFile(directory + "/" + config.IndexPostTemplate) + var blogoIndex string + blogoIndex = "" + for _, post := range config.Posts { + mdpostthumb := readFile(directory + "/" + post.Thumb) + htmlpostthumb := string(blackfriday.Run([]byte(mdpostthumb))) + + //put the htmlpostthumb in the blogo-index-post-template + m := make(map[string]string) + m["blogo-index-post-template"] = htmlpostthumb + r := putHTMLToTemplate(indexPostTemplate, m) + blogoIndex = blogoIndex + r + } + //put the blogoIndex in the index.html + m := make(map[string]string) + m["blogo-title"] = config.Title + m["blogo-index"] = blogoIndex + r := putHTMLToTemplate(indexTemplate, m) + writeFile("index.html", r) + + // generate posts pages + postTemplate := readFile(directory + "/" + config.PostTemplate) + + for _, post := range config.Posts { + mdcontent := readFile(directory + "/" + post.Md) + htmlcontent := string(blackfriday.Run([]byte(mdcontent))) + + m := make(map[string]string) + m["blogo-post-title"] = post.Title + m["blogo-post-content"] = htmlcontent + + r := putHTMLToTemplate(postTemplate, m) + //fmt.Println(r) + + filename := strings.Split(post.Md, ".")[0] + writeFile(filename+".html", r) + } + + //copy raw + fmt.Println("copying raw:") + for _, dir := range config.CopyRaw { + copyRaw(directory+"/"+dir, dir) + } +} + +func putHTMLToTemplate(template string, m map[string]string) string { + lines := getLines(template) + var resultL []string + for _, line := range lines { + inserted := false + for k, v := range m { + if strings.Contains(line, k) { + resultL = append(resultL, v) + inserted = true + } + } + if inserted == false { + resultL = append(resultL, line) + } + } + result := concatStringsWithJumps(resultL) + return result +} diff --git a/readConfig.go b/readConfig.go new file mode 100755 index 0000000..6cf275b --- /dev/null +++ b/readConfig.go @@ -0,0 +1,29 @@ +package main + +import ( + "encoding/json" + "io/ioutil" +) + +type Post struct { + Title string `json:"title"` + Thumb string `json:"thumb"` + Md string `json:"md"` +} +type Config struct { + Title string `json:"title"` + IndexTemplate string `json:"indexTemplate"` + IndexPostTemplate string `json:"indexPostTemplate"` + PostTemplate string `json:"postTemplate"` + Posts []Post `json:"posts"` + CopyRaw []string `json:"copyRaw"` +} + +var config Config + +func readConfig(path string) { + file, err := ioutil.ReadFile(path) + check(err) + content := string(file) + json.Unmarshal([]byte(content), &config) +}