46 lines
880 B
Go
46 lines
880 B
Go
package epub
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"fmt"
|
|
)
|
|
|
|
type Meta struct {
|
|
Attributes map[string]string `xml:"-"`
|
|
Value string `xml:",chardata"`
|
|
}
|
|
|
|
var (
|
|
_ xml.Marshaler = Meta{}
|
|
_ xml.Unmarshaler = (*Meta)(nil)
|
|
)
|
|
|
|
func (m Meta) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
|
for n, v := range m.Attributes {
|
|
start.Attr = append(start.Attr, xml.Attr{
|
|
Name: xml.Name{Local: n},
|
|
Value: v,
|
|
})
|
|
}
|
|
return e.EncodeElement(m.Value, start)
|
|
}
|
|
|
|
func (m *Meta) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
if m == nil {
|
|
m = &Meta{}
|
|
}
|
|
if m.Attributes == nil {
|
|
m.Attributes = map[string]string{}
|
|
}
|
|
|
|
for _, attr := range start.Attr {
|
|
m.Attributes[attr.Name.Local] = attr.Value
|
|
}
|
|
|
|
if err := d.DecodeElement(&m.Value, &start); err != nil {
|
|
return fmt.Errorf("epub.Meta: failed to decode chardata: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|