Implement result blocks
This commit is contained in:
parent
232c27327c
commit
f6f4646d45
8 changed files with 66 additions and 0 deletions
18
org/block.go
18
org/block.go
|
@ -12,6 +12,10 @@ type Block struct {
|
|||
Children []Node
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Node Node
|
||||
}
|
||||
|
||||
type Example struct {
|
||||
Children []Node
|
||||
}
|
||||
|
@ -19,6 +23,7 @@ type Example struct {
|
|||
var exampleLineRegexp = regexp.MustCompile(`^(\s*):(\s(.*)|\s*$)`)
|
||||
var beginBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+BEGIN_(\w+)(.*)`)
|
||||
var endBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+END_(\w+)`)
|
||||
var resultRegexp = regexp.MustCompile(`(?i)^(\s*)#\+RESULTS:`)
|
||||
var exampleBlockEscapeRegexp = regexp.MustCompile(`(^|\n)([ \t]*),([ \t]*)(\*|,\*|#\+|,#\+)`)
|
||||
|
||||
func lexBlock(line string) (token, bool) {
|
||||
|
@ -30,6 +35,13 @@ func lexBlock(line string) (token, bool) {
|
|||
return nilToken, false
|
||||
}
|
||||
|
||||
func lexResult(line string) (token, bool) {
|
||||
if m := resultRegexp.FindStringSubmatch(line); m != nil {
|
||||
return token{"result", len(m[1]), "", m}, true
|
||||
}
|
||||
return nilToken, false
|
||||
}
|
||||
|
||||
func lexExample(line string) (token, bool) {
|
||||
if m := exampleLineRegexp.FindStringSubmatch(line); m != nil {
|
||||
return token{"example", len(m[1]), m[3], m}, true
|
||||
|
@ -75,6 +87,11 @@ func (d *Document) parseExample(i int, parentStop stopFn) (int, Node) {
|
|||
return i - start, example
|
||||
}
|
||||
|
||||
func (d *Document) parseResult(i int, parentStop stopFn) (int, Node) {
|
||||
consumed, node := d.parseOne(i+1, parentStop)
|
||||
return consumed + 1, Result{node}
|
||||
}
|
||||
|
||||
func trimIndentUpTo(max int) func(string) string {
|
||||
return func(line string) string {
|
||||
i := 0
|
||||
|
@ -86,3 +103,4 @@ func trimIndentUpTo(max int) func(string) string {
|
|||
|
||||
func (n Example) String() string { return orgWriter.WriteNodesAsString(n) }
|
||||
func (n Block) String() string { return orgWriter.WriteNodesAsString(n) }
|
||||
func (n Result) String() string { return orgWriter.WriteNodesAsString(n) }
|
||||
|
|
|
@ -63,6 +63,7 @@ var lexFns = []lexFn{
|
|||
lexHeadline,
|
||||
lexDrawer,
|
||||
lexBlock,
|
||||
lexResult,
|
||||
lexList,
|
||||
lexTable,
|
||||
lexHorizontalRule,
|
||||
|
@ -202,6 +203,8 @@ func (d *Document) parseOne(i int, stop stopFn) (consumed int, node Node) {
|
|||
consumed, node = d.parseTable(i, stop)
|
||||
case "beginBlock":
|
||||
consumed, node = d.parseBlock(i, stop)
|
||||
case "result":
|
||||
consumed, node = d.parseResult(i, stop)
|
||||
case "beginDrawer":
|
||||
consumed, node = d.parseDrawer(i, stop)
|
||||
case "text":
|
||||
|
|
|
@ -136,6 +136,8 @@ func (w *HTMLWriter) WriteBlock(b Block) {
|
|||
}
|
||||
}
|
||||
|
||||
func (w *HTMLWriter) WriteResult(r Result) { WriteNodes(w, r.Node) }
|
||||
|
||||
func (w *HTMLWriter) WriteInlineBlock(b InlineBlock) {
|
||||
content := w.blockContent(strings.ToUpper(b.Name), b.Children)
|
||||
switch b.Name {
|
||||
|
|
|
@ -104,6 +104,11 @@ func (w *OrgWriter) WriteBlock(b Block) {
|
|||
w.WriteString("#+END_" + b.Name + "\n")
|
||||
}
|
||||
|
||||
func (w *OrgWriter) WriteResult(r Result) {
|
||||
w.WriteString("#+RESULTS:\n")
|
||||
WriteNodes(w, r.Node)
|
||||
}
|
||||
|
||||
func (w *OrgWriter) WriteInlineBlock(b InlineBlock) {
|
||||
switch b.Name {
|
||||
case "src":
|
||||
|
|
13
org/testdata/blocks.html
vendored
13
org/testdata/blocks.html
vendored
|
@ -1,3 +1,6 @@
|
|||
<pre class="example">
|
||||
some results without a block
|
||||
</pre>
|
||||
<figure>
|
||||
<div class="src src-bash">
|
||||
<div class="highlight">
|
||||
|
@ -23,6 +26,16 @@ a source block without a language
|
|||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="src src-bash">
|
||||
<div class="highlight">
|
||||
<pre>
|
||||
echo a source block with results
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
<pre class="example">
|
||||
a source block with results
|
||||
</pre>
|
||||
<pre class="example">
|
||||
an example block with
|
||||
multiple lines including
|
||||
|
|
11
org/testdata/blocks.org
vendored
11
org/testdata/blocks.org
vendored
|
@ -1,3 +1,6 @@
|
|||
#+RESULTS:
|
||||
: some results without a block
|
||||
|
||||
#+CAPTION: block caption
|
||||
#+BEGIN_SRC bash :results raw
|
||||
echo "a bash source block"
|
||||
|
@ -13,6 +16,14 @@ hello
|
|||
a source block without a language
|
||||
#+END_SRC
|
||||
|
||||
|
||||
#+BEGIN_SRC bash
|
||||
echo a source block with results
|
||||
#+END_SRC
|
||||
|
||||
#+RESULTS:
|
||||
: a source block with results
|
||||
|
||||
#+BEGIN_EXAMPLE foo bar baz
|
||||
an example block with
|
||||
multiple lines including
|
||||
|
|
11
org/testdata/blocks.pretty_org
vendored
11
org/testdata/blocks.pretty_org
vendored
|
@ -1,3 +1,6 @@
|
|||
#+RESULTS:
|
||||
: some results without a block
|
||||
|
||||
#+CAPTION: block caption
|
||||
#+BEGIN_SRC bash :results raw
|
||||
echo "a bash source block"
|
||||
|
@ -13,6 +16,14 @@ hello
|
|||
a source block without a language
|
||||
#+END_SRC
|
||||
|
||||
|
||||
#+BEGIN_SRC bash
|
||||
echo a source block with results
|
||||
#+END_SRC
|
||||
|
||||
#+RESULTS:
|
||||
: a source block with results
|
||||
|
||||
#+BEGIN_EXAMPLE foo bar baz
|
||||
an example block with
|
||||
multiple lines including
|
||||
|
|
|
@ -18,6 +18,7 @@ type Writer interface {
|
|||
WriteNodeWithName(NodeWithName)
|
||||
WriteHeadline(Headline)
|
||||
WriteBlock(Block)
|
||||
WriteResult(Result)
|
||||
WriteInlineBlock(InlineBlock)
|
||||
WriteExample(Example)
|
||||
WriteDrawer(Drawer)
|
||||
|
@ -58,6 +59,8 @@ func WriteNodes(w Writer, nodes ...Node) {
|
|||
w.WriteHeadline(n)
|
||||
case Block:
|
||||
w.WriteBlock(n)
|
||||
case Result:
|
||||
w.WriteResult(n)
|
||||
case InlineBlock:
|
||||
w.WriteInlineBlock(n)
|
||||
case Example:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue