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.

93 lines
2.6 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. r := putHTMLToTemplate(indexTemplate, m)
  36. writeFile(outputDir+"/"+"index.html", r)
  37. // generate posts pages
  38. for _, post := range config.Posts {
  39. mdcontent := readFile(directory + "/" + post.Md)
  40. htmlcontent := string(blackfriday.Run([]byte(mdcontent)))
  41. firstline := strings.Split(mdcontent, "\n")[0]
  42. title := strings.Replace(firstline, "# ", "", -1)
  43. filename := strings.Split(post.Md, ".")[0]
  44. m := make(map[string]string)
  45. m["[blogo-title]"] = title + " - " + config.Title
  46. m["[blogo-content]"] = htmlcontent
  47. m["[blogo-summary]"] = post.MetaDescr
  48. m["[blogo-link]"] = config.AbsoluteUrl + "/" + filename + ".html"
  49. m["[blogo-img]"] = config.AbsoluteUrl + "/" + post.MetaImg
  50. r := putHTMLToTemplate(indexTemplate, m)
  51. writeFile(outputDir+"/"+filename+".html", r)
  52. }
  53. //copy raw
  54. fmt.Println("copying raw:")
  55. for _, dir := range config.CopyRaw {
  56. copyRaw(directory+"/"+dir, outputDir+"/")
  57. }
  58. }
  59. func putHTMLToTemplate(template string, m map[string]string) string {
  60. lines := getLines(template)
  61. var resultL []string
  62. for _, line := range lines {
  63. inserted := false
  64. for k, v := range m {
  65. if strings.Contains(line, k) {
  66. //in the line, change [tag] with the content
  67. lineReplaced := strings.Replace(line, k, v, -1)
  68. resultL = append(resultL, lineReplaced)
  69. inserted = true
  70. }
  71. }
  72. if inserted == false {
  73. resultL = append(resultL, line)
  74. }
  75. }
  76. result := concatStringsWithJumps(resultL)
  77. return result
  78. }