123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package partial
- import (
- "fmt"
- "os"
- "path"
- "strings"
- "git.clearsky.net.au/cody/gex.git/gen/domquery"
- )
- func ProcessHTML(htmlStr string, fp string) (string, error) {
- dom := domquery.LoadHTML(htmlStr)
- partTag := dom.QuerySelector("part")
- if partTag.TagName() == "" {
- return dom.OuterHTML(), nil
- }
- attr := partTag.GetAttribute("src")
- // Set path
- attr = strings.TrimPrefix(attr, "/")
- if !strings.Contains(attr, "app/") {
- dirPath := path.Dir(fp)
- attr = dirPath + "/" + attr
- }
- attr = strings.Replace(attr, ".tpl", ".gen.tpl", 1)
- partFileByt, err := os.ReadFile(attr)
- if err != nil {
- fmt.Printf("ERROR: %s", err)
- return "", err
- }
- partFileHtml := string(partFileByt)
- partFileDom := domquery.LoadHTML(partFileHtml)
- slot := partFileDom.QuerySelector("slot")
- if slot.TagName() != "" {
- slot.SetOuterHTML(partTag.InnerHTML())
- }
- partTag.SetOuterHTML(partFileDom.InnerHTML())
- htmlStr, err = ProcessHTML(dom.InnerHTML(), fp)
- if err != nil {
- fmt.Printf("ERROR: %s", err)
- return "", err
- }
- return htmlStr, nil
- }
|