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.

138 lines
4.1 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
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_20221124"
  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. if post.OutsideArticle != "" {
  57. r = "<a href='" + post.OutsideArticle + "'>" + r + "</a>"
  58. } else {
  59. filename := strings.Split(post.Md, ".")[0]
  60. r = "<a href='" + config.RelativePath + "/" + filename + ".html'>" + r + "</a>"
  61. }
  62. blogoIndex = blogoIndex + r
  63. }
  64. //put the blogoIndex in the index.html
  65. m := make(map[string]string)
  66. m["[blogo-title]"] = config.Title
  67. m["[blogo-content]"] = blogoIndex
  68. m["[blogo-summary]"] = config.MetaDescr
  69. m["[blogo-img]"] = config.AbsoluteUrl + "/" + config.MetaImg
  70. m["[blogo-link]"] = config.AbsoluteUrl
  71. r := putHTMLToTemplate(indexTemplate, m)
  72. writeFile(config.OutputDir+"/"+"index.html", r)
  73. // generate posts pages
  74. for _, post := range config.Posts {
  75. if post.OutsideArticle != "" {
  76. continue
  77. }
  78. mdcontent := readFile(directory + "/" + config.PostsDir + post.Md)
  79. mdParser := parser.NewWithExtensions(mdExtensions)
  80. htmlcontent := markdown.ToHTML([]byte(mdcontent), mdParser, nil)
  81. firstline := strings.Split(mdcontent, "\n")[0]
  82. title := strings.Replace(firstline, "#", "", -1)
  83. filename := strings.Split(post.Md, ".")[0]
  84. m := make(map[string]string)
  85. m["[blogo-title]"] = title + " - " + config.Title
  86. m["[blogo-content]"] = string(htmlcontent)
  87. m["[blogo-summary]"] = post.MetaDescr
  88. m["[blogo-link]"] = config.AbsoluteUrl + "/" + filename + ".html"
  89. m["[blogo-img]"] = config.AbsoluteUrl + "/" + post.MetaImg
  90. r := putHTMLToTemplate(indexTemplate, m)
  91. writeFile(config.OutputDir+"/"+filename+".html", r)
  92. }
  93. //copy raw
  94. fmt.Println("copying raw:")
  95. for _, dir := range config.CopyRaw {
  96. copyRaw(directory+"/"+dir, config.OutputDir+"/")
  97. }
  98. }
  99. func putHTMLToTemplate(template string, m map[string]string) string {
  100. lines := getLines(template)
  101. var resultL []string
  102. for _, line := range lines {
  103. inserted := false
  104. for k, v := range m {
  105. if strings.Contains(line, k) {
  106. //in the line, change [tag] with the content
  107. lineReplaced := strings.Replace(line, k, v, -1)
  108. resultL = append(resultL, lineReplaced)
  109. inserted = true
  110. }
  111. }
  112. if inserted == false {
  113. resultL = append(resultL, line)
  114. }
  115. }
  116. result := concatStringsWithJumps(resultL)
  117. return result
  118. }