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

@ -2,6 +2,7 @@ package org
import (
"fmt"
"regexp"
"strings"
"unicode"
"unicode/utf8"
@ -16,6 +17,8 @@ type OrgWriter struct {
indent string
}
var exampleBlockUnescapeRegexp = regexp.MustCompile(`(^|\n)([ \t]*)(\*|,\*|#\+|,#\+)`)
var emphasisOrgBorders = map[string][]string{
"_": []string{"_", "_"},
"*": []string{"*", "*"},
@ -90,7 +93,11 @@ func (w *OrgWriter) WriteBlock(b Block) {
if isRawTextBlock(b.Name) {
w.WriteString(w.indent)
}
WriteNodes(w, b.Children...)
content := w.WriteNodesAsString(b.Children...)
if b.Name == "EXAMPLE" || (b.Name == "SRC" && len(b.Parameters) >= 1 && b.Parameters[0] == "org") {
content = exampleBlockUnescapeRegexp.ReplaceAllString(content, "$1$2,$3")
}
w.WriteString(content)
if !isRawTextBlock(b.Name) {
w.WriteString(w.indent)
}