arnaucube
arnaucube/blogoPublic Self-hosted repository
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	m["[blogo-summary]"] = config.MetaDescr
43	m["[blogo-img]"] = config.AbsoluteUrl + "/" + config.MetaImg
44	m["[blogo-link]"] = config.AbsoluteUrl
45	r := putHTMLToTemplate(indexTemplate, m)
46	writeFile(outputDir+"/"+"index.html", r)
47
48	// generate posts pages
49
50	for _, post := range config.Posts {
51		mdcontent := readFile(directory + "/" + post.Md)
52		htmlcontent := string(blackfriday.Run([]byte(mdcontent)))
53
54		firstline := strings.Split(mdcontent, "\n")[0]
55		title := strings.Replace(firstline, "# ", "", -1)
56
57		filename := strings.Split(post.Md, ".")[0]
58
59		m := make(map[string]string)
60		m["[blogo-title]"] = title + " - " + config.Title
61		m["[blogo-content]"] = htmlcontent
62		m["[blogo-summary]"] = post.MetaDescr
63		m["[blogo-link]"] = config.AbsoluteUrl + "/" + filename + ".html"
64		m["[blogo-img]"] = config.AbsoluteUrl + "/" + post.MetaImg
65
66		r := putHTMLToTemplate(indexTemplate, m)
67		writeFile(outputDir+"/"+filename+".html", r)
68	}
69
70	//copy raw
71	fmt.Println("copying raw:")
72	for _, dir := range config.CopyRaw {
73		copyRaw(directory+"/"+dir, outputDir+"/")
74	}
75}
76
77func putHTMLToTemplate(template string, m map[string]string) string {
78	lines := getLines(template)
79	var resultL []string
80	for _, line := range lines {
81		inserted := false
82		for k, v := range m {
83			if strings.Contains(line, k) {
84				//in the line, change [tag] with the content
85				lineReplaced := strings.Replace(line, k, v, -1)
86				resultL = append(resultL, lineReplaced)
87				inserted = true
88			}
89		}
90		if inserted == false {
91			resultL = append(resultL, line)
92		}
93	}
94	result := concatStringsWithJumps(resultL)
95	return result
96}