Skip to content

Commit

Permalink
Ficed notes body processing to supprt images in the notees body and c…
Browse files Browse the repository at this point in the history
…ross-links
  • Loading branch information
rupor-github committed Jan 20, 2024
1 parent 3eb52f8 commit 5a51dd1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
12 changes: 9 additions & 3 deletions processor/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand Down
18 changes: 13 additions & 5 deletions processor/xhtml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 <p> tag first
children = append([]*etree.Element{etree.NewElement("p")}, children...)
}
first := true
for i, c := range children {
cc := c.Copy()
Expand All @@ -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.)")
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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{}
Expand All @@ -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{}
Expand Down

0 comments on commit 5a51dd1

Please sign in to comment.