diff --git a/doc/rpc/generate.go b/doc/rpc/generate.go --- a/doc/rpc/generate.go +++ b/doc/rpc/generate.go @@ -16,6 +16,7 @@ "os" "os/exec" "path" + "regexp" "strings" "text/template" ) @@ -99,6 +100,38 @@ } else { name := strings.Split(line, " ")[0] desc := run("help", name) + + // Wrap result sections in code blocks + resultSections := regexp.MustCompile(`Result.*:`).FindAllStringIndex(desc, -1) + if resultSections != nil { + // Insert code blocks in reverse to preserve matched indices + lastSection := len(resultSections) - 1 + for i := lastSection; i >= 0; i-- { + resultIndex := resultSections[i] + start := resultIndex[0] + end := resultIndex[1] + + if i == lastSection { + // Look for the end of the last section. If it can't be found, append + // the end of the code block to end of the description. + endSectionIndex := regexp.MustCompile(`(?s)\n\n`).FindStringIndex(desc[end:]) + if endSectionIndex == nil { + desc += "```\n" + } else { + desc = desc[:end+endSectionIndex[0]+1] + "```\n" + desc[end+endSectionIndex[0]+1:] + } + } + + // Start new code block + desc = desc[:end] + "\n```" + desc[end:] + + if i != 0 { + // End previous code block + desc = desc[:start-1] + "```\n" + desc[start-1:] + } + } + } + comm := Command{ Name: name, Description: desc,