gen.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "net/http"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "time"
  10. )
  11. const GeneratedFolderName = "generated"
  12. func genStaticFiles(rootURL string) {
  13. log.SetFlags(log.Lshortfile)
  14. wd, err := os.Getwd()
  15. if err != nil {
  16. log.Fatalln("Can't get current path:", err)
  17. }
  18. fullPath := func(relPath ...string) string {
  19. return filepath.Join(append([]string{wd}, relPath...)...)
  20. }
  21. _, err = os.Stat(fullPath("web", "static", "go101"))
  22. if err != nil {
  23. if os.IsNotExist(err) { //errors.Is(err, os.ErrNotExist) {
  24. log.Fatal("File web/static/go101 not found. Not run in go101 folder?")
  25. }
  26. log.Fatal(err)
  27. }
  28. // load from http server
  29. loadFile := func(uri string) []byte {
  30. fullURL := rootURL + uri
  31. res, err := http.Get(fullURL)
  32. if err != nil {
  33. log.Fatalf("Load file %s error: %s", uri, err)
  34. }
  35. content, err := ioutil.ReadAll(res.Body)
  36. log.Println(len(content), fullURL)
  37. res.Body.Close()
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. return content
  42. }
  43. // read from OS file system
  44. readFile := func(path string) []byte {
  45. data, err := ioutil.ReadFile(path)
  46. if err != nil {
  47. log.Fatalf("Read file %s error: %s", path, err)
  48. }
  49. return data
  50. }
  51. readFolder := func(path string) (filenames, subfolders []string) {
  52. fds, err := ioutil.ReadDir(path)
  53. if err != nil {
  54. log.Fatalf("Read folder %s error: %s", path, err)
  55. }
  56. subfolders, filenames = make([]string, 0, len(fds)), make([]string, 0, len(fds))
  57. for _, fd := range fds {
  58. // ignore links, ...
  59. if fd.IsDir() {
  60. subfolders = append(subfolders, filepath.Join(path, fd.Name()))
  61. continue
  62. }
  63. filenames = append(filenames, filepath.Join(path, fd.Name()))
  64. }
  65. return
  66. }
  67. var readFolderRecursively func(path string) (filenames []string)
  68. readFolderRecursively = func(path string) (filenames []string) {
  69. type Folder struct {
  70. Path string
  71. Next *Folder
  72. }
  73. var head, tail *Folder
  74. regSubfolders := func(folders []string) {
  75. if len(folders) == 0 {
  76. return
  77. }
  78. start := 0
  79. if head == nil {
  80. head = &Folder{
  81. Path: folders[0],
  82. }
  83. tail = head
  84. start = 1
  85. }
  86. for i := start; i < len(folders); i++ {
  87. fldr := &Folder{
  88. Path: folders[i],
  89. }
  90. tail.Next = fldr
  91. tail = fldr
  92. }
  93. }
  94. var subfolders []string
  95. filenames, subfolders = readFolder(path)
  96. regSubfolders(subfolders)
  97. for head != nil {
  98. files, subfolders := readFolder(head.Path)
  99. head = head.Next
  100. regSubfolders(subfolders)
  101. filenames = append(filenames, files...)
  102. }
  103. return
  104. }
  105. // md -> html
  106. md2htmls := func(group string) {
  107. dir := fullPath("pages", group)
  108. outputs, err := runShellCommand(time.Minute/2, dir, "ebooktool", "-md2htmls")
  109. if err != nil {
  110. log.Fatalf("ebooktool failed to execute in directory: %s.\n%s", dir, outputs)
  111. }
  112. }
  113. // tmd -> html
  114. tmd2htmls := func(group string) {
  115. dir := fullPath("pages", group)
  116. filenames, _ := readFolder(dir)
  117. for _, filename := range filenames {
  118. if strings.HasSuffix(filename, ".tmd") {
  119. outputs, err := runShellCommand(time.Minute/2, dir, "tmd", "render", filename)
  120. if err != nil {
  121. log.Fatalf("tmd failed to execute in directory: %s.\n%s", dir, outputs)
  122. }
  123. }
  124. }
  125. }
  126. // collect ...
  127. files := make(map[string][]byte, 128)
  128. files["index.html"] = loadFile("")
  129. {
  130. dir := fullPath("web", "static")
  131. filenames := readFolderRecursively(dir)
  132. for _, f := range filenames {
  133. name, err := filepath.Rel(dir, f)
  134. if err != nil {
  135. log.Fatalf("filepath.Rel(%s, %s) error: %s", dir, f, err)
  136. }
  137. files["static/"+name] = readFile(f)
  138. }
  139. }
  140. collectPageGroupFiles := func(group, urlPrefix string, collectRes bool) {
  141. if collectRes {
  142. dir := fullPath("pages", group, "res")
  143. filenames, _ := readFolder(dir)
  144. for _, f := range filenames {
  145. if strings.HasSuffix(f, ".png") || strings.HasSuffix(f, ".jpg") {
  146. name, err := filepath.Rel(dir, f)
  147. if err != nil {
  148. log.Fatalf("filepath.Rel(%s, %s) error: %s", dir, f, err)
  149. }
  150. files[urlPrefix+"res/"+name] = readFile(f)
  151. }
  152. }
  153. }
  154. md2htmls(group)
  155. tmd2htmls(group)
  156. {
  157. dir := fullPath("pages", group)
  158. filenames, _ := readFolder(dir)
  159. for _, f := range filenames {
  160. if strings.HasSuffix(f, ".html") {
  161. name, err := filepath.Rel(dir, f)
  162. if err != nil {
  163. log.Fatalf("filepath.Rel(%s, %s) error: %s", dir, f, err)
  164. }
  165. files[urlPrefix+name] = loadFile(urlPrefix + name)
  166. }
  167. }
  168. }
  169. }
  170. {
  171. infos, err := ioutil.ReadDir(fullPath("pages"))
  172. if err != nil {
  173. panic("collect page groups error: " + err.Error())
  174. }
  175. for _, e := range infos {
  176. if e.IsDir() {
  177. group := e.Name()
  178. var urlPrefix string
  179. if group == "fundamentals" {
  180. // For history reason, fundamentals pages use "/article/xxx" URLs.
  181. urlPrefix = "article/"
  182. } else if group != "website" {
  183. urlPrefix = group + "/"
  184. }
  185. var collectRes bool
  186. if _, err := os.Stat(fullPath("pages", group, "res")); err == nil {
  187. collectRes = true
  188. }
  189. collectPageGroupFiles(group, urlPrefix, collectRes)
  190. }
  191. }
  192. }
  193. // write ...
  194. err = os.RemoveAll(fullPath(GeneratedFolderName))
  195. if err != nil {
  196. log.Fatalf("Remove folder %s error: %s", GeneratedFolderName, err)
  197. }
  198. for name, data := range files {
  199. fullFilename := fullPath(GeneratedFolderName, name)
  200. fullFilename = strings.Replace(fullFilename, "/", string(filepath.Separator), -1)
  201. fullFilename = strings.Replace(fullFilename, "\\", string(filepath.Separator), -1)
  202. if err := os.MkdirAll(filepath.Dir(fullFilename), 0700); err != nil {
  203. log.Fatalln("Mkdir error:", err)
  204. }
  205. if err := ioutil.WriteFile(fullFilename, data, 0644); err != nil {
  206. log.Fatalln("Write file error:", err)
  207. }
  208. log.Printf("Generated %s (size: %d).", name, len(data))
  209. }
  210. }