main.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package layout
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. "strings"
  7. "git.clearsky.net.au/cody/gex.git/gen/domquery"
  8. )
  9. func ProcessHTML(htmlStr string, fp string) (string, error) {
  10. dom := domquery.LoadHTML(htmlStr)
  11. layoutTag := dom.QuerySelector("layout")
  12. if layoutTag.TagName() == "" {
  13. return dom.OuterHTML(), nil
  14. }
  15. attr := layoutTag.GetAttribute("src")
  16. // Set path
  17. attr = strings.TrimPrefix(attr, "/")
  18. if !strings.Contains(attr, "app/") {
  19. dirPath := path.Dir(fp)
  20. attr = dirPath + "/" + attr
  21. }
  22. attr = strings.Replace(attr, ".tpl", ".gen.tpl", 1)
  23. layoutFileByt, err := os.ReadFile(attr)
  24. if err != nil {
  25. fmt.Printf("ERROR: %s", err)
  26. return "", err
  27. }
  28. // get the block values
  29. blocks := layoutTag.QuerySelectorAll("block")
  30. layoutFileHtml := string(layoutFileByt)
  31. layoutFileHtml, err = ProcessHTML(layoutFileHtml, fp)
  32. if err != nil {
  33. fmt.Printf("ERROR: %s\n", err)
  34. }
  35. layoutFileDom := domquery.LoadHTML(layoutFileHtml)
  36. layoutFileBlocks := layoutFileDom.QuerySelectorAll("block")
  37. for _, block := range blocks {
  38. for _, layoutFileBlock := range layoutFileBlocks {
  39. if block.GetAttribute("id") == layoutFileBlock.GetAttribute("id") {
  40. layoutFileBlock.SetInnerHTML(block.InnerHTML())
  41. }
  42. }
  43. }
  44. layoutTag.SetOuterHTML(layoutFileDom.InnerHTML())
  45. htmlStr = dom.InnerHTML()
  46. //fmt.Println(htmlStr + "\n---------------------------")
  47. return htmlStr, nil
  48. }