Refactor footnote handling

- Remove unused footnote section title option
- Move away from maintaining a list of footnotes in the document (only needed
  for html export, potential maintainance overhead when modifying the document)
  and rather only build it on export when required.
- HTML export: Rename all footnotes to numbers (so we can support anonymous
  footnote references by assigning them a number) and export footnotes in order
  of reference, not definition. The implementation of this makes it natural to
  also stop exporting unused footnote definitions so we do that as well.
This commit is contained in:
Niklas Fasching 2019-07-07 09:52:23 +02:00
parent 50395f999a
commit 6dc04b4b02
8 changed files with 69 additions and 100 deletions

View file

@ -35,7 +35,6 @@ type Document struct {
Path string // Path of the file containing the parse input - used to resolve relative paths during parsing (e.g. INCLUDE).
tokens []token
Nodes []Node
Footnotes Footnotes
Outline Outline // Outline is a Table Of Contents for the document and contains all sections (headline + content).
BufferSettings map[string]string // Settings contains all settings that were parsed from keywords.
Error error
@ -114,10 +113,6 @@ func (c *Configuration) Parse(input io.Reader, path string) (d *Document) {
outlineSection := &Section{}
d = &Document{
Configuration: c,
Footnotes: Footnotes{
Title: "Footnotes",
Definitions: map[string]*FootnoteDefinition{},
},
Outline: Outline{outlineSection, outlineSection, 0},
BufferSettings: map[string]string{},
Path: path,
@ -243,16 +238,6 @@ func (d *Document) parseMany(i int, stop stopFn) (int, []Node) {
return i - start, nodes
}
func (d *Document) addFootnote(name string, definition *FootnoteDefinition) {
if definition != nil {
if _, exists := d.Footnotes.Definitions[name]; exists {
d.Log.Printf("Footnote [fn:%s] redefined! %#v", name, definition)
}
d.Footnotes.Definitions[name] = definition
}
d.Footnotes.addOrder = append(d.Footnotes.addOrder, name)
}
func (d *Document) addHeadline(headline *Headline) int {
current := &Section{Headline: headline}
d.Outline.last.add(current)

View file

@ -4,12 +4,6 @@ import (
"regexp"
)
type Footnotes struct {
Title string
Definitions map[string]*FootnoteDefinition
addOrder []string
}
type FootnoteDefinition struct {
Name string
Children []Node
@ -35,24 +29,7 @@ func (d *Document) parseFootnoteDefinition(i int, parentStop stopFn) (int, Node)
}
consumed, nodes := d.parseMany(i, stop)
definition := FootnoteDefinition{name, nodes, false}
d.addFootnote(name, &definition)
return consumed, definition
}
func (fs *Footnotes) Ordered() []FootnoteDefinition {
m := map[string]bool{}
definitions, inlineDefinitions := []FootnoteDefinition{}, []FootnoteDefinition{}
for _, name := range fs.addOrder {
if isDuplicate := m[name]; !isDuplicate {
m[name] = true
if definition := *fs.Definitions[name]; definition.Inline {
inlineDefinitions = append(inlineDefinitions, definition)
} else {
definitions = append(definitions, definition)
}
}
}
return append(definitions, inlineDefinitions...)
}
func (n FootnoteDefinition) String() string { return orgWriter.nodesAsString(n) }

View file

@ -19,6 +19,12 @@ type HTMLWriter struct {
HighlightCodeBlock func(source, lang string) string
htmlEscape bool
log *log.Logger
footnotes footnotes
}
type footnotes struct {
mapping map[string]int
list []*FootnoteDefinition
}
var emphasisTags = map[string][]string{
@ -55,6 +61,9 @@ func NewHTMLWriter() *HTMLWriter {
HighlightCodeBlock: func(source, lang string) string {
return fmt.Sprintf("%s\n<pre>\n%s\n</pre>\n</div>", `<div class="highlight">`, html.EscapeString(source))
},
footnotes: footnotes{
mapping: map[string]int{},
},
}
}
@ -129,26 +138,23 @@ func (w *HTMLWriter) WriteInclude(i Include) {
WriteNodes(w, i.Resolve())
}
func (w *HTMLWriter) WriteFootnoteDefinition(FootnoteDefinition) {}
func (w *HTMLWriter) writeFootnoteDefinition(f FootnoteDefinition) {
n := f.Name
w.WriteString(`<div class="footnote-definition">` + "\n")
w.WriteString(fmt.Sprintf(`<sup id="footnote-%s"><a href="#footnote-reference-%s">%s</a></sup>`, n, n, n) + "\n")
w.WriteString(`<div class="footnote-body">` + "\n")
WriteNodes(w, f.Children...)
w.WriteString("</div>\n</div>\n")
func (w *HTMLWriter) WriteFootnoteDefinition(f FootnoteDefinition) {
w.footnotes.updateDefinition(f)
}
func (w *HTMLWriter) WriteFootnotes(d *Document) {
if !w.document.GetOption("f") || len(d.Footnotes.Definitions) == 0 {
if !w.document.GetOption("f") || len(w.footnotes.list) == 0 {
return
}
w.WriteString(`<div class="footnotes">` + "\n")
w.WriteString(`<hr class="footnotes-separatator">` + "\n")
w.WriteString(`<div class="footnote-definitions">` + "\n")
for _, definition := range d.Footnotes.Ordered() {
w.writeFootnoteDefinition(definition)
for i, definition := range w.footnotes.list {
w.WriteString(`<div class="footnote-definition">` + "\n")
w.WriteString(fmt.Sprintf(`<sup id="footnote-%d"><a href="#footnote-reference-%d">%d</a></sup>`, i, i, i) + "\n")
w.WriteString(`<div class="footnote-body">` + "\n")
WriteNodes(w, definition.Children...)
w.WriteString("</div>\n</div>\n")
}
w.WriteString("</div>\n</div>\n")
}
@ -245,8 +251,8 @@ func (w *HTMLWriter) WriteFootnoteLink(l FootnoteLink) {
if !w.document.GetOption("f") {
return
}
n := html.EscapeString(l.Name)
w.WriteString(fmt.Sprintf(`<sup class="footnote-reference"><a id="footnote-reference-%s" href="#footnote-%s">%s</a></sup>`, n, n, n))
id := w.footnotes.add(l)
w.WriteString(fmt.Sprintf(`<sup class="footnote-reference"><a id="footnote-reference-%d" href="#footnote-%d">%d</a></sup>`, id, id, id))
}
func (w *HTMLWriter) WriteTimestamp(t Timestamp) {
@ -445,3 +451,21 @@ func setHTMLAttribute(attributes []h.Attribute, k, v string) []h.Attribute {
}
return append(attributes, h.Attribute{Namespace: "", Key: k, Val: v})
}
func (fs *footnotes) add(f FootnoteLink) int {
if i, ok := fs.mapping[f.Name]; ok && f.Name != "" {
return i
}
fs.list = append(fs.list, f.Definition)
i := len(fs.list) - 1
if f.Name != "" {
fs.mapping[f.Name] = i
}
return i
}
func (fs *footnotes) updateDefinition(f FootnoteDefinition) {
if i, ok := fs.mapping[f.Name]; ok {
fs.list[i] = &f
}
}

View file

@ -171,7 +171,6 @@ func (d *Document) parseFootnoteReference(input string, start int) (int, Node) {
link := FootnoteLink{name, nil}
if definition != "" {
link.Definition = &FootnoteDefinition{name, []Node{Paragraph{d.parseInline(definition)}}, true}
d.addFootnote(name, link.Definition)
}
return len(m[0]), link
}

View file

@ -12,12 +12,12 @@ Using some footnotes
<ul>
<li>
<p>
normal footnote reference <sup class="footnote-reference"><a id="footnote-reference-1" href="#footnote-1">1</a></sup> <sup class="footnote-reference"><a id="footnote-reference-6" href="#footnote-6">6</a></sup>
normal footnote reference <sup class="footnote-reference"><a id="footnote-reference-0" href="#footnote-0">0</a></sup> <sup class="footnote-reference"><a id="footnote-reference-1" href="#footnote-1">1</a></sup>
</p>
</li>
<li>
<p>
further references to the same footnote should not <sup class="footnote-reference"><a id="footnote-reference-1" href="#footnote-1">1</a></sup> render duplicates in the footnote list
further references to the same footnote should not <sup class="footnote-reference"><a id="footnote-reference-0" href="#footnote-0">0</a></sup> render duplicates in the footnote list
</p>
</li>
<li>
@ -28,7 +28,7 @@ inline footnotes are also supported via <sup class="footnote-reference"><a id="f
<li>
<p>
Footnote definitions are not printed where they appear.
Rather, they are gathered and exported at the end of the document in the footnote section. <sup class="footnote-reference"><a id="footnote-reference-4" href="#footnote-4">4</a></sup>
Rather, they are gathered and exported at the end of the document in the footnote section. <sup class="footnote-reference"><a id="footnote-reference-3" href="#footnote-3">3</a></sup>
</p>
</li>
</ul>
@ -36,33 +36,16 @@ Rather, they are gathered and exported at the end of the document in the footnot
Footnotes
</h1>
<p>
Please note that the footnotes section is not automatically excluded from the export like in emacs. <sup class="footnote-reference"><a id="footnote-reference-7" href="#footnote-7">7</a></sup>
Please note that the footnotes section is not automatically excluded from the export like in emacs. <sup class="footnote-reference"><a id="footnote-reference-4" href="#footnote-4">4</a></sup>
</p>
<p>
this is not part of <sup class="footnote-reference"><a id="footnote-reference-7" href="#footnote-7">7</a></sup> anymore as there are 2 blank lines in between!
this is not part of <sup class="footnote-reference"><a id="footnote-reference-4" href="#footnote-4">4</a></sup> anymore as there are 2 blank lines in between!
</p>
<div class="footnotes">
<hr class="footnotes-separatator">
<div class="footnote-definitions">
<div class="footnote-definition">
<sup id="footnote-4"><a href="#footnote-reference-4">4</a></sup>
<div class="footnote-body">
<p>
so this definition will not be at the end of this section in the exported document.
Rather, it will be somewhere down below in the footnotes section.
</p>
</div>
</div>
<div class="footnote-definition">
<sup id="footnote-5"><a href="#footnote-reference-5">5</a></sup>
<div class="footnote-body">
<p>
another unused footnote (this definition overwrites the previous definition of <code class="verbatim">fn:5</code>)
</p>
</div>
</div>
<div class="footnote-definition">
<sup id="footnote-1"><a href="#footnote-reference-1">1</a></sup>
<sup id="footnote-0"><a href="#footnote-reference-0">0</a></sup>
<div class="footnote-body">
<p>
<a href="https://www.example.com">https://www.example.com</a>
@ -115,15 +98,7 @@ and tables
</div>
</div>
<div class="footnote-definition">
<sup id="footnote-3"><a href="#footnote-reference-3">3</a></sup>
<div class="footnote-body">
<p>
<a href="http://example.com/unused-footnote">example.com/unused-footnote</a>
</p>
</div>
</div>
<div class="footnote-definition">
<sup id="footnote-6"><a href="#footnote-reference-6">6</a></sup>
<sup id="footnote-1"><a href="#footnote-reference-1">1</a></sup>
<div class="footnote-body">
<p>
Footnotes break after two consecutive empty lines - just like paragraphs - see <a href="https://orgmode.org/worg/dev/org-syntax.html.">https://orgmode.org/worg/dev/org-syntax.html.</a>
@ -132,15 +107,6 @@ This shouldn&#39;t happen when the definition line and the line after that are e
</div>
</div>
<div class="footnote-definition">
<sup id="footnote-7"><a href="#footnote-reference-7">7</a></sup>
<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>
</div>
</div>
<div class="footnote-definition">
<sup id="footnote-2"><a href="#footnote-reference-2">2</a></sup>
<div class="footnote-body">
<p>
@ -148,5 +114,23 @@ the inline footnote definition
</p>
</div>
</div>
<div class="footnote-definition">
<sup id="footnote-3"><a href="#footnote-reference-3">3</a></sup>
<div class="footnote-body">
<p>
so this definition will not be at the end of this section in the exported document.
Rather, it will be somewhere down below in the footnotes section.
</p>
</div>
</div>
<div class="footnote-definition">
<sup id="footnote-4"><a href="#footnote-reference-4">4</a></sup>
<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>
</div>
</div>
</div>
</div>

View file

@ -409,7 +409,7 @@ src/example/export blocks should not be converted!
<a href="https://github.com/chaseadamsio/goorgeous/issues/87">#87</a>: Markup in footnotes is rendered literally
</h3>
<p>
footnotes can contain <strong>markup</strong> - and other elements and stuff <sup class="footnote-reference"><a id="footnote-reference-2" href="#footnote-2">2</a></sup>
footnotes can contain <strong>markup</strong> - and other elements and stuff <sup class="footnote-reference"><a id="footnote-reference-0" href="#footnote-0">0</a></sup> <sup class="footnote-reference"><a id="footnote-reference-1" href="#footnote-1">1</a></sup>
</p>
<h3 id="headline-23">
<span class="todo">DONE</span>
@ -472,7 +472,7 @@ Footnotes
<hr class="footnotes-separatator">
<div class="footnote-definitions">
<div class="footnote-definition">
<sup id="footnote-1"><a href="#footnote-reference-1">1</a></sup>
<sup id="footnote-0"><a href="#footnote-reference-0">0</a></sup>
<div class="footnote-body">
<p>
a footnote <em>with</em> <strong>markup</strong>
@ -492,7 +492,7 @@ because that&#39;s possible
</div>
</div>
<div class="footnote-definition">
<sup id="footnote-2"><a href="#footnote-reference-2">2</a></sup>
<sup id="footnote-1"><a href="#footnote-reference-1">1</a></sup>
<div class="footnote-body">
<p>
that also goes for <strong>inline</strong> footnote <em>definitions</em>

View file

@ -108,7 +108,7 @@ also, consecutive dashes inside
: --, ---
*** DONE [[https://github.com/chaseadamsio/goorgeous/issues/87][#87]]: Markup in footnotes is rendered literally
footnotes can contain *markup* - and other elements and stuff [fn:2:that also goes for *inline* footnote /definitions/]
footnotes can contain *markup* - and other elements and stuff [fn:1] [fn:2:that also goes for *inline* footnote /definitions/]
*** DONE [[https://github.com/chaseadamsio/goorgeous/issues/92][#92]]: src blocks only render in caps
The behaviour of Org mode =<s TAB= changed and it now inserts lowercased src blocks (go-org already handled this one)
- lowercased:

View file

@ -108,7 +108,7 @@ also, consecutive dashes inside
: --, ---
*** DONE [[https://github.com/chaseadamsio/goorgeous/issues/87][#87]]: Markup in footnotes is rendered literally
footnotes can contain *markup* - and other elements and stuff [fn:2:that also goes for *inline* footnote /definitions/]
footnotes can contain *markup* - and other elements and stuff [fn:1] [fn:2:that also goes for *inline* footnote /definitions/]
*** DONE [[https://github.com/chaseadamsio/goorgeous/issues/92][#92]]: src blocks only render in caps
The behaviour of Org mode =<s TAB= changed and it now inserts lowercased src blocks (go-org already handled this one)
- lowercased: