main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package main
  2. import (
  3. "html/template"
  4. "os"
  5. "path/filepath"
  6. "runtime/pprof"
  7. "os/exec"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/cdelorme/go-log"
  12. "github.com/cdelorme/go-maps"
  13. "github.com/cdelorme/go-option"
  14. "github.com/cdelorme/staticmd"
  15. )
  16. // if within a git repo, gets git version as a short-hash
  17. // otherwise falls back to a unix timestamp
  18. func version() string {
  19. version := strconv.FormatInt(time.Now().Unix(), 10)
  20. out, err := exec.Command("sh", "-c", "git rev-parse --short HEAD").Output()
  21. if err == nil {
  22. version = strings.Trim(string(out), "\n")
  23. }
  24. return version
  25. }
  26. func main() {
  27. // get current directory
  28. cwd, _ := os.Getwd()
  29. // prepare staticmd with dependencies & defaults
  30. gen := staticmd.Staticmd{
  31. Logger: log.Logger{},
  32. Version: version(),
  33. Input: cwd,
  34. Output: filepath.Join(cwd, "public/"),
  35. }
  36. // prepare cli options
  37. appOptions := option.App{Description: "command line tool for generating deliverable static content"}
  38. appOptions.Flag("template", "path to the template file", "--template", "-t")
  39. appOptions.Flag("input", "path to the markdown files", "--input", "-i")
  40. appOptions.Flag("output", "path to place generated content", "--output", "-o")
  41. appOptions.Flag("book", "combine all content into a single file", "--book", "-b")
  42. appOptions.Flag("relative", "use relative paths instead of absolute paths", "--relative", "-r")
  43. appOptions.Flag("profile", "produce profile output to supplied path", "--profile", "-p")
  44. appOptions.Example("-t template.tmpl -i . -b")
  45. appOptions.Example("-t template.tmpl -i src/ -o out/ -r")
  46. flags := appOptions.Parse()
  47. // apply flags
  48. t, _ := maps.String(flags, "", "template")
  49. if tmpl, err := template.ParseFiles(t); err != nil {
  50. gen.Logger.Error("Failed to open template: %s", err)
  51. os.Exit(1)
  52. } else {
  53. gen.Template = *tmpl
  54. }
  55. gen.Input, _ = maps.String(flags, gen.Input, "input")
  56. gen.Output, _ = maps.String(flags, gen.Output, "output")
  57. gen.Book, _ = maps.Bool(flags, gen.Book, "book")
  58. gen.Relative, _ = maps.Bool(flags, gen.Relative, "relative")
  59. // sanitize input & output
  60. gen.Input, _ = filepath.Abs(gen.Input)
  61. gen.Output, _ = filepath.Abs(gen.Output)
  62. // optionally enable profiling
  63. if profile, _ := maps.String(flags, "", "profile"); profile != "" {
  64. f, _ := os.Create(profile)
  65. pprof.StartCPUProfile(f)
  66. defer pprof.StopCPUProfile()
  67. }
  68. // sanitize & validate properties
  69. gen.Input = filepath.Clean(gen.Input)
  70. gen.Output = filepath.Clean(gen.Output)
  71. // print debug status
  72. gen.Logger.Debug("Staticmd State: %+v", gen)
  73. // walk the file system
  74. if err := filepath.Walk(gen.Input, gen.Walk); err != nil {
  75. gen.Logger.Error("failed to walk directory: %s", err)
  76. }
  77. gen.Logger.Debug("Pages: %+v", gen.Pages)
  78. // build
  79. if gen.Book {
  80. gen.Single()
  81. } else {
  82. gen.Multi()
  83. }
  84. }