Skip to content

Commit

Permalink
Added float-new-more notes mode
Browse files Browse the repository at this point in the history
  • Loading branch information
rupor-github committed Jan 18, 2024
1 parent 0f220c5 commit 3eb52f8
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 24 deletions.
1 change: 1 addition & 0 deletions processor/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
NFloat // float
NFloatOld // float-old
NFloatNew // float-new
NFloatNewMore // float-new-more
UnsupportedNotesFmt //
)

Expand Down
7 changes: 4 additions & 3 deletions processor/enums_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion processor/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func NewFB2(r io.Reader, unknownEncoding bool, src, dst string, nodirs, stk, ove
env.Log.Warn("Unknown notes mode requested, switching to default", zap.String("mode", env.Cfg.Doc.Notes.Mode))
notes = NDefault
}
if notes != NFloat && notes != NFloatOld && notes != NFloatNew && env.Cfg.Doc.Notes.Renumber {
if notes != NFloat && notes != NFloatOld && notes != NFloatNew && notes != NFloatNewMore && env.Cfg.Doc.Notes.Renumber {
env.Log.Warn("Notes can be renumbered in floating modes only, ignoring", zap.String("mode", env.Cfg.Doc.Notes.Mode))
}
toct := ParseTOCTypeString(env.Cfg.Doc.TOC.Type)
Expand Down
31 changes: 17 additions & 14 deletions processor/xhtml.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (p *Processor) processBody(index int, from *etree.Element) (err error) {
if p.notesMode == NDefault || !IsOneOf(p.ctx().bodyName, p.env.Cfg.Doc.Notes.BodyNames) {
// initialize first XHTML buffer
ns := []*etree.Attr{attr("xmlns", `http://www.w3.org/1999/xhtml`)}
if p.notesMode == NFloatNew {
if p.notesMode == NFloatNew || p.notesMode == NFloatNewMore {
ns = append(ns, attr("xmlns:epub", `http://www.idpf.org/2007/ops`))
}
to, f := p.ctx().createXHTML("", ns...)
Expand All @@ -53,14 +53,14 @@ func (p *Processor) processBody(index int, from *etree.Element) (err error) {
return p.transfer(from, to)
}

if p.notesMode != NFloat && p.notesMode != NFloatOld && p.notesMode != NFloatNew {
if p.notesMode < NFloat {
// NOTE: for block and inline notes we do not need to save XHTML, have nothing to put there
return nil
}

// initialize XHTML buffer for notes
ns := []*etree.Attr{attr("xmlns", `http://www.w3.org/1999/xhtml`)}
if p.notesMode == NFloatNew {
if p.notesMode == NFloatNew || p.notesMode == NFloatNewMore {
ns = append(ns, attr("xmlns:epub", `http://www.idpf.org/2007/ops`))
}
to, f := p.ctx().createXHTML("", ns...)
Expand Down Expand Up @@ -128,7 +128,7 @@ func (p *Processor) processBody(index int, from *etree.Element) (err error) {
}
}
// NOTE: we are adding .SetTail("\n") to make result readable when debugging, it does not have any other use
if p.notesMode == NFloatNew {
if p.notesMode == NFloatNew || p.notesMode == NFloatNewMore {
// new bidirectional mode
if len(note.parsed.ChildElements()) == 0 || len(note.parsed.Child) == 0 {
p.env.Log.Warn("Unable to interpret parsed note body, ignoring xml...",
Expand All @@ -139,7 +139,9 @@ func (p *Processor) processBody(index int, from *etree.Element) (err error) {
AddNext("a", attr("href", backRef+"#"+backID)).SetText(t).SetTail(strNBSP + note.body)
} else {
aside := to.AddNext("aside", attr("id", nl.id), attr("epub:type", "footnote")).SetTail("\n")
for i, c := range note.parsed.ChildElements() {
children := note.parsed.ChildElements()
first := true
for i, c := range children {
cc := c.Copy()
if i == 0 {
// We need to insert back ref anchor into first note xml element as a first child, so popup would recognize it properly
Expand All @@ -153,18 +155,17 @@ 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.)")
first = false
}
aside.AddChild(cc)
}
aside.AddNext("div", attr("class", "emptyline"))
}
} else {
// old bi-directional mode
// to.AddNext("p", attr("class", "floatnote"), attr("id", nl.id)).SetTail("\n").AddNext("a", attr("href", backRef+"#"+backID)).SetText(t).SetTail(strNBSP + note.body)
p.formatText(strNBSP+note.body, false, true,
to.AddNext("p", attr("class", "floatnote"), attr("id", nl.id)).SetTail("\n").
AddNext("a", attr("href", backRef+"#"+backID)).SetText(t))
Expand Down Expand Up @@ -407,6 +408,8 @@ func (p *Processor) transfer(from, to *etree.Element, decorations ...string) err
case NFloatOld:
fallthrough
case NFloatNew:
fallthrough
case NFloatNewMore:
if note, ok := p.Book.Notes[noteID]; !ok {
css = "linkanchor"
} else {
Expand Down Expand Up @@ -460,7 +463,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 && tag == "a" {
if (p.notesMode == NFloatNew || p.notesMode == NFloatNewMore) && tag == "a" {
attrs = append(attrs, attr("epub:type", "noteref"))
}
inner = to.AddNext(tag, attrs...)
Expand Down Expand Up @@ -629,7 +632,7 @@ func transferSubtitle(p *Processor, from, to *etree.Element) error {
if t == dv && !p.ctx().inHeader && !p.ctx().inSubHeader && len(p.ctx().bodyName) == 0 && !p.ctx().specialParagraph {
// open next XHTML
ns := []*etree.Attr{attr("xmlns", `http://www.w3.org/1999/xhtml`)}
if p.notesMode == NFloatNew {
if p.notesMode == NFloatNew || p.notesMode == NFloatNewMore {
ns = append(ns, attr("xmlns:epub", `http://www.idpf.org/2007/ops`))
}
var f *dataFile
Expand All @@ -655,7 +658,7 @@ func transferParagraph(p *Processor, from, to *etree.Element) error {
!p.ctx().inHeader && !p.ctx().inSubHeader && len(p.ctx().bodyName) == 0 && !p.ctx().specialParagraph {
// open next XHTML
ns := []*etree.Attr{attr("xmlns", `http://www.w3.org/1999/xhtml`)}
if p.notesMode == NFloatNew {
if p.notesMode == NFloatNew || p.notesMode == NFloatNewMore {
ns = append(ns, attr("xmlns:epub", `http://www.idpf.org/2007/ops`))
}
var f *dataFile
Expand Down Expand Up @@ -805,7 +808,7 @@ func transferSection(p *Processor, from, to *etree.Element) error {
if len(p.ctx().bodyName) == 0 && p.ctx().header.Int() < p.env.Cfg.Doc.ChapterLevel {
// open next XHTML
ns := []*etree.Attr{attr("xmlns", `http://www.w3.org/1999/xhtml`)}
if p.notesMode == NFloatNew {
if p.notesMode == NFloatNew || p.notesMode == NFloatNewMore {
ns = append(ns, attr("xmlns:epub", `http://www.idpf.org/2007/ops`))
}
var f *dataFile
Expand Down
15 changes: 9 additions & 6 deletions static/configuration.toml
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,15 @@

[document.notes]
#---- How to render notes in the book
#---- "default" - notes are links
#---- "inline" - note is shown "in-place" and could be styled via css
#---- "block" - notes are shown "in-place" at the paragraph end and could be styled via css
#---- "float" - pop up notes using "bi-directional links" method
#---- "float-old" - same as "float", pop up notes using "bi-directional links" method
#---- "float-new" - pop up notes using "preferred" method - HTML5 with <aside> recommended by Amazon publishing guidelines
#---- "default" - notes are links
#---- "inline" - note is shown "in-place" and could be styled via css
#---- "block" - notes are shown "in-place" at the paragraph end and could be styled via css
#---- "float" - pop up notes using "bi-directional links" method
#---- "float-old" - same as "float", pop up notes using "bi-directional links" method
#---- "float-new" - pop up notes using "preferred" method - HTML5 with <aside> recommended by Amazon publishing guidelines
#---- "float-new-more" - pop up notes using "preferred" method - HTML5 with <aside> recommended by Amazon publishing guidelines.
#---- Shows (…etc.) at the end of first paragraph of the note when note has more than one paragraph (Kindle shows
#---- only first paragraph in floating window)
mode = "default"
#---- Names of the <body> tags in fb2 document to consider for notes processing
body_names = [ "notes", "comments" ]
Expand Down
Binary file modified static/resources/not_found.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3eb52f8

Please sign in to comment.