Add support for ": example" elements

This commit is contained in:
Niklas Fasching 2018-12-16 23:21:26 +01:00
parent 24ace5aa0e
commit c012b0a533
8 changed files with 75 additions and 0 deletions

View file

@ -49,6 +49,7 @@ var lexFns = []lexFn{
lexHorizontalRule, lexHorizontalRule,
lexKeywordOrComment, lexKeywordOrComment,
lexFootnoteDefinition, lexFootnoteDefinition,
lexExample,
lexText, lexText,
} }
@ -169,6 +170,8 @@ func (d *Document) parseOne(i int, stop stopFn) (consumed int, node Node) {
consumed, node = d.parseBlock(i, stop) consumed, node = d.parseBlock(i, stop)
case "text": case "text":
consumed, node = d.parseParagraph(i, stop) consumed, node = d.parseParagraph(i, stop)
case "example":
consumed, node = d.parseExample(i, stop)
case "horizontalRule": case "horizontalRule":
consumed, node = d.parseHorizontalRule(i, stop) consumed, node = d.parseHorizontalRule(i, stop)
case "comment": case "comment":

26
org/example.go Normal file
View file

@ -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
}

View file

@ -88,6 +88,8 @@ func (w *HTMLWriter) writeNodes(ns ...Node) {
case Paragraph: case Paragraph:
w.writeParagraph(n) w.writeParagraph(n)
case Example:
w.writeExample(n)
case HorizontalRule: case HorizontalRule:
w.writeHorizontalRule(n) w.writeHorizontalRule(n)
case Text: case Text:
@ -264,6 +266,17 @@ func (w *HTMLWriter) writeParagraph(p Paragraph) {
w.WriteString("\n</p>\n") w.WriteString("\n</p>\n")
} }
func (w *HTMLWriter) writeExample(e Example) {
w.WriteString(`<pre class="example">` + "\n")
if len(e.Children) != 0 {
for _, n := range e.Children {
w.writeNodes(n)
w.WriteString("\n")
}
}
w.WriteString("\n</pre>\n")
}
func (w *HTMLWriter) writeHorizontalRule(h HorizontalRule) { func (w *HTMLWriter) writeHorizontalRule(h HorizontalRule) {
w.WriteString("<hr>\n") w.WriteString("<hr>\n")
} }

View file

@ -76,6 +76,8 @@ func (w *OrgWriter) writeNodes(ns ...Node) {
case Paragraph: case Paragraph:
w.writeParagraph(n) w.writeParagraph(n)
case Example:
w.writeExample(n)
case HorizontalRule: case HorizontalRule:
w.writeHorizontalRule(n) w.writeHorizontalRule(n)
case Text: case Text:
@ -171,6 +173,14 @@ func (w *OrgWriter) writeParagraph(p Paragraph) {
w.WriteString("\n") 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) { func (w *OrgWriter) writeKeyword(k Keyword) {
w.WriteString(w.indent + fmt.Sprintf("#+%s: %s\n", k.Key, k.Value)) w.WriteString(w.indent + fmt.Sprintf("#+%s: %s\n", k.Key, k.Value))
} }

View file

@ -27,6 +27,14 @@ multiple lines including
empty lines! empty lines!
it also has multiple parameters it also has multiple parameters
note that /inline/ *markup* ignored
</pre>
<pre class="example">
examples like this
are also supported
note that /inline/ *markup* ignored
</pre> </pre>
<blockquote> <blockquote>
<p> <p>

View file

@ -21,8 +21,14 @@ multiple lines including
empty lines! empty lines!
it also has multiple parameters it also has multiple parameters
note that /inline/ *markup* ignored
#+END_EXAMPLE #+END_EXAMPLE
: examples like this
: are also supported
: note that /inline/ *markup* ignored
#+BEGIN_QUOTE #+BEGIN_QUOTE
Mongodb is *webscale*. (source: [[http://www.mongodb-is-web-scale.com/][mongodb-is-web-scale]]) Mongodb is *webscale*. (source: [[http://www.mongodb-is-web-scale.com/][mongodb-is-web-scale]])

View file

@ -84,5 +84,11 @@ and text with an empty line in between as well!
<p> <p>
unordered list item 4 unordered list item 4
</p> </p>
<pre class="example">
with an example
that spans multiple lines
</pre>
</li> </li>
</ul> </ul>

View file

@ -18,3 +18,6 @@
and text with an empty line in between as well! and text with an empty line in between as well!
- unordered list item 4 - unordered list item 4
: with an example
:
: that spans multiple lines