Skip to content

Commit

Permalink
Merge pull request #550 from trheyi/main
Browse files Browse the repository at this point in the history
Refactor attribute handling in build and parser functions
  • Loading branch information
trheyi authored Jan 10, 2024
2 parents f5fccec + cb2f6e8 commit 887de05
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
41 changes: 25 additions & 16 deletions sui/core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (page *Page) Build(option *BuildOption) (*goquery.Document, []string, error
}

// Add Style & Script & Warning
doc, err := NewDocument([]byte(html))
doc, err := NewDocumentString(html)
if err != nil {
warnings = append(warnings, err.Error())
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func (page *Page) BuildForImport(option *BuildOption, slots map[string]interface
}

// Add Style & Script & Warning
doc, err := NewDocument([]byte(html))
doc, err := NewDocumentString(html)
if err != nil {
warnings = append(warnings, err.Error())
}
Expand Down Expand Up @@ -121,7 +121,21 @@ func (page *Page) BuildForImport(option *BuildOption, slots map[string]interface
}

func (page *Page) parse(doc *goquery.Document, option *BuildOption, warnings []string) error {
pages := doc.Find("page")

pages := doc.Find("*").FilterFunction(func(i int, sel *goquery.Selection) bool {
tagName := sel.Get(0).Data
if tagName == "page" {
return true
}

if tagName == "slot" {
return false
}

_, has := sel.Attr("is")
return has
})

sui := SUIs[page.SuiID]
if sui == nil {
return fmt.Errorf("SUI %s not found", page.SuiID)
Expand Down Expand Up @@ -175,21 +189,16 @@ func (page *Page) parse(doc *goquery.Document, option *BuildOption, warnings []s
// Set Attrs
attrs := map[string]string{}
if sel.Length() > 0 {
if page.Attrs != nil {
parentProps := Data{"$prop": page.Attrs}
for k, v := range page.Attrs {
if k == "is" {
continue
}
attrs[k], _ = parentProps.ReplaceUse(slotRe, v)
for _, attr := range sel.Nodes[0].Attr {
if attr.Key == "is" || attr.Key == "parsed" {
continue
}
} else {
for _, attr := range sel.Nodes[0].Attr {
if attr.Key == "is" {
continue
}
attrs[attr.Key] = attr.Val
val := attr.Val
if page.Attrs != nil {
parentProps := Data{"$prop": page.Attrs}
val, _ = parentProps.ReplaceUse(slotRe, val)
}
attrs[attr.Key] = val
}
}

Expand Down
20 changes: 11 additions & 9 deletions sui/core/parser.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core

import (
"bytes"
"fmt"
"strings"

Expand Down Expand Up @@ -58,8 +57,7 @@ func (parser *TemplateParser) Render(html string) (string, error) {
html = fmt.Sprintf(`<!DOCTYPE html><html lang="en">%s</html>`, html)
}

reader := bytes.NewReader([]byte(html))
doc, err := goquery.NewDocumentFromReader(reader)
doc, err := NewDocumentString(html)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -145,7 +143,7 @@ func (parser *TemplateParser) parseElementNode(sel *goquery.Selection) {
parser.forStatementNode(sel)
}

if sel.Get(0).Data == "s:set" {
if _, exist := sel.Attr("s:set"); exist || sel.Get(0).Data == "s:set" {
parser.setStatementNode(sel)
}

Expand All @@ -161,14 +159,18 @@ func (parser *TemplateParser) setStatementNode(sel *goquery.Selection) {
}

valueExp := sel.AttrOr("value", "")
val, err := parser.data.Exec(valueExp)
if err != nil {
log.Warn("Set %s: %s", valueExp, err)
parser.data[name] = nil
if stmtRe.MatchString(valueExp) {
val, err := parser.data.Exec(valueExp)
if err != nil {
log.Warn("Set %s: %s", valueExp, err)
parser.data[name] = valueExp
return
}
parser.data[name] = val
return
}

parser.data[name] = val
parser.data[name] = valueExp
}

func (parser *TemplateParser) parseElementAttrs(sel *goquery.Selection) {
Expand Down
17 changes: 13 additions & 4 deletions sui/core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import (
"strings"

"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)

// NewDocument create a new document
func NewDocument(html []byte) (*goquery.Document, error) {
return goquery.NewDocumentFromReader(bytes.NewReader(html))
func NewDocument(htmlContent []byte) (*goquery.Document, error) {
docNode, err := html.Parse(bytes.NewReader(htmlContent))
if err != nil {
return nil, err
}
return goquery.NewDocumentFromNode(docNode), nil
}

// NewDocumentString create a new document
func NewDocumentString(html string) (*goquery.Document, error) {
return goquery.NewDocumentFromReader(strings.NewReader(html))
func NewDocumentString(htmlContent string) (*goquery.Document, error) {
docNode, err := html.Parse(strings.NewReader(htmlContent))
if err != nil {
return nil, err
}
return goquery.NewDocumentFromNode(docNode), nil
}

0 comments on commit 887de05

Please sign in to comment.