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.

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