github.com/hazelops/ize@v1.1.12-0.20230915191306-97d7c0e48f11/pkg/templates/nornalizer.go (about) 1 package templates 2 3 import ( 4 "strings" 5 ) 6 7 const Indentation = ` ` 8 9 // LongDesc normalizes a command's long description to follow the conventions. 10 func LongDesc(s string) string { 11 if len(s) == 0 { 12 return s 13 } 14 return normalizer{s}.trim().indent().string 15 } 16 17 // Examples normalizes a command's examples to follow the conventions. 18 func Examples(s string) string { 19 if len(s) == 0 { 20 return s 21 } 22 return normalizer{s}.trim().indent().string 23 } 24 25 // Dedent removes any common leading whitespace from every line in text. 26 func Dedent(s string) string { 27 if len(s) == 0 { 28 return s 29 } 30 return normalizer{s}.trim().dedent().string 31 } 32 33 type normalizer struct { 34 string 35 } 36 37 func (s normalizer) trim() normalizer { 38 s.string = strings.TrimSpace(s.string) 39 return s 40 } 41 42 func (s normalizer) dedent() normalizer { 43 text := []string{} 44 for _, line := range strings.Split(s.string, "\n") { 45 trimmed := strings.TrimSpace(line) 46 text = append(text, trimmed) 47 } 48 s.string = strings.Join(text, "\n") 49 return s 50 } 51 52 func (s normalizer) indent() normalizer { 53 indentedLines := []string{} 54 for _, line := range strings.Split(s.string, "\n") { 55 trimmed := strings.TrimSpace(line) 56 indented := Indentation + trimmed 57 indentedLines = append(indentedLines, indented) 58 } 59 s.string = strings.Join(indentedLines, "\n") 60 return s 61 }