You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.9 KiB

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