partial.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package partial
  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. partTag := dom.QuerySelector("part")
  12. if partTag.TagName() == "" {
  13. return dom.OuterHTML(), nil
  14. }
  15. attr := partTag.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. partFileByt, err := os.ReadFile(attr)
  24. if err != nil {
  25. fmt.Printf("ERROR: %s", err)
  26. return "", err
  27. }
  28. partFileHtml := string(partFileByt)
  29. partFileDom := domquery.LoadHTML(partFileHtml)
  30. slot := partFileDom.QuerySelector("slot")
  31. if slot.TagName() != "" {
  32. slot.SetOuterHTML(partTag.InnerHTML())
  33. }
  34. partTag.SetOuterHTML(partFileDom.InnerHTML())
  35. htmlStr, err = ProcessHTML(dom.InnerHTML(), fp)
  36. if err != nil {
  37. fmt.Printf("ERROR: %s", err)
  38. return "", err
  39. }
  40. return htmlStr, nil
  41. }