From c012b0a5338059d884eaf65f6d80c4f31143cf0a Mon Sep 17 00:00:00 2001
From: Niklas Fasching
Date: Sun, 16 Dec 2018 23:21:26 +0100
Subject: [PATCH] Add support for ": example" elements
---
org/document.go | 3 +++
org/example.go | 26 ++++++++++++++++++++++++++
org/html.go | 13 +++++++++++++
org/org.go | 10 ++++++++++
org/testdata/blocks.html | 8 ++++++++
org/testdata/blocks.org | 6 ++++++
org/testdata/lists.html | 6 ++++++
org/testdata/lists.org | 3 +++
8 files changed, 75 insertions(+)
create mode 100644 org/example.go
diff --git a/org/document.go b/org/document.go
index 6a62d79..ffe56de 100644
--- a/org/document.go
+++ b/org/document.go
@@ -49,6 +49,7 @@ var lexFns = []lexFn{
lexHorizontalRule,
lexKeywordOrComment,
lexFootnoteDefinition,
+ lexExample,
lexText,
}
@@ -169,6 +170,8 @@ func (d *Document) parseOne(i int, stop stopFn) (consumed int, node Node) {
consumed, node = d.parseBlock(i, stop)
case "text":
consumed, node = d.parseParagraph(i, stop)
+ case "example":
+ consumed, node = d.parseExample(i, stop)
case "horizontalRule":
consumed, node = d.parseHorizontalRule(i, stop)
case "comment":
diff --git a/org/example.go b/org/example.go
new file mode 100644
index 0000000..9d716f3
--- /dev/null
+++ b/org/example.go
@@ -0,0 +1,26 @@
+package org
+
+import (
+ "regexp"
+)
+
+type Example struct {
+ Children []Node
+}
+
+var exampleLineRegexp = regexp.MustCompile(`^(\s*): (.*)`)
+
+func lexExample(line string) (token, bool) {
+ if m := exampleLineRegexp.FindStringSubmatch(line); m != nil {
+ return token{"example", len(m[1]), m[2], m}, true
+ }
+ return nilToken, false
+}
+
+func (d *Document) parseExample(i int, parentStop stopFn) (int, Node) {
+ example, start := Example{}, i
+ for ; !parentStop(d, i) && d.tokens[i].kind == "example"; i++ {
+ example.Children = append(example.Children, Text{d.tokens[i].content})
+ }
+ return i - start, example
+}
diff --git a/org/html.go b/org/html.go
index 6f60db0..5b88d20 100644
--- a/org/html.go
+++ b/org/html.go
@@ -88,6 +88,8 @@ func (w *HTMLWriter) writeNodes(ns ...Node) {
case Paragraph:
w.writeParagraph(n)
+ case Example:
+ w.writeExample(n)
case HorizontalRule:
w.writeHorizontalRule(n)
case Text:
@@ -264,6 +266,17 @@ func (w *HTMLWriter) writeParagraph(p Paragraph) {
w.WriteString("\n
\n")
}
+func (w *HTMLWriter) writeExample(e Example) {
+ w.WriteString(`` + "\n")
+ if len(e.Children) != 0 {
+ for _, n := range e.Children {
+ w.writeNodes(n)
+ w.WriteString("\n")
+ }
+ }
+ w.WriteString("\n
\n")
+}
+
func (w *HTMLWriter) writeHorizontalRule(h HorizontalRule) {
w.WriteString("
\n")
}
diff --git a/org/org.go b/org/org.go
index df99a93..ab8306f 100644
--- a/org/org.go
+++ b/org/org.go
@@ -76,6 +76,8 @@ func (w *OrgWriter) writeNodes(ns ...Node) {
case Paragraph:
w.writeParagraph(n)
+ case Example:
+ w.writeExample(n)
case HorizontalRule:
w.writeHorizontalRule(n)
case Text:
@@ -171,6 +173,14 @@ func (w *OrgWriter) writeParagraph(p Paragraph) {
w.WriteString("\n")
}
+func (w *OrgWriter) writeExample(e Example) {
+ for _, n := range e.Children {
+ w.WriteString(w.indent + ":" + " ")
+ w.writeNodes(n)
+ w.WriteString("\n")
+ }
+}
+
func (w *OrgWriter) writeKeyword(k Keyword) {
w.WriteString(w.indent + fmt.Sprintf("#+%s: %s\n", k.Key, k.Value))
}
diff --git a/org/testdata/blocks.html b/org/testdata/blocks.html
index b90c498..5160801 100644
--- a/org/testdata/blocks.html
+++ b/org/testdata/blocks.html
@@ -27,6 +27,14 @@ multiple lines including
empty lines!
it also has multiple parameters
+
+note that /inline/ *markup* ignored
+
+
+examples like this
+are also supported
+note that /inline/ *markup* ignored
+
diff --git a/org/testdata/blocks.org b/org/testdata/blocks.org
index bbb4390..1ed1c7e 100644
--- a/org/testdata/blocks.org
+++ b/org/testdata/blocks.org
@@ -21,8 +21,14 @@ multiple lines including
empty lines!
it also has multiple parameters
+
+note that /inline/ *markup* ignored
#+END_EXAMPLE
+: examples like this
+: are also supported
+: note that /inline/ *markup* ignored
+
#+BEGIN_QUOTE
Mongodb is *webscale*. (source: [[http://www.mongodb-is-web-scale.com/][mongodb-is-web-scale]])
diff --git a/org/testdata/lists.html b/org/testdata/lists.html
index bae58c4..fdc0d24 100644
--- a/org/testdata/lists.html
+++ b/org/testdata/lists.html
@@ -84,5 +84,11 @@ and text with an empty line in between as well!
unordered list item 4
+
+with an example
+
+that spans multiple lines
+
+
diff --git a/org/testdata/lists.org b/org/testdata/lists.org
index 54211db..8c16479 100644
--- a/org/testdata/lists.org
+++ b/org/testdata/lists.org
@@ -18,3 +18,6 @@
and text with an empty line in between as well!
- unordered list item 4
+ : with an example
+ :
+ : that spans multiple lines