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