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 = "blogo-input"
11
12func main() {
13 readConfig(directory + "/blogo.json")
14 fmt.Println(config)
15
16 // generate index page
17 indexTemplate := readFile(directory + "/" + config.IndexTemplate)
18 indexPostTemplate := readFile(directory + "/" + config.PostThumbTemplate)
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 filename := strings.Split(post.Md, ".")[0]
30 r = "<a href='" + config.RelativePath + "/" + filename + ".html'>" + r + "</a>"
31 blogoIndex = blogoIndex + r
32 }
33 //put the blogoIndex in the index.html
34 m := make(map[string]string)
35 m["[blogo-title]"] = config.Title
36 m["[blogo-content]"] = blogoIndex
37 r := putHTMLToTemplate(indexTemplate, m)
38 writeFile("index.html", r)
39
40 // generate posts pages
41
42 for _, post := range config.Posts {
43 mdcontent := readFile(directory + "/" + post.Md)
44 htmlcontent := string(blackfriday.Run([]byte(mdcontent)))
45
46 m := make(map[string]string)
47 m["[blogo-title]"] = config.Title
48 m["[blogo-content]"] = htmlcontent
49
50 r := putHTMLToTemplate(indexTemplate, m)
51 //fmt.Println(r)
52
53 filename := strings.Split(post.Md, ".")[0]
54 writeFile(filename+".html", r)
55 }
56
57 //copy raw
58 fmt.Println("copying raw:")
59 for _, dir := range config.CopyRaw {
60 copyRaw(directory+"/"+dir, ".")
61 }
62}
63
64func putHTMLToTemplate(template string, m map[string]string) string {
65 lines := getLines(template)
66 var resultL []string
67 for _, line := range lines {
68 inserted := false
69 for k, v := range m {
70 if strings.Contains(line, k) {
71 //in the line, change [tag] with the content
72 lineReplaced := strings.Replace(line, k, v, -1)
73 resultL = append(resultL, lineReplaced)
74 inserted = true
75 }
76 }
77 if inserted == false {
78 resultL = append(resultL, line)
79 }
80 }
81 result := concatStringsWithJumps(resultL)
82 return result
83}