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.

129 lines
3.7 KiB

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