Skip to content

Commit

Permalink
utils/cobrautil/templates: support links formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Choraden authored and mmatczuk committed Mar 25, 2024
1 parent 86c3985 commit bd0e343
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
25 changes: 24 additions & 1 deletion utils/cobrautil/templates/help_flags_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"io"
"regexp"
"strings"
"unicode"

Expand Down Expand Up @@ -61,7 +62,8 @@ func (p *HelpFlagPrinter) PrintHelpFlag(flag *flag.Flag) {

usageWithBreakLines := strings.ReplaceAll(usageStr, "<br>", "\n\n")
usageWithExamples := strings.ReplaceAll(usageWithBreakLines, "<ex>", "\"")
wrappedUsages := wordwrap.WrapString(usageWithExamples, p.wrapLimit-offset)
usageWithLinks := withLinks(usageWithExamples)
wrappedUsages := wordwrap.WrapString(usageWithLinks, p.wrapLimit-offset)
wrappedStr = flagStr + "\n" + wrappedUsages
appendTabStr := strings.ReplaceAll(wrappedStr, "\n", "\n\t")

Expand Down Expand Up @@ -163,6 +165,27 @@ func findValueType(usage string) int {
return len(runes)
}

func withLinks(s string) string {
re := regexp.MustCompile(linkPattern)

result := re.ReplaceAllStringFunc(s, func(match string) string {
submatches := re.FindStringSubmatch(match)
if len(submatches) < 4 {
// If the match does not have two groups (text, root, and path), return the match as is.
return match
}
text, root, path := submatches[1], submatches[2], submatches[3]

if text != "" {
text = text + ": "
}

return text + root + path
})

return result
}

var envReplacer = strings.NewReplacer(".", "_", "-", "_")

func envName(envPrefix, flagName string) string {
Expand Down
4 changes: 4 additions & 0 deletions utils/cobrautil/templates/link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package templates

// Regular expression pattern to match "<link text,root,path>".
var linkPattern = `<link (.*?),(.*?),(.*?)>`
19 changes: 19 additions & 0 deletions utils/cobrautil/templates/markdown_flag_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"bytes"
"fmt"
"io"
"regexp"
"strings"

"github.com/spf13/pflag"
Expand All @@ -35,11 +36,29 @@ func (p *MarkdownFlagPrinter) PrintHelpFlag(f *pflag.Flag) {
body = strings.ReplaceAll(body, ". ", ".\n")
body = strings.ReplaceAll(body, "<br>", "\n")
body = strings.ReplaceAll(body, "<ex>", "\n```\n")
body = withMarkdownLinks(body)

fmt.Fprintf(p.out, body)
fmt.Fprintf(p.out, "\n\n")
}

func withMarkdownLinks(s string) string {
re := regexp.MustCompile(linkPattern)

result := re.ReplaceAllStringFunc(s, func(match string) string {
submatches := re.FindStringSubmatch(match)
if len(submatches) < 4 {
// If the match does not have two groups (text, root, and path), return the match as is.
return match
}
text, path := submatches[1], submatches[3]

return fmt.Sprintf("[%s](%s)", text, path)
})

return result
}

func (p *MarkdownFlagPrinter) header(f *pflag.Flag) string {
format := "--%s"
if f.Shorthand != "" {
Expand Down

0 comments on commit bd0e343

Please sign in to comment.