~dricottone/epub2html

f7d5416bafe786b9eed8459efee8f52d09385d2f — Dominic Ricottone 2 years ago 565f40f
Fixing blockquotes

This hacks on the unmarshaler to add an Order field, so that the
paragraphs and quotes can be normalized and sorted.

Still need to hack on the marshaler so that paragraphs and quotes can be
passed together as an interface array.
1 files changed, 48 insertions(+), 2 deletions(-)

M xml.go
M xml.go => xml.go +48 -2
@@ 3,6 3,7 @@
package main

import (
	"io"
	"encoding/xml"
)



@@ 11,11 12,13 @@ type Head struct {
}

type Paragraph struct {
	Text string `xml:",innerxml"`
	Text  string `xml:",innerxml"`
	Order int    `xml:"-"`
}

type BlockQuote struct {
	Paragraphs  []Paragraph  `xml:"p"`
	Paragraphs []Paragraph `xml:"p"`
	Order      int         `xml:"-"`
}

type Division struct {


@@ 24,6 27,49 @@ type Division struct {
	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"`