htmgo/tools/html-to-htmgo/internal/domain/node.go
2024-10-11 10:25:41 -05:00

127 lines
2.8 KiB
Go

package domain
import (
"fmt"
"slices"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type CustomNode struct {
ParentNode *CustomNode
Level uint
customType bool
Type string
Attrs []Attr
Nodes []*CustomNode
}
func (n *CustomNode) SetType(in string) {
switch in {
case "head":
n.Type = "h.Head"
case "thead":
n.Type = "h.THead"
case "tbody":
n.Type = "h.TBody"
case "id":
n.Type = "h.Id"
case "path":
n.Type = "path"
n.customType = true
case "circle":
n.Type = "circle"
n.customType = true
case "rect":
n.Type = "rect"
n.customType = true
case "line":
n.Type = "line"
n.customType = true
case "polyline":
n.Type = "line"
n.customType = true
case "svg":
n.Type = "h.Svg"
default:
n.Type = fmt.Sprintf("h.%s", cases.Title(language.English).String(in))
}
}
func (n *CustomNode) AddAttr(key, value string) {
if slices.Contains([]string{"xmlns", "fill", "viewBox", "stroke", "stroke-width", "fill-rule", "d", "stroke-linecap", "stroke-linejoin", "cx", "cy", "r", "x", "y", "rx", "ry", "x1", "x2", "y1", "y2", "points"}, key) {
n.Attrs = append(n.Attrs, Attr{
custom: true,
key: key,
value: value,
})
return
}
switch {
case key == "id":
n.Attrs = append(n.Attrs, Attr{key: "h.Id", value: value})
case key == "tabindex":
n.Attrs = append(n.Attrs, Attr{key: "h.TabIndex", value: value})
case key == "h.Text":
n.Attrs = append(n.Attrs, Attr{key: key, value: value})
case strings.ContainsRune(key, '-'):
n.Attrs = append(n.Attrs, Attr{
custom: true,
key: key,
value: value,
})
fmt.Printf("key: %s, value: %s\n", key, value)
default:
n.Attrs = append(n.Attrs, Attr{key: "h." + cases.Title(language.English).String(key), value: value})
}
}
func (n *CustomNode) String() string {
str := ""
if n.customType {
str += "h.Tag(\"" + n.Type + "\","
} else {
str += n.Type + "("
}
if len(n.Attrs) > 0 {
for _, v := range n.Attrs {
switch {
case v.custom:
str = fmt.Sprintf("%sh.Attribute(\"%s\",\"%s\"),", str, v.key, v.value)
case v.hyphenated:
str = fmt.Sprintf("%s%s(\"%s\", \"%s\"),", str, v.key, v.arg, v.value)
case len(v.value) > 0:
if strings.Contains(v.value, "\n") {
str = fmt.Sprintf("%s%s(`%s`),", str, v.key, v.value)
} else {
str = fmt.Sprintf("%s%s(\"%s\"),", str, v.key, v.value)
}
case v.value == "":
str = fmt.Sprintf("%s%s(\"\"),", str, v.key)
default:
str = fmt.Sprintf("%s%s(),", str, v.key)
}
}
}
if len(n.Nodes) > 0 {
for _, v := range n.Nodes {
if v.Type != "" {
str = fmt.Sprintf("%s\n%s%s,", str, strings.Repeat(" ", int(n.Level)), v)
}
}
}
str = fmt.Sprintf("%s\n%s)", str, strings.Repeat(" ", int(n.Level)))
return str
}
type Attr struct {
custom, hyphenated bool
key, value, arg string
}