arnaucube
arnaucube/blogoPublic Self-hosted repository
blogo/main.go
main.goRaw
  1package main
  2
  3import (
  4	"flag"
  5	"fmt"
  6	"log"
  7	"net/http"
  8	"os"
  9	"strings"
 10
 11	"github.com/gomarkdown/markdown"
 12	"github.com/gomarkdown/markdown/parser"
 13)
 14
 15const version = "v0_20221124"
 16const directory = "blogo-input"
 17const defaultOutputDir = "public"
 18
 19func main() {
 20	devMode := flag.Bool("d", false, "dev mode")
 21	port := flag.String("p", "8080", "port (only for dev mode)")
 22	flag.Parse()
 23
 24	fmt.Println("Blogo version:", version)
 25	readConfig(directory + "/blogo.json")
 26	_ = os.Mkdir(config.OutputDir, os.ModePerm)
 27
 28	fmt.Println("devMode:", *devMode)
 29	generateHTML()
 30	if !*devMode {
 31		return
 32	}
 33
 34	go watch("./blogo-input")
 35	// TODO public not watched, until a way to force browser refresh
 36	// go watch("./public")
 37
 38	// serve files
 39	fs := http.FileServer(http.Dir(config.OutputDir))
 40	http.Handle(config.RelativePath+"/", http.StripPrefix(config.RelativePath, fs))
 41	fmt.Printf("Blog being served in: \n http://127.0.0.1:%s%s\n http://localhost:%s%s\n",
 42		*port, config.RelativePath, *port, config.RelativePath)
 43	log.Fatal(http.ListenAndServe(":"+*port, nil))
 44}
 45
 46func generateHTML() {
 47	readConfig(directory + "/blogo.json")
 48	fmt.Println(config)
 49
 50	mdExtensions := parser.NoIntraEmphasis | parser.Tables | parser.FencedCode |
 51		parser.Autolink | parser.Strikethrough | parser.SpaceHeadings | parser.HeadingIDs |
 52		parser.BackslashLineBreak | parser.DefinitionLists | parser.MathJax
 53
 54	// generate index page
 55	indexTemplate := readFile(directory + "/" + config.IndexTemplate)
 56	indexPostTemplate := readFile(directory + "/" + config.PostThumbTemplate)
 57	var blogoIndex string
 58	blogoIndex = ""
 59	for _, post := range config.Posts {
 60		mdpostthumb := readFile(directory + "/" + config.PostsDir + post.Thumb)
 61		mdParser := parser.NewWithExtensions(mdExtensions)
 62		htmlpostthumb := markdown.ToHTML([]byte(mdpostthumb), mdParser, nil)
 63
 64		//put the htmlpostthumb in the blogo-index-post-template
 65		m := make(map[string]string)
 66		m["[blogo-index-post-template]"] = string(htmlpostthumb)
 67		r := putHTMLToTemplate(indexPostTemplate, m)
 68		if post.OutsideArticle != "" {
 69			r = "<a href='" + post.OutsideArticle + "'>" + r + "</a>"
 70		} else {
 71			filename := strings.Split(post.Md, ".")[0]
 72			r = "<a href='" + config.RelativePath + "/" + filename + ".html'>" + r + "</a>"
 73		}
 74		blogoIndex = blogoIndex + r
 75	}
 76	//put the blogoIndex in the index.html
 77	m := make(map[string]string)
 78	m["[blogo-title]"] = config.Title
 79	m["[blogo-content]"] = blogoIndex
 80	m["[blogo-summary]"] = config.MetaDescr
 81	m["[blogo-img]"] = config.AbsoluteUrl + "/" + config.MetaImg
 82	m["[blogo-link]"] = config.AbsoluteUrl
 83	r := putHTMLToTemplate(indexTemplate, m)
 84	writeFile(config.OutputDir+"/"+"index.html", r)
 85
 86	// generate posts pages
 87
 88	for _, post := range config.Posts {
 89		if post.OutsideArticle != "" {
 90			continue
 91		}
 92		mdcontent := readFile(directory + "/" + config.PostsDir + post.Md)
 93		mdParser := parser.NewWithExtensions(mdExtensions)
 94		htmlcontent := markdown.ToHTML([]byte(mdcontent), mdParser, nil)
 95
 96		firstline := strings.Split(mdcontent, "\n")[0]
 97		title := strings.Replace(firstline, "#", "", -1)
 98
 99		filename := strings.Split(post.Md, ".")[0]
100
101		m := make(map[string]string)
102		m["[blogo-title]"] = title + " - " + config.Title
103		m["[blogo-content]"] = string(htmlcontent)
104		m["[blogo-summary]"] = post.MetaDescr
105		m["[blogo-link]"] = config.AbsoluteUrl + "/" + filename + ".html"
106		m["[blogo-img]"] = config.AbsoluteUrl + "/" + post.MetaImg
107
108		r := putHTMLToTemplate(indexTemplate, m)
109		writeFile(config.OutputDir+"/"+filename+".html", r)
110	}
111
112	//copy raw
113	fmt.Println("copying raw:")
114	for _, dir := range config.CopyRaw {
115		copyRaw(directory+"/"+dir, config.OutputDir+"/")
116	}
117}
118
119func putHTMLToTemplate(template string, m map[string]string) string {
120	lines := getLines(template)
121	var resultL []string
122	for _, line := range lines {
123		inserted := false
124		for k, v := range m {
125			if strings.Contains(line, k) {
126				//in the line, change [tag] with the content
127				lineReplaced := strings.Replace(line, k, v, -1)
128				resultL = append(resultL, lineReplaced)
129				inserted = true
130			}
131		}
132		if inserted == false {
133			resultL = append(resultL, line)
134		}
135	}
136	result := concatStringsWithJumps(resultL)
137	return result
138}