main.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. package gen
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "git.clearsky.net.au/cody/gex.git/gen/domquery"
  9. "git.clearsky.net.au/cody/gex.git/gen/img"
  10. "git.clearsky.net.au/cody/gex.git/gen/layout"
  11. "git.clearsky.net.au/cody/gex.git/gen/partial"
  12. )
  13. var appRuleList cssRuleList
  14. //var appJsStr string
  15. func GeneratePages(dir string) error {
  16. err := processTemplate(dir)
  17. if err != nil {
  18. fmt.Println(err)
  19. return err
  20. }
  21. // Process Partials
  22. err = processPartials(dir)
  23. if err != nil {
  24. fmt.Println(err)
  25. return err
  26. }
  27. // Build the layout & blocks
  28. err = processLayout(dir)
  29. if err != nil {
  30. fmt.Println(err)
  31. return err
  32. }
  33. // Process IMG on the generated files
  34. err = ProcessImg(dir)
  35. if err != nil {
  36. fmt.Println(err)
  37. return err
  38. }
  39. // Clean block tags
  40. err = cleanBlocks(dir)
  41. if err != nil {
  42. fmt.Println(err)
  43. return err
  44. }
  45. appRuleList = mergeMediaRules(appRuleList)
  46. appRuleList = orderRules(appRuleList)
  47. cssStr := rulesToCSSstr(appRuleList)
  48. os.WriteFile("app/pub/css/app.gen.css", []byte(cssStr), 0655)
  49. return nil
  50. }
  51. func processTemplate(dir string) error {
  52. processFile := func(fp string) error {
  53. if strings.Contains(fp, ".gen.tpl") {
  54. return nil
  55. }
  56. if !strings.Contains(fp, ".tpl") {
  57. return nil
  58. }
  59. // get the html string
  60. data, err := os.ReadFile(fp)
  61. if err != nil {
  62. return err
  63. }
  64. htmlStr := string(data)
  65. // add the scope class to attributes
  66. className := genRandName(8)
  67. htmlStr = AddClassAttr(htmlStr, className)
  68. // get & strip the css rules
  69. cssStr := ExtractCSS(&htmlStr)
  70. ruleList := toRuleList(cssStr)
  71. // scope the rules with css class
  72. ruleList = scopeSelectors(ruleList, className)
  73. appRuleList = append(appRuleList, ruleList...)
  74. // Process JS on the generated file
  75. htmlStr = ProcessJs(htmlStr, className)
  76. // write the .gen.html file as
  77. newPath := strings.Replace(fp, ".tpl", ".gen.tpl", 1)
  78. return os.WriteFile(newPath, []byte(htmlStr), 0655)
  79. }
  80. walk := func(path string, info os.FileInfo, err error) error {
  81. if err != nil {
  82. return err
  83. }
  84. if info.IsDir() {
  85. return nil
  86. }
  87. return processFile(path)
  88. }
  89. return filepath.Walk(dir, walk)
  90. }
  91. func processPartials(dir string) error {
  92. processFile := func(fp string) error {
  93. if !strings.Contains(fp, ".gen.tpl") {
  94. return nil
  95. }
  96. data, err := os.ReadFile(fp)
  97. if err != nil {
  98. return err
  99. }
  100. htmlStr := string(data)
  101. htmlStr, err = partial.ProcessHTML(htmlStr, fp)
  102. if err != nil {
  103. fmt.Println("\n" + fp)
  104. return err
  105. }
  106. return os.WriteFile(fp, []byte(htmlStr), 0655)
  107. }
  108. walk := func(path string, info os.FileInfo, err error) error {
  109. if err != nil {
  110. return err
  111. }
  112. if info.IsDir() {
  113. return nil
  114. }
  115. return processFile(path)
  116. }
  117. err := filepath.Walk(dir, walk)
  118. if err != nil {
  119. return err
  120. }
  121. return nil
  122. }
  123. func processLayout(dir string) error {
  124. processFile := func(fp string) error {
  125. if !strings.Contains(fp, ".gen.tpl") {
  126. return nil
  127. }
  128. data, err := os.ReadFile(fp)
  129. if err != nil {
  130. return err
  131. }
  132. htmlStr := string(data)
  133. htmlStr, err = layout.ProcessHTML(htmlStr, fp)
  134. if err != nil {
  135. fmt.Println("\n" + fp)
  136. return err
  137. }
  138. os.WriteFile(fp, []byte(htmlStr), 0655)
  139. return nil
  140. }
  141. walk := func(path string, info os.FileInfo, err error) error {
  142. if err != nil {
  143. return err
  144. }
  145. if info.IsDir() {
  146. return nil
  147. }
  148. return processFile(path)
  149. }
  150. filepath.Walk(dir, walk)
  151. return nil
  152. }
  153. func ProcessJs(htmlStr string, className string) string {
  154. dom := domquery.LoadHTML(htmlStr)
  155. scripts := dom.QuerySelectorAll("script")
  156. i := 0
  157. jsTag := ""
  158. for _, script := range scripts {
  159. scriptName := className + fmt.Sprintf("%d", i)
  160. if script.GetAttribute("inline") != "" {
  161. continue
  162. }
  163. if script.GetAttribute("src") != "" {
  164. continue
  165. }
  166. jsStr := script.InnerHTML()
  167. funcStr := `if (typeof ` + scriptName + ` === 'undefined') {`
  168. funcStr += `window.` + scriptName + ` = () => {`
  169. funcStr += jsStr
  170. funcStr += `
  171. if(typeof unload == "undefined")
  172. return
  173. const watchInt = setInterval(() => {
  174. let watch = document.querySelector(".` + className + `");
  175. if (!watch) {
  176. unload();
  177. clearInterval(watchInt);
  178. }
  179. },100);
  180. }
  181. }
  182. ` + scriptName + `()
  183. `
  184. srcPath := "app/pub/gen/" + scriptName + ".gen.js"
  185. os.WriteFile(srcPath, []byte(funcStr), 0655)
  186. jsTag += `<script defer src="/` + srcPath + `"></script>`
  187. if script.GetAttribute("type") == "module" {
  188. jsTag = `<script type="module" defer src="/` + srcPath + `"></script>`
  189. }
  190. script.Remove()
  191. i++
  192. }
  193. insDiv := dom.QuerySelector("." + className)
  194. insDiv.SetInnerHTML(jsTag + insDiv.InnerHTML())
  195. htmlStr = dom.InnerHTML()
  196. return htmlStr
  197. }
  198. func ProcessImg(dir string) error {
  199. processFile := func(fp string) error {
  200. htmlDir := filepath.Dir(fp)
  201. if !strings.Contains(fp, ".gen.tpl") {
  202. return nil
  203. }
  204. // get the html string
  205. data, err := os.ReadFile(fp)
  206. if err != nil {
  207. return err
  208. }
  209. htmlStr := string(data)
  210. document := domquery.LoadHTML(htmlStr)
  211. imgTags := document.QuerySelectorAll("img")
  212. for _, tag := range imgTags {
  213. if !(tag.HasAttribute("webp") && tag.HasAttribute("src")) {
  214. continue
  215. }
  216. img.Process(htmlDir, tag)
  217. }
  218. htmlStr = document.InnerHTML()
  219. os.WriteFile(fp, []byte(htmlStr), 0655)
  220. return nil
  221. }
  222. walk := func(path string, info os.FileInfo, err error) error {
  223. if err != nil {
  224. return err
  225. }
  226. if info.IsDir() {
  227. return nil
  228. }
  229. return processFile(path)
  230. }
  231. return filepath.Walk(dir, walk)
  232. }
  233. func cleanBlocks(dir string) error {
  234. processFile := func(fp string) error {
  235. if !strings.Contains(fp, ".gen.tpl") {
  236. return nil
  237. }
  238. // get the html string
  239. data, err := os.ReadFile(fp)
  240. if err != nil {
  241. return err
  242. }
  243. htmlStr := string(data)
  244. dom := domquery.LoadHTML(htmlStr)
  245. var clean func()
  246. clean = func() {
  247. blocks := dom.QuerySelectorAll("block")
  248. // grab the top block only as blocks is invalid after a SetOuterHTML event
  249. for _, block := range blocks {
  250. block.SetOuterHTML(block.InnerHTML())
  251. clean()
  252. return
  253. }
  254. }
  255. clean()
  256. htmlStr = dom.InnerHTML()
  257. os.WriteFile(fp, []byte(htmlStr), 0655)
  258. return nil
  259. }
  260. walk := func(dir string, info os.FileInfo, err error) error {
  261. if err != nil {
  262. return err
  263. }
  264. if info.IsDir() {
  265. return nil
  266. }
  267. return processFile(dir)
  268. }
  269. return filepath.Walk(dir, walk)
  270. }
  271. // generate a random string to use as a class name
  272. func genRandName(length int) string {
  273. const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  274. b := make([]byte, length)
  275. for i := range b {
  276. b[i] = charset[rand.Intn(len(charset))]
  277. }
  278. return string(b)
  279. }