Skip to content

Commit

Permalink
chore(utils/cobrautil/templates): refactor string replace to method
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatczuk committed May 21, 2024
1 parent f71763b commit 9e5f099
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 30 deletions.
30 changes: 19 additions & 11 deletions utils/cobrautil/templates/help_flags_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,33 @@ func (p *HelpFlagPrinter) PrintHelpFlag(flag *flag.Flag) {

// It always has at least 2 elements.
flagStr, usage := flagAndUsage[0], strings.Join(flagAndUsage[1:], " ")

usage = strings.ReplaceAll(usage, "<br>", "\n")
usage = strings.ReplaceAll(usage, "<ul>", "")
usage = strings.ReplaceAll(usage, "<li>", "\n- ")
usage = strings.ReplaceAll(usage, "</ul>", "\n\n")
usage = strings.ReplaceAll(usage, "<code>", "")
usage = strings.ReplaceAll(usage, "</code>", "")
usage = strings.ReplaceAll(usage, "<code-block>", "\"")
usage = strings.ReplaceAll(usage, "</code-block>", "\"")
usage = p.replaceHTML(usage)
usage = withLinks(usage)
usage = strings.TrimSpace(usage)

usage = wordwrap.WrapString(usage, p.wrapLimit-offset)

wrappedStr = flagStr + "\n" + usage
appendTabStr := strings.ReplaceAll(wrappedStr, "\n", "\n\t")

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

func (p *HelpFlagPrinter) replaceHTML(s string) string {
r := strings.NewReplacer(
"<br>", "\n",
"<ul>", "",
"<li>", "\n- ",
"</ul>", "\n\n",
"<code>", "",
"</code>", "",
"<code-block>", "\"",
"</code-block>", "\"",
)

s = r.Replace(s)
s = strings.TrimSpace(s)
return s
}

// writeFlag will output the help flag based
// on the format provided by getFlagFormat to i/o writer
func writeFlag(out io.Writer, f *flag.Flag, envPrefix string) {
Expand Down
29 changes: 19 additions & 10 deletions utils/cobrautil/templates/markdown_flag_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,31 @@ func (p *MarkdownFlagPrinter) PrintHelpFlag(f *pflag.Flag) {
fmt.Fprint(p.out, "\n\n")

body := p.body(f)
body = strings.ReplaceAll(body, ". ", ".\n")
body = strings.ReplaceAll(body, "<br>", "\n")
body = strings.ReplaceAll(body, "<ul>", "\n")
body = strings.ReplaceAll(body, "<li>", "\n- ")
body = strings.ReplaceAll(body, "</ul>", "\n\n")
body = strings.ReplaceAll(body, "<code>", "`")
body = strings.ReplaceAll(body, "</code>", "`")
body = strings.ReplaceAll(body, "<code-block>", "\n```\n")
body = strings.ReplaceAll(body, "</code-block>", "\n```\n")
body = p.replaceHTML(body)
body = withMarkdownLinks(body)
body = strings.TrimSpace(body)

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

func (p *MarkdownFlagPrinter) replaceHTML(s string) string {
r := strings.NewReplacer(
". ", ".\n",
"<br>", "\n",
"<ul>", "\n",
"<li>", "\n- ",
"</ul>", "\n\n",
"<code>", "`",
"</code>", "`",
"<code-block>", "\n```\n",
"</code-block>", "\n```\n",
)

s = r.Replace(s)
s = strings.TrimSpace(s)
return s
}

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

Expand Down
27 changes: 18 additions & 9 deletions utils/cobrautil/templates/yaml_flag_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,8 @@ func NewYamlFlagPrinter(out io.Writer, wrapLimit uint) *YamlFlagPrinter {
func (p *YamlFlagPrinter) PrintHelpFlag(f *pflag.Flag) {
name, usage := flagNameAndUsage(f)

usage = strings.ReplaceAll(usage, "<br>", "\n")
usage = strings.ReplaceAll(usage, "<ul>", "")
usage = strings.ReplaceAll(usage, "<li>", "\n- ")
usage = strings.ReplaceAll(usage, "</ul>", "\n\n")
usage = strings.ReplaceAll(usage, "<code>", "")
usage = strings.ReplaceAll(usage, "</code>", "")
usage = strings.ReplaceAll(usage, "<code-block>", "\"")
usage = strings.ReplaceAll(usage, "</code-block>", "\"")
usage = p.replaceHTML(usage)
usage = withLinks(usage)
usage = strings.TrimSpace(usage)

fmt.Fprintf(p.out, "# %s%s\n#\n", f.Name, name)
for _, l := range strings.Split(wordwrap.WrapString(usage, p.wrapLimit-2), "\n") {
Expand All @@ -51,6 +43,23 @@ func (p *YamlFlagPrinter) PrintHelpFlag(f *pflag.Flag) {
fmt.Fprintf(p.out, "#%s: %s\n\n", f.Name, p.defaultValue(f))
}

func (p *YamlFlagPrinter) replaceHTML(s string) string {
r := strings.NewReplacer(
"<br>", "\n",
"<ul>", "",
"<li>", "\n- ",
"</ul>", "\n\n",
"<code>", "",
"</code>", "",
"<code-block>", "\"",
"</code-block>", "\"",
)

s = r.Replace(s)
s = strings.TrimSpace(s)
return s
}

func (p *YamlFlagPrinter) defaultValue(f *pflag.Flag) string {
def := f.DefValue
if def == "[]" {
Expand Down

0 comments on commit 9e5f099

Please sign in to comment.