Allow using :role:'payload' in addition to :role:payload as the former can be used in Go `` string literals

This commit is contained in:
Kovid Goyal 2022-09-21 20:53:19 +05:30
parent 7c41737370
commit 317b108497
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -50,38 +50,41 @@ func New(allow_escape_codes bool) *Context {
return &ans return &ans
} }
func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string { func replace_all_rst_roles(str string, repl func(rst_format_match) string) string {
result := "" result := strings.Builder{}
lastIndex := 0 result.Grow(len(str) + 256)
last_index := 0
for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) { matches := prettify_pat().FindAllStringSubmatchIndex(str, -1)
groups := []string{} for _, v := range matches {
for i := 0; i < len(v); i += 2 { match_start, match_end := v[0], v[1]
if v[i] == -1 || v[i+1] == -1 { m := rst_format_match{}
groups = append(groups, "") if v[2] > -1 && v[3] > -1 {
} else { m.role = str[v[2]:v[3]]
groups = append(groups, str[v[i]:v[i+1]])
} }
if v[4] > -1 && v[5] > -1 {
m.payload = str[v[4]:v[5]]
} else if v[6] > -1 && v[7] > -1 {
m.payload = str[v[6]:v[7]]
} }
result += str[lastIndex:v[0]] + repl(groups) result.WriteString(str[last_index:match_start])
lastIndex = v[1] result.WriteString(repl(m))
last_index = match_end
} }
return result + str[lastIndex:] result.WriteString(str[last_index:])
return result.String()
} }
func website_url(doc string) string { var _prettify_pat *regexp.Regexp
if doc != "" {
doc = strings.TrimSuffix(doc, "/")
if doc != "" {
doc += "/"
}
}
return kitty.WebsiteBaseURL + doc
}
var prettify_pat = regexp.MustCompile(":([a-z]+):`([^`]+)`") func prettify_pat() *regexp.Regexp {
if _prettify_pat == nil {
_prettify_pat = regexp.MustCompile(":([a-z]+):(?:(?:`([^`]+)`)|(?:'([^']+)'))")
}
return _prettify_pat
}
func (self *Context) hyperlink_for_url(url string, text string) string { func (self *Context) hyperlink_for_url(url string, text string) string {
return self.Url(url, text) return self.Url(url, text)
@ -108,19 +111,23 @@ func text_and_target(x string) (text string, target string) {
return return
} }
type rst_format_match struct {
role, payload string
}
func (self *Context) ref_hyperlink(x string, prefix string) string { func (self *Context) ref_hyperlink(x string, prefix string) string {
text, target := text_and_target(x) text, target := text_and_target(x)
url := "kitty+doc://" + utils.CachedHostname() + "/#ref=" + prefix + target url := "kitty+doc://" + utils.CachedHostname() + "/#ref=" + prefix + target
text = ReplaceAllStringSubmatchFunc(prettify_pat, text, func(groups []string) string { text = replace_all_rst_roles(text, func(group rst_format_match) string {
return groups[2] return group.payload
}) })
return self.hyperlink_for_url(url, text) return self.hyperlink_for_url(url, text)
} }
func (self *Context) Prettify(text string) string { func (self *Context) Prettify(text string) string {
return ReplaceAllStringSubmatchFunc(prettify_pat, text, func(groups []string) string { return replace_all_rst_roles(text, func(group rst_format_match) string {
val := groups[2] val := group.payload
switch groups[1] { switch group.role {
case "file": case "file":
if val == "kitty.conf" && self.fmt_ctx.AllowEscapeCodes { if val == "kitty.conf" && self.fmt_ctx.AllowEscapeCodes {
path := filepath.Join(utils.ConfigDir(), val) path := filepath.Join(utils.ConfigDir(), val)