Implement escaping in src (org only) and example blocks

inside src example blocks lines starting with `\s*,` are escaped - i.e. org
mode will not try to parse them as e.g. a headline. We don't want to render the
escape commata to html so let's take them out - and put them back in before
rendering to org. Doing it this way allows us to render them correctly even
when the input did not include them.

see https://orgmode.org/manual/Literal-Examples.html#Literal-Examples
This commit is contained in:
Niklas Fasching 2020-04-16 15:49:32 +02:00
parent cbf1b8c38c
commit d417c2a6dd
5 changed files with 65 additions and 2 deletions

View file

@ -19,6 +19,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 exampleBlockEscapeRegexp = regexp.MustCompile(`(^|\n)([ \t]*),([ \t]*)(\*|,\*|#\+|,#\+)`)
func lexBlock(line string) (token, bool) {
if m := beginBlockRegexp.FindStringSubmatch(line); m != nil {
@ -51,6 +52,9 @@ func (d *Document) parseBlock(i int, parentStop stopFn) (int, Node) {
for ; !stop(d, i); i++ {
rawText += trim(d.tokens[i].matches[0]) + "\n"
}
if name == "EXAMPLE" || (name == "SRC" && len(parameters) >= 1 && parameters[0] == "org") {
rawText = exampleBlockEscapeRegexp.ReplaceAllString(rawText, "$1$2$3$4")
}
block.Children = d.parseRawInline(rawText)
} else {
consumed, nodes := d.parseMany(i, stop)