Skip to content

Commit

Permalink
Merge pull request #111 from facundoolano/footnote_in_footnote
Browse files Browse the repository at this point in the history
Update HTML writer to support footnotes from footnotes
  • Loading branch information
niklasfasching authored Mar 23, 2024
2 parents a32df14 + 3f3817e commit fc91674
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
19 changes: 18 additions & 1 deletion org/html_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type HTMLWriter struct {
type footnotes struct {
mapping map[string]int
list []*FootnoteDefinition
unused map[string]*FootnoteDefinition
}

var emphasisTags = map[string][]string{
Expand Down Expand Up @@ -89,6 +90,7 @@ func NewHTMLWriter() *HTMLWriter {
TopLevelHLevel: 2,
footnotes: &footnotes{
mapping: map[string]int{},
unused: map[string]*FootnoteDefinition{},
},
}
}
Expand Down Expand Up @@ -228,7 +230,10 @@ func (w *HTMLWriter) WriteFootnotes(d *Document) {
w.WriteString(`<div class="footnotes">` + "\n")
w.WriteString(`<hr class="footnotes-separatator"/>` + "\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
if definition == nil {
name := ""
Expand Down Expand Up @@ -657,6 +662,14 @@ func (fs *footnotes) add(f FootnoteLink) int {
if i, ok := fs.mapping[f.Name]; ok && f.Name != "" {
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)
i := len(fs.list) - 1
if f.Name != "" {
Expand All @@ -668,5 +681,9 @@ func (fs *footnotes) add(f FootnoteLink) int {
func (fs *footnotes) updateDefinition(f FootnoteDefinition) {
if i, ok := fs.mapping[f.Name]; ok {
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
}
}
8 changes: 7 additions & 1 deletion org/testdata/footnotes.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ <h2 id="headline-2">
<div class="footnote-body">
<p>
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>
Expand Down
5 changes: 4 additions & 1 deletion org/testdata/footnotes.org
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ This shouldn't happen when the definition line and the line after that are empty

[fn:7]
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!


[fn:8] Footnotes can be linked from another footnote's definition.
5 changes: 4 additions & 1 deletion org/testdata/footnotes.pretty_org
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ This shouldn't happen when the definition line and the line after that are empty

[fn:7]
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!


[fn:8] Footnotes can be linked from another footnote's definition.

0 comments on commit fc91674

Please sign in to comment.