normalizers.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package templates
  14. import (
  15. "strings"
  16. "github.com/MakeNowJust/heredoc"
  17. "github.com/russross/blackfriday"
  18. "github.com/spf13/cobra"
  19. )
  20. const Indentation = ` `
  21. // LongDesc normalizes a command's long description to follow the conventions.
  22. func LongDesc(s string) string {
  23. if len(s) == 0 {
  24. return s
  25. }
  26. return normalizer{s}.heredoc().markdown().trim().string
  27. }
  28. // Examples normalizes a command's examples to follow the conventions.
  29. func Examples(s string) string {
  30. if len(s) == 0 {
  31. return s
  32. }
  33. return normalizer{s}.trim().indent().string
  34. }
  35. // Normalize perform all required normalizations on a given command.
  36. func Normalize(cmd *cobra.Command) *cobra.Command {
  37. if len(cmd.Long) > 0 {
  38. cmd.Long = LongDesc(cmd.Long)
  39. }
  40. if len(cmd.Example) > 0 {
  41. cmd.Example = Examples(cmd.Example)
  42. }
  43. return cmd
  44. }
  45. // NormalizeAll perform all required normalizations in the entire command tree.
  46. func NormalizeAll(cmd *cobra.Command) *cobra.Command {
  47. if cmd.HasSubCommands() {
  48. for _, subCmd := range cmd.Commands() {
  49. NormalizeAll(subCmd)
  50. }
  51. }
  52. Normalize(cmd)
  53. return cmd
  54. }
  55. type normalizer struct {
  56. string
  57. }
  58. func (s normalizer) markdown() normalizer {
  59. bytes := []byte(s.string)
  60. formatted := blackfriday.Markdown(bytes, &ASCIIRenderer{Indentation: Indentation}, blackfriday.EXTENSION_NO_INTRA_EMPHASIS)
  61. s.string = string(formatted)
  62. return s
  63. }
  64. func (s normalizer) heredoc() normalizer {
  65. s.string = heredoc.Doc(s.string)
  66. return s
  67. }
  68. func (s normalizer) trim() normalizer {
  69. s.string = strings.TrimSpace(s.string)
  70. return s
  71. }
  72. func (s normalizer) indent() normalizer {
  73. indentedLines := []string{}
  74. for _, line := range strings.Split(s.string, "\n") {
  75. trimmed := strings.TrimSpace(line)
  76. indented := Indentation + trimmed
  77. indentedLines = append(indentedLines, indented)
  78. }
  79. s.string = strings.Join(indentedLines, "\n")
  80. return s
  81. }