staticmd.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package main
  2. import (
  3. "html/template"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. // "bufio"
  8. // "io/ioutil"
  9. // "path"
  10. // "github.com/russross/blackfriday"
  11. "github.com/cdelorme/go-log"
  12. )
  13. type Staticmd struct {
  14. Logger log.Logger
  15. Input string
  16. Output string
  17. Template template.Template
  18. Book bool
  19. Relative bool
  20. MaxParallelism int
  21. Version string
  22. Navigation []string
  23. Pages []string
  24. Subdirectories map[string][]string
  25. Indexes map[string][]string
  26. }
  27. // search for readme or fallback to index
  28. func (staticmd *Staticmd) index(pages []string) string {
  29. for _, page := range pages {
  30. if n := basename(page); n == "readme" {
  31. return n
  32. }
  33. }
  34. return "index"
  35. }
  36. func (staticmd *Staticmd) Walk(path string, file os.FileInfo, err error) error {
  37. if file == nil {
  38. } else if file.IsDir() {
  39. // prepare path to create for matching output
  40. mkd := filepath.Join(staticmd.Output, strings.TrimPrefix(path, staticmd.Input))
  41. // include parent output path
  42. if path == staticmd.Input {
  43. mkd = staticmd.Output
  44. } else {
  45. // note subdirectory for indexing
  46. if _, ok := staticmd.Subdirectories[filepath.Dir(path)]; !ok {
  47. staticmd.Subdirectories[filepath.Dir(path)] = make([]string, 0)
  48. }
  49. staticmd.Subdirectories[filepath.Dir(path)] = append(staticmd.Subdirectories[filepath.Dir(path)], path)
  50. }
  51. // create all matching output paths
  52. staticmd.Logger.Debug("creating matching output path: %s", mkd)
  53. if err := os.MkdirAll(mkd, 0770); err != nil {
  54. staticmd.Logger.Error("failed to create matching output path: %s, %s", path, err)
  55. }
  56. } else if file.Mode().IsRegular() && file.Size() > 0 {
  57. // append all markdown files to our pages list
  58. if strings.HasSuffix(path, ".md") || strings.HasSuffix(path, ".mkd") || strings.HasSuffix(path, ".markdown") {
  59. staticmd.Pages = append(staticmd.Pages, path)
  60. dir := filepath.Dir(path)
  61. // append to navigation, index
  62. if dir == staticmd.Input {
  63. staticmd.Navigation = append(staticmd.Navigation, path)
  64. } else {
  65. // prepare index container
  66. if _, ok := staticmd.Indexes[dir]; !ok {
  67. staticmd.Indexes[dir] = make([]string, 0)
  68. }
  69. staticmd.Indexes[dir] = append(staticmd.Indexes[dir], dir+basename(strings.TrimPrefix(path, staticmd.Input))+".html")
  70. }
  71. }
  72. }
  73. return err
  74. }
  75. func (staticmd *Staticmd) Multi() {
  76. // test filepath.Rel()
  77. // iterate all subdirectories to find index/readme and append to
  78. // for i, _ := range staticmd.Subdirectories {
  79. // for _, d := range staticmd.Subdirectories[i] {
  80. // // can only append to indexes that exist
  81. // if _, ok := staticmd.Indexes[filepath.Dir(staticmd.Subdirectories[i][d])]; !ok {
  82. // continue
  83. // }
  84. // // determine whether to append relative path or just the folder
  85. // if staticmd.Relative {
  86. // // trim input path, and append to indexes as .html
  87. // idx := staticmd.index(staticmd.Subdirectories[i][d])
  88. // staticmd.Subdirectories[i][d]+idx+".html"
  89. // // determine whether index or readme exists, assume index
  90. // } else {
  91. // // simply append `subfolder/`
  92. // }
  93. // }
  94. // }
  95. // rebuild navigation pathing
  96. // for i, page := range staticmd.Navigation {
  97. // if staticmd.Relative {
  98. // staticmd.Navigation[i] = strings.TrimPrefix(strings.TrimPrefix(page, staticmd.Input), "/")
  99. // } else if basename(page) == "index" {
  100. // staticmd.Navigation[i] = "/"
  101. // } else {
  102. // staticmd.Navigation[i] = strings.TrimPrefix(page, staticmd.Input)
  103. // }
  104. // }
  105. // debug output
  106. staticmd.Logger.Debug("Navigation: %+v", staticmd.Navigation)
  107. staticmd.Logger.Debug("Indexes: %+v", staticmd.Indexes)
  108. // concurrently build pages
  109. }
  110. func (staticmd *Staticmd) Single() {
  111. // open single index.html for building
  112. file := staticmd.Output
  113. // prepare index
  114. index := make([]string, 0)
  115. // prepare markdown thingy?
  116. // content := template.HTML(blackfriday.MarkdownCommon(markdown))
  117. // synchronously build one output file
  118. for i, _ := range staticmd.Pages {
  119. // create index record
  120. // staticmd.Pages[i]
  121. // append each file to content
  122. }
  123. // append index
  124. }