Handle backslash escapes in :code: blocks

This commit is contained in:
Kovid Goyal 2022-11-25 22:03:13 +05:30
parent 5d3a9f2628
commit 4969611bdb
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"unicode"
"kitty"
"kitty/tools/utils"
@ -50,6 +51,28 @@ func New(allow_escape_codes bool) *Context {
return &ans
}
func remove_backslash_escapes(text string) string {
// see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#escaping-mechanism
out := strings.Builder{}
prev_was_slash := false
out.Grow(len(text))
for _, ch := range text {
if prev_was_slash {
if !unicode.IsSpace(ch) {
out.WriteRune(ch)
}
prev_was_slash = false
} else {
if ch == '\\' {
prev_was_slash = true
} else {
out.WriteRune(ch)
}
}
}
return out.String()
}
func replace_all_rst_roles(str string, repl func(rst_format_match) string) string {
var m rst_format_match
rf := func(full_match string, groupdict map[string]utils.SubMatch) string {
@ -132,7 +155,7 @@ func (self *Context) Prettify(text string) string {
case "term":
return self.ref_hyperlink(val, "term-")
case "code":
return self.Code(val)
return self.Code(remove_backslash_escapes(val))
case "option":
idx := strings.LastIndex(val, "--")
if idx < 0 {