Merge pull request #691 from trheyi/main
refactor: Improve SUI page parser logic for rendering i18n and escapi…
This commit is contained in:
commit
3c662332cb
2 changed files with 31 additions and 8 deletions
|
|
@ -69,13 +69,7 @@ func (parser *TemplateParser) parseComponent(sel *goquery.Selection) {
|
||||||
func (parser *TemplateParser) RenderComponent(comp *JitComponent, props map[string]interface{}, slots *goquery.Selection) (string, string, error) {
|
func (parser *TemplateParser) RenderComponent(comp *JitComponent, props map[string]interface{}, slots *goquery.Selection) (string, string, error) {
|
||||||
html := comp.html
|
html := comp.html
|
||||||
randvar := fmt.Sprintf("__%s_$props", time.Now().Format("20060102150405"))
|
randvar := fmt.Sprintf("__%s_$props", time.Now().Format("20060102150405"))
|
||||||
html = slotRe.ReplaceAllStringFunc(html, func(exp string) string {
|
html = replaceRandVar(html, randvar)
|
||||||
exp = strings.ReplaceAll(exp, "[{", "{{")
|
|
||||||
exp = strings.ReplaceAll(exp, "}]", "}}")
|
|
||||||
exp = strings.ReplaceAll(exp, "$props", randvar)
|
|
||||||
return exp
|
|
||||||
})
|
|
||||||
|
|
||||||
data := Data{}
|
data := Data{}
|
||||||
data[randvar] = props
|
data[randvar] = props
|
||||||
if parser.data != nil {
|
if parser.data != nil {
|
||||||
|
|
@ -109,7 +103,13 @@ func (parser *TemplateParser) RenderComponent(comp *JitComponent, props map[stri
|
||||||
})
|
})
|
||||||
|
|
||||||
option := *parser.option
|
option := *parser.option
|
||||||
|
option.Route = comp.route
|
||||||
compParser := NewTemplateParser(data, &option)
|
compParser := NewTemplateParser(data, &option)
|
||||||
|
locale := compParser.Locale()
|
||||||
|
if locale != nil {
|
||||||
|
locale.replaceVars(randvar)
|
||||||
|
}
|
||||||
|
compParser.locale = locale
|
||||||
|
|
||||||
// Parse the node
|
// Parse the node
|
||||||
ns := Namespace(comp.route, compParser.sequence, comp.buildOption.ScriptMinify)
|
ns := Namespace(comp.route, compParser.sequence, comp.buildOption.ScriptMinify)
|
||||||
|
|
@ -298,6 +298,20 @@ func (parser *TemplateParser) componentFile(sel *goquery.Selection, props map[st
|
||||||
return file, route, nil
|
return file, route, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (locale *Locale) replaceVars(randvar string) {
|
||||||
|
if locale.Keys != nil {
|
||||||
|
for key, val := range locale.Keys {
|
||||||
|
locale.Keys[key] = replaceRandVar(val, randvar)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if locale.Messages != nil {
|
||||||
|
for key, val := range locale.Messages {
|
||||||
|
locale.Messages[key] = replaceRandVar(val, randvar)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func componentWriter() {
|
func componentWriter() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
|
@ -394,3 +408,12 @@ func readComponent(route string, file string) (*JitComponent, error) {
|
||||||
buildOption: &buildOption,
|
buildOption: &buildOption,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func replaceRandVar(html string, randvar string) string {
|
||||||
|
return slotRe.ReplaceAllStringFunc(html, func(exp string) string {
|
||||||
|
exp = strings.ReplaceAll(exp, "[{", "{{")
|
||||||
|
exp = strings.ReplaceAll(exp, "}]", "}}")
|
||||||
|
exp = strings.ReplaceAll(exp, "$props", randvar)
|
||||||
|
return exp
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -326,7 +326,7 @@ func (parser *TemplateParser) transTextNode(node *html.Node) {
|
||||||
text = parser.transText(text, keys)
|
text = parser.transText(text, keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
node.Data = text
|
node.Data = strings.Replace(node.Data, strings.TrimSpace(node.Data), text, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *TemplateParser) transElementNode(sel *goquery.Selection) {
|
func (parser *TemplateParser) transElementNode(sel *goquery.Selection) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue