// Structs for parsing X(HT)?ML files in e-pub archives
package main
import (
"io"
"encoding/xml"
)
type Head struct {
Title string `xml:"title"`
}
type Paragraph struct {
Text string `xml:",innerxml"`
Order int `xml:"-"`
}
type BlockQuote struct {
Paragraphs []Paragraph `xml:"p"`
Order int `xml:"-"`
}
type Division struct {
Divisions []Division `xml:"div"`
Paragraphs []Paragraph `xml:"p"`
BlockQuotes []BlockQuote `xml:"blockquote"`
}
func (d *Division) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error {
counter := 0
for {
token, err := decoder.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
switch token.(type) {
case xml.StartElement:
new_start := token.(xml.StartElement)
if (new_start.Name.Local == "p") {
target := Paragraph{}
decoder.DecodeElement(&target, &new_start)
target.Order = counter
counter += 1
d.Paragraphs = append(d.Paragraphs, target)
} else if (new_start.Name.Local == "blockquote") {
target := BlockQuote{}
decoder.DecodeElement(&target, &new_start)
target.Order = counter
counter += 1
d.BlockQuotes = append(d.BlockQuotes, target)
} else if (new_start.Name.Local == "div") {
target := Division{}
decoder.DecodeElement(&target, &new_start)
d.Divisions = append(d.Divisions, target)
}
}
}
return nil
}
type Body struct {
Title string `xml:"h3"`
Division Division `xml:"div"`
}
type Xhtml struct {
XMLName xml.Name `xml:"html"`
Head Head `xml:"head"`
Body Body `xml:"body"`
}
type Content struct {
Src string `xml:"src,attr"`
}
type NavPoint struct {
Label string `xml:"navLabel>text"`
Content Content `xml:"content"`
Order int `xml:"playOrder,attr"`
}
type Ncx struct {
XMLName xml.Name `xml:"ncx"`
Title string `xml:"docTitle>text"`
NavPoints []NavPoint `xml:"navMap>navPoint"`
}