diff --git a/org/block.go b/org/block.go index 46d031d..e1480c3 100644 --- a/org/block.go +++ b/org/block.go @@ -10,6 +10,7 @@ type Block struct { Name string Parameters []string Children []Node + Result Node } type Result struct { @@ -58,7 +59,7 @@ func (d *Document) parseBlock(i int, parentStop stopFn) (int, Node) { stop := func(d *Document, i int) bool { return i >= len(d.tokens) || (d.tokens[i].kind == "endBlock" && d.tokens[i].content == name) } - block, i := Block{name, parameters, nil}, i+1 + block, i := Block{name, parameters, nil, nil}, i+1 if isRawTextBlock(name) { rawText := "" for ; !stop(d, i); i++ { @@ -73,10 +74,26 @@ func (d *Document) parseBlock(i int, parentStop stopFn) (int, Node) { block.Children = nodes i += consumed } - if i < len(d.tokens) && d.tokens[i].kind == "endBlock" && d.tokens[i].content == name { - return i + 1 - start, block + if i >= len(d.tokens) || d.tokens[i].kind != "endBlock" || d.tokens[i].content != name { + return 0, nil } - return 0, nil + if name == "SRC" { + consumed, result := d.parseSrcBlockResult(i+1, parentStop) + block.Result = result + i += consumed + } + return i + 1 - start, block +} + +func (d *Document) parseSrcBlockResult(i int, parentStop stopFn) (int, Node) { + start := i + for ; !parentStop(d, i) && d.tokens[i].kind == "text" && d.tokens[i].content == ""; i++ { + } + if parentStop(d, i) || d.tokens[i].kind != "result" { + return 0, nil + } + consumed, result := d.parseResult(i, parentStop) + return (i - start) + consumed, result } func (d *Document) parseExample(i int, parentStop stopFn) (int, Node) { diff --git a/org/html_writer.go b/org/html_writer.go index e9871cf..3fc6783 100644 --- a/org/html_writer.go +++ b/org/html_writer.go @@ -134,6 +134,10 @@ func (w *HTMLWriter) WriteBlock(b Block) { w.WriteString(fmt.Sprintf(`
`, strings.ToLower(b.Name)) + "\n") w.WriteString(content + "
\n") } + + if b.Result != nil { + WriteNodes(w, b.Result) + } } func (w *HTMLWriter) WriteResult(r Result) { WriteNodes(w, r.Node) } diff --git a/org/keyword.go b/org/keyword.go index 3ab8804..aa96caa 100644 --- a/org/keyword.go +++ b/org/keyword.go @@ -150,7 +150,7 @@ func (d *Document) parseInclude(k Keyword) (int, Node) { d.Log.Printf("Bad include %#v: %s", k, err) return k } - return Block{strings.ToUpper(kind), []string{lang}, d.parseRawInline(string(bs))} + return Block{strings.ToUpper(kind), []string{lang}, d.parseRawInline(string(bs)), nil} } } return 1, Include{k, resolve} diff --git a/org/org_writer.go b/org/org_writer.go index cd41e0c..e22a40c 100644 --- a/org/org_writer.go +++ b/org/org_writer.go @@ -102,6 +102,11 @@ func (w *OrgWriter) WriteBlock(b Block) { w.WriteString(w.indent) } w.WriteString("#+END_" + b.Name + "\n") + + if b.Result != nil { + w.WriteString("\n") + WriteNodes(w, b.Result) + } } func (w *OrgWriter) WriteResult(r Result) {