blogo/main.go
main.goRaw
1package main
2
3import (
4 "fmt"
5 "strings"
6
7 blackfriday "gopkg.in/russross/blackfriday.v2"
8)
9
10const directory = "input"
11
12func main() {
13 readConfig("input/config.json")
14 fmt.Println(config)
15
16 // generate index page
17 indexTemplate := readFile(directory + "/" + config.IndexTemplate)
18 indexPostTemplate := readFile(directory + "/" + config.IndexPostTemplate)
19 var blogoIndex string
20 blogoIndex = ""
21 for _, post := range config.Posts {
22 mdpostthumb := readFile(directory + "/" + post.Thumb)
23 htmlpostthumb := string(blackfriday.Run([]byte(mdpostthumb)))
24
25 //put the htmlpostthumb in the blogo-index-post-template
26 m := make(map[string]string)
27 m["blogo-index-post-template"] = htmlpostthumb
28 r := putHTMLToTemplate(indexPostTemplate, m)
29 blogoIndex = blogoIndex + r
30 }
31 //put the blogoIndex in the index.html
32 m := make(map[string]string)
33 m["blogo-title"] = config.Title
34 m["blogo-index"] = blogoIndex
35 r := putHTMLToTemplate(indexTemplate, m)
36 writeFile("index.html", r)
37
38 // generate posts pages
39 postTemplate := readFile(directory + "/" + config.PostTemplate)
40
41 for _, post := range config.Posts {
42 mdcontent := readFile(directory + "/" + post.Md)
43 htmlcontent := string(blackfriday.Run([]byte(mdcontent)))
44
45 m := make(map[string]string)
46 m["blogo-post-title"] = post.Title
47 m["blogo-post-content"] = htmlcontent
48
49 r := putHTMLToTemplate(postTemplate, m)
50 //fmt.Println(r)
51
52 filename := strings.Split(post.Md, ".")[0]
53 writeFile(filename+".html", r)
54 }
55
56 //copy raw
57 fmt.Println("copying raw:")
58 for _, dir := range config.CopyRaw {
59 copyRaw(directory+"/"+dir, dir)
60 }
61}
62
63func putHTMLToTemplate(template string, m map[string]string) string {
64 lines := getLines(template)
65 var resultL []string
66 for _, line := range lines {
67 inserted := false
68 for k, v := range m {
69 if strings.Contains(line, k) {
70 resultL = append(resultL, v)
71 inserted = true
72 }
73 }
74 if inserted == false {
75 resultL = append(resultL, line)
76 }
77 }
78 result := concatStringsWithJumps(resultL)
79 return result
80}