diff --git a/processor/process.go b/processor/process.go index 86f697d..1f4b64e 100644 --- a/processor/process.go +++ b/processor/process.go @@ -226,13 +226,14 @@ func (p *Processor) Process() error { // Processing - order of steps and their presence are important as information and context // being built and accumulated... - if err := p.processDescription(); err != nil { + // notes may contain images, so we need to process them first + if err := p.processBinaries(); err != nil { return err } if err := p.processNotes(); err != nil { return err } - if err := p.processBinaries(); err != nil { + if err := p.processDescription(); err != nil { return err } if err := p.processBodies(); err != nil { @@ -709,6 +710,9 @@ func (p *Processor) parseNoteSectionElement(el *etree.Element, name string, note } ctx := p.ctxPush() ctx.inHeader = true + // we know exactly what name would be + ctx.fname = GenSafeName(name) + ".xhtml" + // we know exactly what name would be if err := p.transfer(el, &ctx.out.Element, "div", "h0"); err != nil { p.env.Log.Warn("Unable to parse notes body title", zap.String("path", el.GetPath()), zap.Error(err)) } @@ -742,7 +746,9 @@ func (p *Processor) parseNoteSectionElement(el *etree.Element, name string, note note.body += "\n" } note.body += getFullTextFragment(c) - p.ctxPush() + ctx := p.ctxPush() + // we know exactly what name would be + ctx.fname = GenSafeName(name) + ".xhtml" if err := p.transfer(c, noteXml.Root(), c.Tag); err != nil { p.env.Log.Warn("Unable to parse notes body", zap.String("path", c.GetPath()), zap.Error(err)) } diff --git a/processor/xhtml.go b/processor/xhtml.go index fbca663..3fbe079 100644 --- a/processor/xhtml.go +++ b/processor/xhtml.go @@ -26,10 +26,11 @@ func (p *Processor) processBody(index int, from *etree.Element) (err error) { // setup processing context if index != 0 { - // always ignore first body name - it is main body p.ctx().bodyName = getAttrValue(from, "name") + } else { + // always ignore first body name - it is main body + p.ctx().bodyName = "" } - p.ctx().header = 0 p.ctx().firstBodyTitle = true @@ -140,6 +141,10 @@ func (p *Processor) processBody(index int, from *etree.Element) (err error) { } else { aside := to.AddNext("aside", attr("id", nl.id), attr("epub:type", "footnote")).SetTail("\n") children := note.parsed.ChildElements() + if children[0].Tag != "p" { + // to get properly formatted note we need to have

tag first + children = append([]*etree.Element{etree.NewElement("p")}, children...) + } first := true for i, c := range children { cc := c.Copy() @@ -155,6 +160,11 @@ func (p *Processor) processBody(index int, from *etree.Element) (err error) { if cc.Tag == "p" { cc.CreateAttr("class", "floatnote") } + for _, a := range cc.FindElements(".//p") { + if len(getAttrValue(a, "class")) == 0 { + a.CreateAttr("class", "floatnote") + } + } if p.notesMode == NFloatNewMore && len(children) > 1 && cc.Tag == "p" && first { // indicate that note body has more than one paragraph cc.CreateCharData(" (…etc.)") @@ -463,7 +473,7 @@ func (p *Processor) transfer(from, to *etree.Element, decorations ...string) err attrs[0] = attr("id", newid) attrs[1] = attr("class", css) attrs[2] = attr("href", href) - if (p.notesMode == NFloatNew || p.notesMode == NFloatNewMore) && tag == "a" { + if (p.notesMode == NFloatNew || p.notesMode == NFloatNewMore) && tag == "a" && css == "anchor" { attrs = append(attrs, attr("epub:type", "noteref")) } inner = to.AddNext(tag, attrs...) @@ -514,7 +524,6 @@ func (p *Processor) transfer(from, to *etree.Element, decorations ...string) err if len(p.ctx().currentNotes) > 0 { // insert inline and block notes if p.notesMode == NInline && tag == "span" { - // inner = to.AddNext("span", attr("class", "inlinenote")).SetText(currentNotes[0].body) inner = to.AddNext("span", attr("class", "inlinenote")) p.formatText(currentNotes[0].body, false, false, inner) p.ctx().currentNotes = []*note{} @@ -525,7 +534,6 @@ func (p *Processor) transfer(from, to *etree.Element, decorations ...string) err if i, err := strconv.Atoi(t); err == nil { t = fmt.Sprintf("%d) ", i) } - // inner.AddNext("p").AddNext("span", attr("class", "notenum")).SetText(t).SetTail(n.body) p.formatText(n.body, false, true, inner.AddNext("p").AddNext("span", attr("class", "notenum")).SetText(t)) } p.ctx().currentNotes = []*note{}