From eb72bab8863ed6c1f2f9042db2533b47da6e4cc7 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L de Mello" Date: Tue, 20 May 2025 10:12:02 -0300 Subject: [PATCH] feat(ipub,ast): image element --- ipub/ast/content.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/ipub/ast/content.go b/ipub/ast/content.go index a6d06ea..37d291c 100644 --- a/ipub/ast/content.go +++ b/ipub/ast/content.go @@ -2,7 +2,7 @@ package ast import ( "encoding/xml" - "io" + "slices" ) type Content struct { @@ -27,3 +27,46 @@ func (e *Content) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error { return e.UnmarshalXMLElement(e, dec, start) } +type Image struct { + src string + + BaseElement +} + +var KindImage = NewElementKind("image", &Image{}) + +func (e *Image) Name() ElementName { + return ElementName{Local: "img"} +} + +func (e *Image) Kind() ElementKind { + return KindImage +} + +func (e *Image) MarshalXML(enc *xml.Encoder, start xml.StartElement) error { + if src := e.Source(); src != "" { + start.Attr = append(start.Attr, xml.Attr{ + Name: xml.Name{Local: "src"}, + Value: src, + }) + } + return e.MarshalXMLElement(e, enc, start) +} + +func (e *Image) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error { + i := slices.IndexFunc(start.Attr, func(a xml.Attr) bool { + return a.Name == xml.Name{Local: "src"} + }) + if i > -1 { + e.SetSource(start.Attr[i].Value) + } + return e.UnmarshalXMLElement(e, dec, start) +} + +func (e Image) Source() string { + return e.src +} + +func (e *Image) SetSource(src string) { + e.src = src +}