Merge pull request #111 from facundoolano/footnote_in_footnote

Update HTML writer to support footnotes from footnotes
This commit is contained in:
Niklas Fasching 2024-03-23 18:36:55 +01:00 committed by GitHub
commit fc916744d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 4 deletions

View file

@ -46,6 +46,7 @@ type HTMLWriter struct {
type footnotes struct { type footnotes struct {
mapping map[string]int mapping map[string]int
list []*FootnoteDefinition list []*FootnoteDefinition
unused map[string]*FootnoteDefinition
} }
var emphasisTags = map[string][]string{ var emphasisTags = map[string][]string{
@ -89,6 +90,7 @@ func NewHTMLWriter() *HTMLWriter {
TopLevelHLevel: 2, TopLevelHLevel: 2,
footnotes: &footnotes{ footnotes: &footnotes{
mapping: map[string]int{}, mapping: map[string]int{},
unused: map[string]*FootnoteDefinition{},
}, },
} }
} }
@ -228,7 +230,10 @@ func (w *HTMLWriter) WriteFootnotes(d *Document) {
w.WriteString(`<div class="footnotes">` + "\n") w.WriteString(`<div class="footnotes">` + "\n")
w.WriteString(`<hr class="footnotes-separatator"/>` + "\n") w.WriteString(`<hr class="footnotes-separatator"/>` + "\n")
w.WriteString(`<div class="footnote-definitions">` + "\n") w.WriteString(`<div class="footnote-definitions">` + "\n")
for i, definition := range w.footnotes.list {
// iterate by index instead of ranging, since new footnotes can be added when writing the definitions
for i := 0; i < len(w.footnotes.list); i++ {
definition := w.footnotes.list[i]
id := i + 1 id := i + 1
if definition == nil { if definition == nil {
name := "" name := ""
@ -657,6 +662,14 @@ func (fs *footnotes) add(f FootnoteLink) int {
if i, ok := fs.mapping[f.Name]; ok && f.Name != "" { if i, ok := fs.mapping[f.Name]; ok && f.Name != "" {
return i return i
} }
if def, ok := fs.unused[f.Name]; ok && f.Name != "" && f.Definition == nil {
// if there was an a previously unused definition with the same name, attach it to this link
// (this enables footnotes from another footnote's definition)
f.Definition = def
delete(fs.unused, f.Name)
}
fs.list = append(fs.list, f.Definition) fs.list = append(fs.list, f.Definition)
i := len(fs.list) - 1 i := len(fs.list) - 1
if f.Name != "" { if f.Name != "" {
@ -668,5 +681,9 @@ func (fs *footnotes) add(f FootnoteLink) int {
func (fs *footnotes) updateDefinition(f FootnoteDefinition) { func (fs *footnotes) updateDefinition(f FootnoteDefinition) {
if i, ok := fs.mapping[f.Name]; ok { if i, ok := fs.mapping[f.Name]; ok {
fs.list[i] = &f fs.list[i] = &f
} else {
// this could either be an unused definition or one used in another footnote
// save in case it's the latter
fs.unused[f.Name] = &f
} }
} }

View file

@ -116,7 +116,13 @@ Rather, it will be somewhere down below in the footnotes section.</p>
<div class="footnote-body"> <div class="footnote-body">
<p> <p>
There&#39;s multiple reasons for that. Among others, doing so requires i18n (to recognize the section) and silently There&#39;s multiple reasons for that. Among others, doing so requires i18n (to recognize the section) and silently
hides content before and after the footnotes.</p> hides content before and after the footnotes<sup class="footnote-reference"><a id="footnote-reference-9" href="#footnote-9">9</a></sup>.</p>
</div>
</div>
<div class="footnote-definition">
<sup id="footnote-9"><a href="#footnote-reference-9">9</a></sup>
<div class="footnote-body">
<p>Footnotes can be linked from another footnote&#39;s definition.</p>
</div> </div>
</div> </div>
</div> </div>

View file

@ -41,8 +41,11 @@ This shouldn't happen when the definition line and the line after that are empty
[fn:7] [fn:7]
There's multiple reasons for that. Among others, doing so requires i18n (to recognize the section) and silently There's multiple reasons for that. Among others, doing so requires i18n (to recognize the section) and silently
hides content before and after the footnotes. hides content before and after the footnotes[fn:8].
this is not part of [fn:7] anymore as there are 2 blank lines in between! this is not part of [fn:7] anymore as there are 2 blank lines in between!
[fn:8] Footnotes can be linked from another footnote's definition.

View file

@ -41,8 +41,11 @@ This shouldn't happen when the definition line and the line after that are empty
[fn:7] [fn:7]
There's multiple reasons for that. Among others, doing so requires i18n (to recognize the section) and silently There's multiple reasons for that. Among others, doing so requires i18n (to recognize the section) and silently
hides content before and after the footnotes. hides content before and after the footnotes[fn:8].
this is not part of [fn:7] anymore as there are 2 blank lines in between! this is not part of [fn:7] anymore as there are 2 blank lines in between!
[fn:8] Footnotes can be linked from another footnote's definition.