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 firstline := strings.Split(mdcontent, "\n")[0]
47 title := strings.Replace(firstline, "# ", "", -1)
48
49 m := make(map[string]string)
50 m["[blogo-title]"] = title + " - " + config.Title
51 m["[blogo-content]"] = htmlcontent
52
53 r := putHTMLToTemplate(indexTemplate, m)
54 //fmt.Println(r)
55
56 filename := strings.Split(post.Md, ".")[0]
57 writeFile(filename+".html", r)
58 }
59
60 //copy raw
61 fmt.Println("copying raw:")
62 for _, dir := range config.CopyRaw {
63 copyRaw(directory+"/"+dir, ".")
64 }
65}
66
67func putHTMLToTemplate(template string, m map[string]string) string {
68 lines := getLines(template)
69 var resultL []string
70 for _, line := range lines {
71 inserted := false
72 for k, v := range m {
73 if strings.Contains(line, k) {
74 //in the line, change [tag] with the content
75 lineReplaced := strings.Replace(line, k, v, -1)
76 resultL = append(resultL, lineReplaced)
77 inserted = true
78 }
79 }
80 if inserted == false {
81 resultL = append(resultL, line)
82 }
83 }
84 result := concatStringsWithJumps(resultL)
85 return result
86}