/
transform_tag.go
60 lines (49 loc) · 1.56 KB
/
transform_tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package pugjs
import (
"bytes"
"fmt"
"strings"
)
// Render an interpolated tag
func (it *InterpolatedTag) Render(p *renderState, wr *bytes.Buffer) error {
return it.CommonTag.render(p.JsExpr(it.Expr, true, false), p, wr)
}
// Render a tag
func (t *Tag) Render(p *renderState, wr *bytes.Buffer) error {
return t.CommonTag.render(t.Name, p, wr)
}
func (ct *CommonTag) render(name string, p *renderState, wr *bytes.Buffer) error {
var subblock = new(bytes.Buffer)
if err := ct.Block.Render(p, subblock); err != nil {
return err
}
var attrs string
if len(ct.AttributeBlocks) > 0 || len(ct.Attrs) > 0 {
attrs = `{{ __attrs `
for _, attr := range ct.Attrs {
if attr.MustEscape {
attrs += fmt.Sprintf(`(__attr %q %s %t) `, attr.Name, p.JsExpr(attr.Val, false, false), attr.MustEscape)
} else {
attrs += fmt.Sprintf(`(__attr %q %q %t) `, attr.Name, p.JsExpr(attr.Val, false, false), attr.MustEscape)
}
}
for _, ab := range ct.AttributeBlocks {
attrs += fmt.Sprintf(`(__and_attrs $%s)`, ab)
}
attrs += ` }}`
}
switch {
case ct.SelfClosing:
fmt.Fprintf(wr, `<%s%s>`, name, attrs)
case name == "script" && strings.Index(subblock.String(), "\n") > -1:
fmt.Fprintf(wr, "<%s%s>\n%s\n</%s>", name, attrs, subblock.String(), name)
case !ct.Block.Inline() && p.debug:
fmt.Fprintf(wr, "<%s%s> {{- \"\" -}}\n%s {{- \"\" -}}\n</%s>", name, attrs, subblock.String(), name)
default:
fmt.Fprintf(wr, `<%s%s>%s</%s>`, name, attrs, subblock.String(), name)
}
if !ct.Inline() && p.debug {
wr.WriteString(" {{- \"\" -}}\n")
}
return nil
}