blogo/main.go
main.goRaw
1package main
2
3import (
4 "fmt"
5 "os"
6 "strings"
7
8 // blackfriday "gopkg.in/russross/blackfriday.v2"
9 "github.com/russross/blackfriday"
10)
11
12const directory = "blogo-input"
13const outputDir = "public"
14
15func main() {
16 readConfig(directory + "/blogo.json")
17 fmt.Println(config)
18
19 _ = os.Mkdir(outputDir, os.ModePerm)
20
21 // generate index page
22 indexTemplate := readFile(directory + "/" + config.IndexTemplate)
23 indexPostTemplate := readFile(directory + "/" + config.PostThumbTemplate)
24 var blogoIndex string
25 blogoIndex = ""
26 for _, post := range config.Posts {
27 mdpostthumb := readFile(directory + "/" + post.Thumb)
28 htmlpostthumb := string(blackfriday.Run([]byte(mdpostthumb)))
29
30 //put the htmlpostthumb in the blogo-index-post-template
31 m := make(map[string]string)
32 m["[blogo-index-post-template]"] = htmlpostthumb
33 r := putHTMLToTemplate(indexPostTemplate, m)
34 filename := strings.Split(post.Md, ".")[0]
35 r = "<a href='" + config.RelativePath + "/" + filename + ".html'>" + r + "</a>"
36 blogoIndex = blogoIndex + r
37 }
38 //put the blogoIndex in the index.html
39 m := make(map[string]string)
40 m["[blogo-title]"] = config.Title
41 m["[blogo-content]"] = blogoIndex
42 r := putHTMLToTemplate(indexTemplate, m)
43 writeFile(outputDir+"/"+"index.html", r)
44
45 // generate posts pages
46
47 for _, post := range config.Posts {
48 mdcontent := readFile(directory + "/" + post.Md)
49 htmlcontent := string(blackfriday.Run([]byte(mdcontent)))
50
51 firstline := strings.Split(mdcontent, "\n")[0]
52 title := strings.Replace(firstline, "# ", "", -1)
53
54 filename := strings.Split(post.Md, ".")[0]
55
56 m := make(map[string]string)
57 m["[blogo-title]"] = title + " - " + config.Title
58 m["[blogo-content]"] = htmlcontent
59 m["[blogo-summary]"] = post.MetaDescr
60 m["[blogo-link]"] = config.AbsoluteUrl + "/" + filename + ".html"
61 m["[blogo-img]"] = config.AbsoluteUrl + "/" + post.MetaImg
62
63 r := putHTMLToTemplate(indexTemplate, m)
64 writeFile(outputDir+"/"+filename+".html", r)
65 }
66
67 //copy raw
68 fmt.Println("copying raw:")
69 for _, dir := range config.CopyRaw {
70 copyRaw(directory+"/"+dir, outputDir+"/")
71 }
72}
73
74func putHTMLToTemplate(template string, m map[string]string) string {
75 lines := getLines(template)
76 var resultL []string
77 for _, line := range lines {
78 inserted := false
79 for k, v := range m {
80 if strings.Contains(line, k) {
81 //in the line, change [tag] with the content
82 lineReplaced := strings.Replace(line, k, v, -1)
83 resultL = append(resultL, lineReplaced)
84 inserted = true
85 }
86 }
87 if inserted == false {
88 resultL = append(resultL, line)
89 }
90 }
91 result := concatStringsWithJumps(resultL)
92 return result
93}