Fix footnotes starting with empty line

This commit is contained in:
Niklas Fasching 2018-12-03 00:35:12 +01:00
parent 3c2e9ed204
commit d5665fb21c
5 changed files with 26 additions and 11 deletions

View file

@ -17,7 +17,7 @@ type FootnoteDefinition struct {
Inline bool
}
var footnoteDefinitionRegexp = regexp.MustCompile(`^\[fn:([\w-]+)\]\s+(.+)`)
var footnoteDefinitionRegexp = regexp.MustCompile(`^\[fn:([\w-]+)\](\s+(.+)|$)`)
func lexFootnoteDefinition(line string) (token, bool) {
if m := footnoteDefinitionRegexp.FindStringSubmatch(line); m != nil {
@ -27,10 +27,11 @@ func lexFootnoteDefinition(line string) (token, bool) {
}
func (d *Document) parseFootnoteDefinition(i int, parentStop stopFn) (int, Node) {
name := d.tokens[i].content
start, name := i, d.tokens[i].content
d.tokens[i] = tokenize(d.tokens[i].matches[2])
stop := func(d *Document, i int) bool {
return parentStop(d, i) || isSecondBlankLine(d, i) ||
return parentStop(d, i) ||
(isSecondBlankLine(d, i) && i > start+1) ||
d.tokens[i].kind == "headline" || d.tokens[i].kind == "footnoteDefinition"
}
consumed, nodes := d.parseMany(i, stop)

View file

@ -118,8 +118,8 @@ func (d *Document) parseFootnoteReference(input string, start int) (int, Node) {
if definition != "" {
paragraph := Paragraph{[]Node{Line{d.parseInline(definition)}}}
link.Definition = &FootnoteDefinition{name, []Node{paragraph}, true}
}
d.Footnotes.add(name, link.Definition)
}
return len(m[0]), link
}
return 0, nil

View file

@ -148,8 +148,18 @@ func (w *OrgWriter) writeFootnotes(d *Document) {
}
}
func isEmptyLineParagraph(n Node) bool {
if p, _ := n.(Paragraph); len(p.Children) == 1 {
return len(p.Children[0].(Line).Children) == 0
}
return false
}
func (w *OrgWriter) writeFootnoteDefinition(f FootnoteDefinition) {
w.WriteString(fmt.Sprintf("[fn:%s]", f.Name))
if !(len(f.Children) >= 1 && isEmptyLineParagraph(f.Children[0])) {
w.WriteString(" ")
}
w.writeNodes(f.Children...)
}

View file

@ -199,7 +199,7 @@ or <code>that</code> foo.
</p>
<h3><a href="https://github.com/chaseadamsio/goorgeous/issues/68">#68</a>: Quote block with inline markup</h3>
<blockquote>
<a href="www.example.com"><em>this</em> <strong>is</strong> <span style="text-decoration: underline;">markup</span>!</a>
<a href="https://www.example.com"><em>this</em> <strong>is</strong> <span style="text-decoration: underline;">markup</span>!</a>
</blockquote>
<h3><a href="https://github.com/chaseadamsio/goorgeous/issues/77">#77</a>: Recognize <code class="verbatim">code</code>--- as code plus dash</h3>
<h3><a href="https://github.com/chaseadamsio/goorgeous/issues/75">#75</a>: Not parsing nested lists correctly</h3>
@ -243,7 +243,7 @@ Bar paragraph
<ul>
<li>
<p>
normal footnote reference <sup class="footnote-reference"><a href="#footnote-1">1</a></sup>
normal footnote reference <sup class="footnote-reference"><a href="#footnote-1">1</a></sup> <sup class="footnote-reference"><a href="#footnote-6">6</a></sup>
</p>
</li>
<li>
@ -327,7 +327,8 @@ another unused footnote
<div class="footnote-definition">
<sup id="footnote-6">6</sup>
<p>
another unused footnote
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>
This shouldn&#39;t happen when the definition line and the line after that are empty.
</p>
</div>
<div class="footnote-definition">

View file

@ -89,7 +89,7 @@ either ~this~
or ~that~ foo.
*** DONE [[https://github.com/chaseadamsio/goorgeous/issues/68][#68]]: Quote block with inline markup
#+BEGIN_QUOTE
[[www.example.com][/this/ *is* _markup_!]]
[[https://www.example.com][/this/ *is* _markup_!]]
#+END_QUOTE
*** DONE [[https://github.com/chaseadamsio/goorgeous/issues/77][#77]]: Recognize =code=--- as code plus dash
@ -111,7 +111,7 @@ Foo paragraph.
**** Bar
Bar paragraph
** Footnotes
- normal footnote reference [fn:1]
- normal footnote reference [fn:1] [fn:6]
- further references to the same footnote should not [fn:1] render duplicates in the footnote list
- inline footnotes are also supported via [fn:2:the inline footnote definition].
@ -134,4 +134,7 @@ Bar paragraph
[fn:5] another unused footnote
[fn:6] another unused footnote
[fn:6]
Footnotes break after two consecutive empty lines - just like paragraphs - see https://orgmode.org/worg/dev/org-syntax.html.
This shouldn't happen when the definition line and the line after that are empty.