diff --git a/org/footnote.go b/org/footnote.go index 5bea596..a4007b0 100644 --- a/org/footnote.go +++ b/org/footnote.go @@ -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) diff --git a/org/inline.go b/org/inline.go index 1248c44..df7bade 100644 --- a/org/inline.go +++ b/org/inline.go @@ -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) } - d.Footnotes.add(name, link.Definition) return len(m[0]), link } return 0, nil diff --git a/org/org.go b/org/org.go index b28b176..1b3511d 100644 --- a/org/org.go +++ b/org/org.go @@ -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)) + w.WriteString(fmt.Sprintf("[fn:%s]", f.Name)) + if !(len(f.Children) >= 1 && isEmptyLineParagraph(f.Children[0])) { + w.WriteString(" ") + } w.writeNodes(f.Children...) } diff --git a/org/testdata/example.html b/org/testdata/example.html index a332be9..ea6c340 100644 --- a/org/testdata/example.html +++ b/org/testdata/example.html @@ -199,7 +199,7 @@ or that foo.

#68: Quote block with inline markup

-this is markup! +this is markup!

#77: Recognize code--- as code plus dash

#75: Not parsing nested lists correctly

@@ -243,7 +243,7 @@ Bar paragraph