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("
+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