Skip to content

Commit

Permalink
Merge pull request #642 from trheyi/main
Browse files Browse the repository at this point in the history
refactor: Update go.mod dependencies to latest versions
  • Loading branch information
trheyi authored Jul 3, 2024
2 parents 7e7896d + 671a6b4 commit f802bdb
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/dchest/captcha v1.0.0
github.com/elazarl/go-bindata-assetfs v1.0.1
github.com/evanw/esbuild v0.19.5
github.com/expr-lang/expr v1.15.8
github.com/expr-lang/expr v1.16.9
github.com/fatih/color v1.16.0
github.com/fsnotify/fsnotify v1.7.0
github.com/gin-gonic/gin v1.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/evanw/esbuild v0.19.5 h1:9ildZqajUJzDAwNf9MyQsLh2RdDRKTq3kcyyzhE39us=
github.com/evanw/esbuild v0.19.5/go.mod h1:D2vIQZqV/vIf/VRHtViaUtViZmG7o+kKmlBfVQuRi48=
github.com/expr-lang/expr v1.15.8 h1:FL8+d3rSSP4tmK9o+vKfSMqqpGL8n15pEPiHcnBpxoI=
github.com/expr-lang/expr v1.15.8/go.mod h1:uCkhfG+x7fcZ5A5sXHKuQ07jGZRl6J0FCAaf2k4PtVQ=
github.com/expr-lang/expr v1.16.9 h1:WUAzmR0JNI9JCiF0/ewwHB1gmcGw5wW7nWt8gc6PpCI=
github.com/expr-lang/expr v1.16.9/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
Expand Down
35 changes: 21 additions & 14 deletions sui/core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,18 @@ func (page *Page) BuildForImport(ctx *BuildContext, option *BuildOption, slots m

func (page *Page) parse(ctx *BuildContext, doc *goquery.Document, option *BuildOption, warnings []string) error {

sui := SUIs[page.SuiID]
if sui == nil {
return fmt.Errorf("SUI %s not found", page.SuiID)
}

tmpl, err := sui.GetTemplate(page.TemplateID)
if err != nil {
return err
}

public := sui.GetPublic()

// Find the import pages
pages := doc.Find("*").FilterFunction(func(i int, sel *goquery.Selection) bool {

Expand All @@ -158,23 +170,18 @@ func (page *Page) parse(ctx *BuildContext, doc *goquery.Document, option *BuildO
}

name, has := sel.Attr("is")
_, jit := sel.Attr("s:jit") // Just in time rendering
if has && jit {
ctx.addJitComponent(name)
if has {
// Just in time component
if ctx.isJitComponent(name) {
sel.SetAttr("s:jit", "true")
sel.SetAttr("s:root", public.Root)
ctx.addJitComponent(name)
return false
}
}
return has && !jit
return has
})

sui := SUIs[page.SuiID]
if sui == nil {
return fmt.Errorf("SUI %s not found", page.SuiID)
}

tmpl, err := sui.GetTemplate(page.TemplateID)
if err != nil {
return err
}

for _, node := range pages.Nodes {
sel := goquery.NewDocumentFromNode(node)
name, has := sel.Attr("is")
Expand Down
6 changes: 6 additions & 0 deletions sui/core/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ func (ctx *BuildContext) addJitComponent(name string) {
ctx.global.jitComponents[name] = true
}
}

func (ctx *BuildContext) isJitComponent(name string) bool {
hasStmt := stmtRe.MatchString(name)
hasProp := propRe.MatchString(name)
return hasStmt || hasProp
}
53 changes: 52 additions & 1 deletion sui/core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"fmt"
"os"
"path/filepath"
"strings"

Expand All @@ -13,6 +14,9 @@ import (
"gopkg.in/yaml.v3"
)

// Load the jit components
var components = map[string]string{}

// TemplateParser parser for the template
type TemplateParser struct {
data Data
Expand All @@ -22,6 +26,11 @@ type TemplateParser struct {
replace map[*goquery.Selection][]*html.Node // replace nodes
option *ParserOption // parser option
locale *Locale // locale
context *ParserContext // parser context
}

// ParserContext parser context for the template
type ParserContext struct {
}

// Mapping mapping for the template
Expand All @@ -33,6 +42,7 @@ type Mapping struct {

// ParserOption parser option
type ParserOption struct {
Component bool `json:"component,omitempty"`
Editor bool `json:"editor,omitempty"`
Preview bool `json:"preview,omitempty"`
Debug bool `json:"debug,omitempty"`
Expand Down Expand Up @@ -190,7 +200,7 @@ func (parser *TemplateParser) Render(html string) (string, error) {

// Append the data to the body
body := doc.Find("body")
if body.Length() > 0 {
if body.Length() > 0 && !parser.option.Component {
data, err := jsoniter.MarshalToString(parser.data)
if err != nil {
data, _ = jsoniter.MarshalToString(map[string]string{"error": err.Error()})
Expand Down Expand Up @@ -273,13 +283,54 @@ func (parser *TemplateParser) parseElementNode(sel *goquery.Selection) {
parser.setStatementNode(sel)
}

// JIT Compile the element
if _, exist := sel.Attr("s:jit"); exist {
parser.parseJitElementNode(sel)
}

// Parse the attributes
parser.parseElementAttrs(sel)

// Translations
parser.transElementNode(sel)
}

func (parser *TemplateParser) parseJitElementNode(sel *goquery.Selection) {

is := sel.AttrOr("is", "")
if is == "" {
sel.Remove()
return
}

// Render the JIT component Data
is, _ = parser.data.Replace(is)
root := sel.AttrOr("s:root", "/")
file := filepath.Join(string(os.PathSeparator), "public", root, is+".jit")

// Should be cached to reduce the file read and unnecessary parsing
content, err := application.App.Read(file)
if err != nil {
log.Error("[parser] %s JIT %s", file, err.Error())
return
}

// With Properties

// copy options
option := *parser.option
option.Component = true
p := NewTemplateParser(parser.data, &option)
html, err := p.Render(string(content))
if err != nil {
log.Error("[parser] %s JIT %s", file, err.Error())
return
}

// Replace the node
sel.ReplaceWithHtml(html)
}

func (parser *TemplateParser) transElementNode(sel *goquery.Selection) {
if parser.locale == nil {
return
Expand Down
2 changes: 1 addition & 1 deletion sui/storages/local/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (page *Page) Build(globalCtx *core.GlobalBuildContext, option *core.BuildOp

// BuildAsComponent build the page as component
func (page *Page) BuildAsComponent(globalCtx *core.GlobalBuildContext, option *core.BuildOption) error {
return page.writeJitHTML([]byte("<div>"+page.Route+"<div>"), option.Data)
return page.writeJitHTML([]byte("<div>"+page.Route+" {{ type }} {{ bind }} </div>"), option.Data)
}

func (page *Page) publicFile(data map[string]interface{}) string {
Expand Down

0 comments on commit f802bdb

Please sign in to comment.