Implement result blocks

This commit is contained in:
Niklas Fasching 2020-04-16 17:28:49 +02:00
parent 232c27327c
commit f6f4646d45
8 changed files with 66 additions and 0 deletions

View file

@ -12,6 +12,10 @@ type Block struct {
Children []Node Children []Node
} }
type Result struct {
Node Node
}
type Example struct { type Example struct {
Children []Node Children []Node
} }
@ -19,6 +23,7 @@ type Example struct {
var exampleLineRegexp = regexp.MustCompile(`^(\s*):(\s(.*)|\s*$)`) var exampleLineRegexp = regexp.MustCompile(`^(\s*):(\s(.*)|\s*$)`)
var beginBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+BEGIN_(\w+)(.*)`) var beginBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+BEGIN_(\w+)(.*)`)
var endBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+END_(\w+)`) var endBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+END_(\w+)`)
var resultRegexp = regexp.MustCompile(`(?i)^(\s*)#\+RESULTS:`)
var exampleBlockEscapeRegexp = regexp.MustCompile(`(^|\n)([ \t]*),([ \t]*)(\*|,\*|#\+|,#\+)`) var exampleBlockEscapeRegexp = regexp.MustCompile(`(^|\n)([ \t]*),([ \t]*)(\*|,\*|#\+|,#\+)`)
func lexBlock(line string) (token, bool) { func lexBlock(line string) (token, bool) {
@ -30,6 +35,13 @@ func lexBlock(line string) (token, bool) {
return nilToken, false 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) { func lexExample(line string) (token, bool) {
if m := exampleLineRegexp.FindStringSubmatch(line); m != nil { if m := exampleLineRegexp.FindStringSubmatch(line); m != nil {
return token{"example", len(m[1]), m[3], m}, true 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 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 { func trimIndentUpTo(max int) func(string) string {
return func(line string) string { return func(line string) string {
i := 0 i := 0
@ -86,3 +103,4 @@ func trimIndentUpTo(max int) func(string) string {
func (n Example) String() string { return orgWriter.WriteNodesAsString(n) } func (n Example) String() string { return orgWriter.WriteNodesAsString(n) }
func (n Block) String() string { return orgWriter.WriteNodesAsString(n) } func (n Block) String() string { return orgWriter.WriteNodesAsString(n) }
func (n Result) String() string { return orgWriter.WriteNodesAsString(n) }

View file

@ -63,6 +63,7 @@ var lexFns = []lexFn{
lexHeadline, lexHeadline,
lexDrawer, lexDrawer,
lexBlock, lexBlock,
lexResult,
lexList, lexList,
lexTable, lexTable,
lexHorizontalRule, lexHorizontalRule,
@ -202,6 +203,8 @@ func (d *Document) parseOne(i int, stop stopFn) (consumed int, node Node) {
consumed, node = d.parseTable(i, stop) consumed, node = d.parseTable(i, stop)
case "beginBlock": case "beginBlock":
consumed, node = d.parseBlock(i, stop) consumed, node = d.parseBlock(i, stop)
case "result":
consumed, node = d.parseResult(i, stop)
case "beginDrawer": case "beginDrawer":
consumed, node = d.parseDrawer(i, stop) consumed, node = d.parseDrawer(i, stop)
case "text": case "text":

View file

@ -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) { func (w *HTMLWriter) WriteInlineBlock(b InlineBlock) {
content := w.blockContent(strings.ToUpper(b.Name), b.Children) content := w.blockContent(strings.ToUpper(b.Name), b.Children)
switch b.Name { switch b.Name {

View file

@ -104,6 +104,11 @@ func (w *OrgWriter) WriteBlock(b Block) {
w.WriteString("#+END_" + b.Name + "\n") 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) { func (w *OrgWriter) WriteInlineBlock(b InlineBlock) {
switch b.Name { switch b.Name {
case "src": case "src":

View file

@ -1,3 +1,6 @@
<pre class="example">
some results without a block
</pre>
<figure> <figure>
<div class="src src-bash"> <div class="src src-bash">
<div class="highlight"> <div class="highlight">
@ -23,6 +26,16 @@ a source block without a language
</pre> </pre>
</div> </div>
</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"> <pre class="example">
an example block with an example block with
multiple lines including multiple lines including

View file

@ -1,3 +1,6 @@
#+RESULTS:
: some results without a block
#+CAPTION: block caption #+CAPTION: block caption
#+BEGIN_SRC bash :results raw #+BEGIN_SRC bash :results raw
echo "a bash source block" echo "a bash source block"
@ -13,6 +16,14 @@ hello
a source block without a language a source block without a language
#+END_SRC #+END_SRC
#+BEGIN_SRC bash
echo a source block with results
#+END_SRC
#+RESULTS:
: a source block with results
#+BEGIN_EXAMPLE foo bar baz #+BEGIN_EXAMPLE foo bar baz
an example block with an example block with
multiple lines including multiple lines including

View file

@ -1,3 +1,6 @@
#+RESULTS:
: some results without a block
#+CAPTION: block caption #+CAPTION: block caption
#+BEGIN_SRC bash :results raw #+BEGIN_SRC bash :results raw
echo "a bash source block" echo "a bash source block"
@ -13,6 +16,14 @@ hello
a source block without a language a source block without a language
#+END_SRC #+END_SRC
#+BEGIN_SRC bash
echo a source block with results
#+END_SRC
#+RESULTS:
: a source block with results
#+BEGIN_EXAMPLE foo bar baz #+BEGIN_EXAMPLE foo bar baz
an example block with an example block with
multiple lines including multiple lines including

View file

@ -18,6 +18,7 @@ type Writer interface {
WriteNodeWithName(NodeWithName) WriteNodeWithName(NodeWithName)
WriteHeadline(Headline) WriteHeadline(Headline)
WriteBlock(Block) WriteBlock(Block)
WriteResult(Result)
WriteInlineBlock(InlineBlock) WriteInlineBlock(InlineBlock)
WriteExample(Example) WriteExample(Example)
WriteDrawer(Drawer) WriteDrawer(Drawer)
@ -58,6 +59,8 @@ func WriteNodes(w Writer, nodes ...Node) {
w.WriteHeadline(n) w.WriteHeadline(n)
case Block: case Block:
w.WriteBlock(n) w.WriteBlock(n)
case Result:
w.WriteResult(n)
case InlineBlock: case InlineBlock:
w.WriteInlineBlock(n) w.WriteInlineBlock(n)
case Example: case Example: