From ed8764940fb3b21e8da92d18377fdb1b21e9c0d8 Mon Sep 17 00:00:00 2001
From: Niklas Fasching
Date: Mon, 3 Dec 2018 01:42:31 +0100
Subject: [PATCH] Fix paragraphs: Empty lines separate paragraphs
Somehow i thought it was 2 empty lines rather than 1 - makes more sense this
way... :D
---
org/org.go | 7 -------
org/paragraph.go | 22 ++++------------------
org/testdata/example.html | 10 ++++++++++
org/testdata/example.org | 8 ++++++++
org/util.go | 19 +++++++++++++++++++
5 files changed, 41 insertions(+), 25 deletions(-)
create mode 100644 org/util.go
diff --git a/org/org.go b/org/org.go
index 1b3511d..b2aedea 100644
--- a/org/org.go
+++ b/org/org.go
@@ -148,13 +148,6 @@ 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])) {
diff --git a/org/paragraph.go b/org/paragraph.go
index 9d8c289..5f94010 100644
--- a/org/paragraph.go
+++ b/org/paragraph.go
@@ -25,27 +25,13 @@ func lexHorizontalRule(line string) (token, bool) {
return nilToken, false
}
-func isSecondBlankLine(d *Document, i int) bool {
- if i-1 <= 0 {
- return false
- }
- t1, t2 := d.tokens[i-1], d.tokens[i]
- if t1.kind == "text" && t2.kind == "text" && t1.content == "" && t2.content == "" {
- return true
- }
- return false
-}
-
func (d *Document) parseParagraph(i int, parentStop stopFn) (int, Node) {
lines, start := []Node{Line{d.parseInline(d.tokens[i].content)}}, i
i++
- stop := func(d *Document, i int) bool { return parentStop(d, i) || d.tokens[i].kind != "text" }
- for ; !stop(d, i) && !isSecondBlankLine(d, i); i++ {
- if isSecondBlankLine(d, i) {
- lines = lines[:len(lines)-1]
- i++
- break
- }
+ stop := func(d *Document, i int) bool {
+ return parentStop(d, i) || d.tokens[i].kind != "text" || d.tokens[i].content == ""
+ }
+ for ; !stop(d, i); i++ {
lines = append(lines, Line{d.parseInline(d.tokens[i].content)})
}
consumed := i - start
diff --git a/org/testdata/example.html b/org/testdata/example.html
index 26a4df6..ce63503 100644
--- a/org/testdata/example.html
+++ b/org/testdata/example.html
@@ -5,6 +5,16 @@ works we can be kind of sure that the parsing worked.
At least I hope so - I would like to get around writing tests for the individual parsing
functions...
+Paragraphs
+
+Empty lines separate paragraphs.
+
+
+Right?
+
+
+They do!
+
Headlines with TODO status, priority & tags
Headline with todo status & priority
Headline with TODO status
diff --git a/org/testdata/example.org b/org/testdata/example.org
index 4f17d1a..5ae2b30 100644
--- a/org/testdata/example.org
+++ b/org/testdata/example.org
@@ -9,6 +9,14 @@ works we can be kind of sure that the parsing worked.
At least I hope so - I would like to get around writing tests for the individual parsing
functions...
+** Paragraphs
+
+Empty lines separate paragraphs.
+
+Right?
+
+They do!
+
** Headlines with TODO status, priority & tags
*** TODO [#B] Headline with todo status & priority
*** DONE Headline with TODO status
diff --git a/org/util.go b/org/util.go
new file mode 100644
index 0000000..0ee6a97
--- /dev/null
+++ b/org/util.go
@@ -0,0 +1,19 @@
+package org
+
+func isSecondBlankLine(d *Document, i int) bool {
+ if i-1 <= 0 {
+ return false
+ }
+ t1, t2 := d.tokens[i-1], d.tokens[i]
+ if t1.kind == "text" && t2.kind == "text" && t1.content == "" && t2.content == "" {
+ return true
+ }
+ return false
+}
+
+func isEmptyLineParagraph(n Node) bool {
+ if p, _ := n.(Paragraph); len(p.Children) == 1 {
+ return len(p.Children[0].(Line).Children) == 0
+ }
+ return false
+}