Files
inventory-plus-plus/server/ui/svg/element.go
T
angel 82efe19ae0
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
prototyped svg reports
2026-08-19 23:19:41 -06:00

58 lines
1.0 KiB
Go

package svg
import (
"fmt"
"io"
"strings"
)
type (
// Element is a convenience for implementing SVG element models.
// Element implements MarshalerReader.
Element[T Tag, A Attribute, C MarshalerReader] struct {
Attributes []A
Children []C
}
)
func (e *Element[T, A, C]) Attr(as ...A) *Element[T, A, C] {
e.Attributes = append(e.Attributes, as...)
return e
}
func (e *Element[T, A, C]) Child(cs ...C) *Element[T, A, C] {
e.Children = append(e.Children, cs...)
return e
}
func (e Element[T, A, C]) GetMarkup() string {
attrs := make([]string, len(e.Attributes))
for i, a := range e.Attributes {
attrs[i] = PrintAttribute(a)
}
children := make([]string, len(e.Children))
for i, c := range e.Children {
children[i] = c.GetMarkup()
}
tag := e.PrintTag()
return fmt.Sprintf(
`<%s %s>%s</%s>`,
tag,
strings.Join(attrs, " "),
strings.Join(children, ""),
tag,
)
}
func (e Element[T, A, C]) GetMarkupReader() io.Reader {
return newElementReader(e)
}
func (e Element[T, _, _]) PrintTag() string {
var t T
return t.PrintTag()
}