package htmlx import "github.com/d5/tengo/v2" var Module = map[string] tengo.Object{ "Renderer" : &tengo.BuiltinFunction{ Name: "Renderer", Value: func(args ...tengo.Object) (tengo.Object, error){ ret := &Renderer{} return ret, nil }, }, } type Renderer struct{ tengo.ObjectImpl } func (html *Renderer) TypeName() string { return "htmlx.Renderer" } func (html *Renderer) String() (string) { return "htmlx.Renderer{...}" } /* html.div({ id: "some-el-id", value: "shit value" }).body( html.raw("cock "), html.strong("something") ) */ func (html *Renderer) IndexGet( index tengo.Object, ) (tengo.Object, error) { str, ok := tengo.ToString(index) if !ok { return nil, tengo.ErrInvalidIndexValueType } fn := func(args ...tengo.Object) (tengo.Object, error) { if len(args) > 1 { return nil, tengo.ErrWrongNumArguments } var arg tengo.Object if len(args) == 1 { arg = args[0] } if arg == nil { return &Element{ Tag: str, }, nil } if can := arg.CanIterate() ; !can { return nil, tengo.ErrInvalidArgumentType{ Name: "first", Expected: "iterable", Found: arg.TypeName(), } } attr := map[string] string{} iter := arg.Iterate() for iter.Next() { key, val := iter.Key(), iter.Value() skey, ok := tengo.ToString(key) if !ok { return nil, tengo.ErrInvalidArgumentType{ Name: "attribute(key)", Expected: "stringer", Found: key.TypeName(), } } sval, ok := tengo.ToString(val) if !ok { return nil, tengo.ErrInvalidArgumentType{ Name: "attribute(value)", Expected: "stringer", Found: val.TypeName(), } } attr[skey] = sval } return &Element{ Tag: str, Attr: attr, }, nil } return &tengo.UserFunction{ Name: str, Value: fn, }, nil }