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.

83 lines
2.1 KiB

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