Merge pull request #679 from trheyi/main
refactor: Update BuildContext components to use boolean values instea…
This commit is contained in:
commit
ce45178e65
1 changed files with 53 additions and 22 deletions
|
|
@ -41,12 +41,12 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
|
||||||
namespace := Namespace(page.Route, ctx.sequence, option.ScriptMinify)
|
namespace := Namespace(page.Route, ctx.sequence, option.ScriptMinify)
|
||||||
page.namespace = namespace
|
page.namespace = namespace
|
||||||
|
|
||||||
html, err := page.BuildHTML(option)
|
source, err := page.BuildHTML(option)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.warnings = append(ctx.warnings, err.Error())
|
ctx.warnings = append(ctx.warnings, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
doc, err := NewDocumentString(html)
|
doc, err := NewDocumentString(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ctx.warnings, err
|
return nil, ctx.warnings, err
|
||||||
}
|
}
|
||||||
|
|
@ -72,10 +72,6 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
|
||||||
return nil, ctx.warnings, err
|
return nil, ctx.warnings, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append the scripts and styles
|
|
||||||
ctx.scripts = append(ctx.scripts, scripts...)
|
|
||||||
ctx.styles = append(ctx.styles, styles...)
|
|
||||||
|
|
||||||
// Add the translation marks
|
// Add the translation marks
|
||||||
sequence := 0
|
sequence := 0
|
||||||
err = page.TranslateMarks(ctx, doc, &sequence)
|
err = page.TranslateMarks(ctx, doc, &sequence)
|
||||||
|
|
@ -85,18 +81,25 @@ func (page *Page) Build(ctx *BuildContext, option *BuildOption) (*goquery.Docume
|
||||||
|
|
||||||
// Translate the scripts
|
// Translate the scripts
|
||||||
if (scripts != nil) && len(scripts) > 0 {
|
if (scripts != nil) && len(scripts) > 0 {
|
||||||
for _, script := range scripts {
|
for i, script := range scripts {
|
||||||
if script.Source == "" {
|
if script.Source == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
trans, _, err := page.translateScript(script.Source, &sequence)
|
trans, keys, err := page.translateScript(script.Source, &sequence)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, ctx.warnings, err
|
return nil, ctx.warnings, err
|
||||||
}
|
}
|
||||||
ctx.translations = append(ctx.translations, trans...)
|
if len(keys) > 0 {
|
||||||
|
ctx.translations = append(ctx.translations, trans...)
|
||||||
|
scripts[i].Attrs = append(script.Attrs, html.Attribute{Key: "s:trans-script", Val: strings.Join(keys, ",")})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Append the scripts and styles
|
||||||
|
ctx.scripts = append(ctx.scripts, scripts...)
|
||||||
|
ctx.styles = append(ctx.styles, styles...)
|
||||||
|
|
||||||
return doc, ctx.warnings, err
|
return doc, ctx.warnings, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -142,12 +145,12 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op
|
||||||
ctx.sequence++
|
ctx.sequence++
|
||||||
var opt = *option
|
var opt = *option
|
||||||
opt.IgnoreDocument = true
|
opt.IgnoreDocument = true
|
||||||
html, err := page.BuildHTML(&opt)
|
source, err := page.BuildHTML(&opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
doc, err := NewDocumentStringWithWrapper(html)
|
doc, err := NewDocumentStringWithWrapper(source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -168,8 +171,10 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append the scripts
|
styles, err := page.BuildStyles(ctx, &opt, component, namespace)
|
||||||
ctx.scripts = append(ctx.scripts, scripts...)
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
// Pass the component props
|
// Pass the component props
|
||||||
first := body.Children().First()
|
first := body.Children().First()
|
||||||
|
|
@ -189,25 +194,32 @@ func (page *Page) BuildAsComponent(sel *goquery.Selection, ctx *BuildContext, op
|
||||||
|
|
||||||
// Translate the scripts
|
// Translate the scripts
|
||||||
if (scripts != nil) && len(scripts) > 0 {
|
if (scripts != nil) && len(scripts) > 0 {
|
||||||
for _, script := range scripts {
|
for i, script := range scripts {
|
||||||
if script.Source == "" {
|
if script.Source == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
trans, _, err := page.translateScript(script.Source, &sequence)
|
trans, keys, err := page.translateScript(script.Source, &sequence)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
ctx.translations = append(ctx.translations, trans...)
|
if len(keys) > 0 {
|
||||||
|
ctx.translations = append(ctx.translations, trans...)
|
||||||
|
scripts[i].Attrs = append(script.Attrs, html.Attribute{Key: "s:trans-script", Val: strings.Join(keys, ",")})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
html, err = body.Html()
|
// Append the scripts
|
||||||
|
ctx.scripts = append(ctx.scripts, scripts...)
|
||||||
|
ctx.styles = append(ctx.styles, styles...)
|
||||||
|
|
||||||
|
source, err = body.Html()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
sel.ReplaceWithHtml(html)
|
sel.ReplaceWithHtml(source)
|
||||||
ctx.components[page.Route] = true
|
ctx.components[page.Route] = true
|
||||||
return html, nil
|
return source, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (page *Page) copySlots(ctx *BuildContext, from *goquery.Selection, to *goquery.Selection) error {
|
func (page *Page) copySlots(ctx *BuildContext, from *goquery.Selection, to *goquery.Selection) error {
|
||||||
|
|
@ -593,20 +605,31 @@ func (page *Page) translateNode(node *html.Node, sequence *int) ([]Translation,
|
||||||
|
|
||||||
case html.ElementNode:
|
case html.ElementNode:
|
||||||
|
|
||||||
|
sel := goquery.NewDocumentFromNode(node)
|
||||||
// Script
|
// Script
|
||||||
if node.Data == "script" {
|
if node.Data == "script" {
|
||||||
|
if _, has := sel.Attr("s:trans-script"); has {
|
||||||
|
break
|
||||||
|
}
|
||||||
code := goquery.NewDocumentFromNode(node).Text()
|
code := goquery.NewDocumentFromNode(node).Text()
|
||||||
trans, _, err := page.translateScript(code, sequence)
|
trans, keys, err := page.translateScript(code, sequence)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
translations = append(translations, trans...)
|
if len(keys) > 0 {
|
||||||
|
raw := strings.Join(keys, ",")
|
||||||
|
sel.SetAttr("s:trans-script", raw)
|
||||||
|
translations = append(translations, trans...)
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
sel := goquery.NewDocumentFromNode(node)
|
|
||||||
for _, attr := range node.Attr {
|
for _, attr := range node.Attr {
|
||||||
|
|
||||||
|
if _, has := sel.Attr("s:trans-attr-" + attr.Key); has {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
trans, keys, err := page.translateText(attr.Val, sequence, "attr")
|
trans, keys, err := page.translateText(attr.Val, sequence, "attr")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -632,6 +655,10 @@ func (page *Page) translateNode(node *html.Node, sequence *int) ([]Translation,
|
||||||
case html.TextNode:
|
case html.TextNode:
|
||||||
parentSel := goquery.NewDocumentFromNode(node.Parent)
|
parentSel := goquery.NewDocumentFromNode(node.Parent)
|
||||||
if _, has := parentSel.Attr("s:trans"); has {
|
if _, has := parentSel.Attr("s:trans"); has {
|
||||||
|
if _, has := parentSel.Attr("s:trans-node"); has {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
key := TranslationKey(page.Route, *sequence)
|
key := TranslationKey(page.Route, *sequence)
|
||||||
message := strings.TrimSpace(node.Data)
|
message := strings.TrimSpace(node.Data)
|
||||||
if message != "" {
|
if message != "" {
|
||||||
|
|
@ -646,6 +673,9 @@ func (page *Page) translateNode(node *html.Node, sequence *int) ([]Translation,
|
||||||
parentSel.RemoveAttr("s:trans")
|
parentSel.RemoveAttr("s:trans")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if _, has := parentSel.Attr("s:trans-text"); has {
|
||||||
|
break
|
||||||
|
}
|
||||||
trans, keys, err := page.translateText(node.Data, sequence, "text")
|
trans, keys, err := page.translateText(node.Data, sequence, "text")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -703,6 +733,7 @@ func (page *Page) translateScript(code string, sequence *int) ([]Translation, []
|
||||||
Type: "script",
|
Type: "script",
|
||||||
})
|
})
|
||||||
*sequence = *sequence + 1
|
*sequence = *sequence + 1
|
||||||
|
keys = append(keys, key)
|
||||||
}
|
}
|
||||||
return translations, keys, nil
|
return translations, keys, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue