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.

96 lines
2.7 KiB

6 years ago
4 years ago
6 years ago
4 years ago
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
4 years ago
6 years ago
4 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
6 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. // blackfriday "gopkg.in/russross/blackfriday.v2"
  7. "github.com/russross/blackfriday"
  8. )
  9. const directory = "blogo-input"
  10. const outputDir = "public"
  11. func main() {
  12. readConfig(directory + "/blogo.json")
  13. fmt.Println(config)
  14. _ = os.Mkdir(outputDir, os.ModePerm)
  15. // generate index page
  16. indexTemplate := readFile(directory + "/" + config.IndexTemplate)
  17. indexPostTemplate := readFile(directory + "/" + config.PostThumbTemplate)
  18. var blogoIndex string
  19. blogoIndex = ""
  20. for _, post := range config.Posts {
  21. mdpostthumb := readFile(directory + "/" + post.Thumb)
  22. htmlpostthumb := string(blackfriday.Run([]byte(mdpostthumb)))
  23. //put the htmlpostthumb in the blogo-index-post-template
  24. m := make(map[string]string)
  25. m["[blogo-index-post-template]"] = htmlpostthumb
  26. r := putHTMLToTemplate(indexPostTemplate, m)
  27. filename := strings.Split(post.Md, ".")[0]
  28. r = "<a href='" + config.RelativePath + "/" + filename + ".html'>" + r + "</a>"
  29. blogoIndex = blogoIndex + r
  30. }
  31. //put the blogoIndex in the index.html
  32. m := make(map[string]string)
  33. m["[blogo-title]"] = config.Title
  34. m["[blogo-content]"] = blogoIndex
  35. m["[blogo-summary]"] = config.MetaDescr
  36. m["[blogo-img]"] = config.AbsoluteUrl + "/" + config.MetaImg
  37. m["[blogo-link]"] = config.AbsoluteUrl
  38. r := putHTMLToTemplate(indexTemplate, m)
  39. writeFile(outputDir+"/"+"index.html", r)
  40. // generate posts pages
  41. for _, post := range config.Posts {
  42. mdcontent := readFile(directory + "/" + post.Md)
  43. htmlcontent := string(blackfriday.Run([]byte(mdcontent)))
  44. firstline := strings.Split(mdcontent, "\n")[0]
  45. title := strings.Replace(firstline, "# ", "", -1)
  46. filename := strings.Split(post.Md, ".")[0]
  47. m := make(map[string]string)
  48. m["[blogo-title]"] = title + " - " + config.Title
  49. m["[blogo-content]"] = htmlcontent
  50. m["[blogo-summary]"] = post.MetaDescr
  51. m["[blogo-link]"] = config.AbsoluteUrl + "/" + filename + ".html"
  52. m["[blogo-img]"] = config.AbsoluteUrl + "/" + post.MetaImg
  53. r := putHTMLToTemplate(indexTemplate, m)
  54. writeFile(outputDir+"/"+filename+".html", r)
  55. }
  56. //copy raw
  57. fmt.Println("copying raw:")
  58. for _, dir := range config.CopyRaw {
  59. copyRaw(directory+"/"+dir, outputDir+"/")
  60. }
  61. }
  62. func putHTMLToTemplate(template string, m map[string]string) string {
  63. lines := getLines(template)
  64. var resultL []string
  65. for _, line := range lines {
  66. inserted := false
  67. for k, v := range m {
  68. if strings.Contains(line, k) {
  69. //in the line, change [tag] with the content
  70. lineReplaced := strings.Replace(line, k, v, -1)
  71. resultL = append(resultL, lineReplaced)
  72. inserted = true
  73. }
  74. }
  75. if inserted == false {
  76. resultL = append(resultL, line)
  77. }
  78. }
  79. result := concatStringsWithJumps(resultL)
  80. return result
  81. }