Add support for latex blocks / environments
current support for latex fragments was inline only, i.e. lines containing block elements (e.g. a line starting with `* `, i.e. a headline) will not be parsed as part of the latex fragment but the respective block element. Parsing latex blocks at the block level should fix that. Note that in any case we don't do any processing and just emit the raw latex (leaving the rendering to e.g. js).
This commit is contained in:
parent
05a2dedbf8
commit
1849701ba7
8 changed files with 77 additions and 5 deletions
39
org/block.go
39
org/block.go
|
@ -1,6 +1,7 @@
|
|||
package org
|
||||
|
||||
import (
|
||||
"math"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
@ -21,9 +22,15 @@ type Example struct {
|
|||
Children []Node
|
||||
}
|
||||
|
||||
type LatexBlock struct {
|
||||
Content []Node
|
||||
}
|
||||
|
||||
var exampleLineRegexp = regexp.MustCompile(`^(\s*):(\s(.*)|\s*$)`)
|
||||
var beginBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+BEGIN_(\w+)(.*)`)
|
||||
var endBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+END_(\w+)`)
|
||||
var beginLatexBlockRegexp = regexp.MustCompile(`(?i)^(\s*)\\begin{([^}]+)}(\s*)$`)
|
||||
var endLatexBlockRegexp = regexp.MustCompile(`(?i)^(\s*)\\end{([^}]+)}(\s*)$`)
|
||||
var resultRegexp = regexp.MustCompile(`(?i)^(\s*)#\+RESULTS:`)
|
||||
var exampleBlockEscapeRegexp = regexp.MustCompile(`(^|\n)([ \t]*),([ \t]*)(\*|,\*|#\+|,#\+)`)
|
||||
|
||||
|
@ -36,6 +43,15 @@ func lexBlock(line string) (token, bool) {
|
|||
return nilToken, false
|
||||
}
|
||||
|
||||
func lexLatexBlock(line string) (token, bool) {
|
||||
if m := beginLatexBlockRegexp.FindStringSubmatch(line); m != nil {
|
||||
return token{"beginLatexBlock", len(m[1]), strings.ToUpper(m[2]), m}, true
|
||||
} else if m := endLatexBlockRegexp.FindStringSubmatch(line); m != nil {
|
||||
return token{"endLatexBlock", len(m[1]), strings.ToUpper(m[2]), m}, true
|
||||
}
|
||||
return nilToken, false
|
||||
}
|
||||
|
||||
func lexResult(line string) (token, bool) {
|
||||
if m := resultRegexp.FindStringSubmatch(line); m != nil {
|
||||
return token{"result", len(m[1]), "", m}, true
|
||||
|
@ -85,6 +101,22 @@ func (d *Document) parseBlock(i int, parentStop stopFn) (int, Node) {
|
|||
return i + 1 - start, block
|
||||
}
|
||||
|
||||
func (d *Document) parseLatexBlock(i int, parentStop stopFn) (int, Node) {
|
||||
t, start := d.tokens[i], i
|
||||
name, rawText, trim := t.content, "", trimIndentUpTo(int(math.Max((float64(d.baseLvl)), float64(t.lvl))))
|
||||
stop := func(d *Document, i int) bool {
|
||||
return i >= len(d.tokens) || (d.tokens[i].kind == "endLatexBlock" && d.tokens[i].content == name)
|
||||
}
|
||||
for ; !stop(d, i); i++ {
|
||||
rawText += trim(d.tokens[i].matches[0]) + "\n"
|
||||
}
|
||||
if i >= len(d.tokens) || d.tokens[i].kind != "endLatexBlock" || d.tokens[i].content != name {
|
||||
return 0, nil
|
||||
}
|
||||
rawText += trim(d.tokens[i].matches[0])
|
||||
return i + 1 - start, LatexBlock{d.parseRawInline(rawText)}
|
||||
}
|
||||
|
||||
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++ {
|
||||
|
@ -145,6 +177,7 @@ func (b Block) ParameterMap() map[string]string {
|
|||
return m
|
||||
}
|
||||
|
||||
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) }
|
||||
func (n Example) String() string { return orgWriter.WriteNodesAsString(n) }
|
||||
func (n Block) String() string { return orgWriter.WriteNodesAsString(n) }
|
||||
func (n LatexBlock) String() string { return orgWriter.WriteNodesAsString(n) }
|
||||
func (n Result) String() string { return orgWriter.WriteNodesAsString(n) }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue