Merge pull request #560 from trheyi/main
Add support for rendering request data in template parser
This commit is contained in:
commit
cae8de841c
2 changed files with 72 additions and 21 deletions
|
|
@ -179,7 +179,7 @@ func (r *Request) Render() (string, int, error) {
|
||||||
printData = true
|
printData = true
|
||||||
}
|
}
|
||||||
|
|
||||||
parser := core.NewTemplateParser(data, &core.ParserOption{PrintData: printData})
|
parser := core.NewTemplateParser(data, &core.ParserOption{PrintData: printData, Request: true})
|
||||||
html, err := parser.Render(c.HTML)
|
html, err := parser.Render(c.HTML)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", 500, fmt.Errorf("render error, please re-complie the page %s", err.Error())
|
return "", 500, fmt.Errorf("render error, please re-complie the page %s", err.Error())
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,18 @@ type ParserOption struct {
|
||||||
Editor bool `json:"editor,omitempty"`
|
Editor bool `json:"editor,omitempty"`
|
||||||
Preview bool `json:"preview,omitempty"`
|
Preview bool `json:"preview,omitempty"`
|
||||||
PrintData bool `json:"print_data,omitempty"`
|
PrintData bool `json:"print_data,omitempty"`
|
||||||
|
Request bool `json:"request,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var keepWords = map[string]bool{
|
||||||
|
"s:if": true,
|
||||||
|
"s:for": true,
|
||||||
|
"s:for-item": true,
|
||||||
|
"s:for-index": true,
|
||||||
|
"s:elif": true,
|
||||||
|
"s:else": true,
|
||||||
|
"s:set": true,
|
||||||
|
"s:bind": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTemplateParser create a new template parser
|
// NewTemplateParser create a new template parser
|
||||||
|
|
@ -98,6 +110,15 @@ func (parser *TemplateParser) Render(html string) (string, error) {
|
||||||
return doc.Find("body").Html()
|
return doc.Find("body").Html()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For Request
|
||||||
|
if parser.option != nil && (parser.option.Request || parser.option.Preview) {
|
||||||
|
// Remove the sui-hide attribute
|
||||||
|
doc.Find("[sui-hide]").Remove()
|
||||||
|
|
||||||
|
// Remove All comments
|
||||||
|
parser.tidy(doc.Selection)
|
||||||
|
}
|
||||||
|
|
||||||
// fmt.Println(doc.Html())
|
// fmt.Println(doc.Html())
|
||||||
// fmt.Println(parser.errors)
|
// fmt.Println(parser.errors)
|
||||||
return doc.Html()
|
return doc.Html()
|
||||||
|
|
@ -406,17 +427,19 @@ func (parser *TemplateParser) hide(sel *goquery.Selection) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
style := sel.AttrOr("style", "")
|
sel.SetAttr("sui-hide", "true")
|
||||||
if strings.Contains(style, "display: none") {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if style != "" {
|
// style := sel.AttrOr("style", "")
|
||||||
style = fmt.Sprintf("%s; display: none", style)
|
// if strings.Contains(style, "display: none") {
|
||||||
} else {
|
// return
|
||||||
style = "display: none"
|
// }
|
||||||
}
|
|
||||||
sel.SetAttr("style", style)
|
// if style != "" {
|
||||||
|
// style = fmt.Sprintf("%s; display: none", style)
|
||||||
|
// } else {
|
||||||
|
// style = "display: none"
|
||||||
|
// }
|
||||||
|
// sel.SetAttr("style", style)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *TemplateParser) show(sel *goquery.Selection) {
|
func (parser *TemplateParser) show(sel *goquery.Selection) {
|
||||||
|
|
@ -426,18 +449,46 @@ func (parser *TemplateParser) show(sel *goquery.Selection) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
style := sel.AttrOr("style", "")
|
sel.RemoveAttr("sui-hide")
|
||||||
if !strings.Contains(style, "display: none") {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
style = strings.ReplaceAll(style, "display: none", "")
|
// style := sel.AttrOr("style", "")
|
||||||
if style == "" {
|
// if !strings.Contains(style, "display: none") {
|
||||||
sel.RemoveAttr("style")
|
// return
|
||||||
return
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
sel.SetAttr("style", style)
|
// style = strings.ReplaceAll(style, "display: none", "")
|
||||||
|
// if style == "" {
|
||||||
|
// sel.RemoveAttr("style")
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
|
// sel.SetAttr("style", style)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (parser *TemplateParser) tidy(selection *goquery.Selection) {
|
||||||
|
selection.Contents().Each(func(i int, s *goquery.Selection) {
|
||||||
|
|
||||||
|
if s.Nodes[0].Type == html.CommentNode {
|
||||||
|
s.Remove()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove the s:key-* attributes
|
||||||
|
if s.Nodes[0].Type == html.ElementNode {
|
||||||
|
for _, attr := range s.Nodes[0].Attr {
|
||||||
|
if attr.Key == "parsed" || keepWords[attr.Key] || strings.HasPrefix(attr.Key, "s:key") || strings.HasPrefix(attr.Key, "s:bind") {
|
||||||
|
s.RemoveAttr(attr.Key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.Nodes[0].Data == "s:set" {
|
||||||
|
s.Remove()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parser.tidy(s)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (parser *TemplateParser) key(prefix string, sel *goquery.Selection) string {
|
func (parser *TemplateParser) key(prefix string, sel *goquery.Selection) string {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue