staticmd.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package main
  2. import (
  3. "bufio"
  4. "html/template"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "sync"
  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. Pages []string
  23. }
  24. // convert markdown input path to html output path
  25. func (staticmd *Staticmd) ior(path string) string {
  26. return strings.TrimSuffix(strings.Replace(path, staticmd.Input, staticmd.Output, 1), filepath.Ext(path)) + ".html"
  27. }
  28. // for relative rendering generate relative depth string, or fallback to empty
  29. func (staticmd *Staticmd) depth(path string) string {
  30. if staticmd.Relative {
  31. if rel, err := filepath.Rel(filepath.Dir(path), staticmd.Output); err == nil {
  32. return rel
  33. }
  34. }
  35. return ""
  36. }
  37. // get link to file, with support for relative path linking
  38. func (staticmd *Staticmd) link(path string) string {
  39. if staticmd.Relative {
  40. return strings.TrimPrefix(path, filepath.Dir(path))
  41. }
  42. return strings.TrimPrefix(path, staticmd.Output)
  43. }
  44. // walk the directories and build a list of pages
  45. func (staticmd *Staticmd) Walk(path string, file os.FileInfo, err error) error {
  46. // only pay attention to files with a size greater than 0
  47. if file == nil {
  48. } else if file.Mode().IsRegular() && file.Size() > 0 {
  49. // only add markdown files to our pages array (.md, .mkd, .markdown)
  50. if strings.HasSuffix(path, ".md") || strings.HasSuffix(path, ".mkd") || strings.HasSuffix(path, ".markdown") {
  51. staticmd.Pages = append(staticmd.Pages, path)
  52. }
  53. }
  54. return err
  55. }
  56. // build output concurrently to many pages
  57. func (staticmd *Staticmd) Multi() {
  58. // prepare navigation storage
  59. navigation := make(map[string][]Navigation)
  60. // loop pages to build table of contents
  61. for i, _ := range staticmd.Pages {
  62. // build output directory
  63. out := staticmd.ior(staticmd.Pages[i])
  64. dir := filepath.Dir(staticmd.ior(out))
  65. // create a new navigation object
  66. nav := Navigation{
  67. Name: basename(out),
  68. Link: staticmd.link(out),
  69. }
  70. // handle special table-of-contents cases
  71. if filepath.Dir(out) != staticmd.Output && strings.ToLower(nav.Name) == "index" {
  72. nav.Name = basename(dir)
  73. dir = filepath.Dir(dir)
  74. }
  75. // build indexes first-match
  76. if _, ok := navigation[dir]; !ok {
  77. navigation[dir] = make([]Navigation, 0)
  78. // create output directory for when we create files
  79. if ok, _ := exists(dir); !ok {
  80. if err := os.MkdirAll(dir, 0770); err != nil {
  81. staticmd.Logger.Error("Failed to create path: %s, %s", dir, err)
  82. }
  83. }
  84. }
  85. // append to navigational list
  86. navigation[dir] = append(navigation[dir], nav)
  87. }
  88. // debug output
  89. staticmd.Logger.Debug("Navigation: %+v", navigation)
  90. // prepare waitgroup, bufferer channel, and add number of async handlers to wg
  91. var wg sync.WaitGroup
  92. pages := make(chan string, staticmd.MaxParallelism)
  93. wg.Add(staticmd.MaxParallelism)
  94. // prepare workers
  95. for i := 0; i < staticmd.MaxParallelism; i++ {
  96. go func() {
  97. defer wg.Done()
  98. // iterate supplied pages
  99. for p := range pages {
  100. // acquire output filepath
  101. out := staticmd.ior(p)
  102. dir := filepath.Dir(out)
  103. // prepare a new page object for our template to render
  104. page := Page{
  105. Name: basename(p),
  106. Version: staticmd.Version,
  107. Nav: navigation[staticmd.Output],
  108. Depth: staticmd.depth(p),
  109. }
  110. // read in page text
  111. if markdown, err := ioutil.ReadFile(p); err == nil {
  112. // if this page happens to be a sub-index we can generate the table of contents
  113. if dir != staticmd.Output && strings.ToLower(basename(p)) == "index" {
  114. // iterate and build table of contents as basic markdown
  115. toc := "\n## Table of Contents:\n\n"
  116. for i, _ := range navigation[dir] {
  117. toc = toc + "- [" + navigation[dir][i].Name + "](" + navigation[dir][i].Link + ")\n"
  118. }
  119. // debug table of contents output
  120. staticmd.Logger.Debug("table of contents for %s, %s", out, toc)
  121. // prepend table of contents
  122. markdown = append([]byte(toc), markdown...)
  123. }
  124. page.Content = template.HTML(blackfriday.MarkdownCommon(markdown))
  125. } else {
  126. staticmd.Logger.Error("failed to read file: %s, %s", p, err)
  127. }
  128. // debug page output
  129. staticmd.Logger.Debug("page: %+v", page)
  130. // translate input path to output path & create a write context
  131. if f, err := os.Create(out); err == nil {
  132. defer f.Close()
  133. // prepare a writer /w buffer
  134. fb := bufio.NewWriter(f)
  135. defer fb.Flush()
  136. // attempt to use template to write output with page context
  137. if e := staticmd.Template.Execute(fb, page); e != nil {
  138. staticmd.Logger.Error("Failed to write template: %s, %s", out, e)
  139. }
  140. } else {
  141. staticmd.Logger.Error("failed to create new file: %s, %s", out, err)
  142. }
  143. }
  144. }()
  145. }
  146. // send pages to workers for async rendering
  147. for _, page := range staticmd.Pages {
  148. pages <- page
  149. }
  150. // close channel and wait for async to finish before continuing
  151. close(pages)
  152. wg.Wait()
  153. }
  154. // build output synchronously to a single page
  155. func (staticmd *Staticmd) Single() {
  156. // still determining strategy
  157. }