postprocessing.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. Copyright 2017 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 main
  14. import (
  15. "io/ioutil"
  16. "path/filepath"
  17. "strings"
  18. "github.com/spf13/cobra"
  19. )
  20. // MarkdownPostProcessing goes though the generated files
  21. func MarkdownPostProcessing(cmd *cobra.Command, dir string, processor func(string) string) error {
  22. for _, c := range cmd.Commands() {
  23. if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
  24. continue
  25. }
  26. if err := MarkdownPostProcessing(c, dir, processor); err != nil {
  27. return err
  28. }
  29. }
  30. basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md"
  31. filename := filepath.Join(dir, basename)
  32. markdownBytes, err := ioutil.ReadFile(filename)
  33. if err != nil {
  34. return err
  35. }
  36. processedMarkDown := processor(string(markdownBytes))
  37. return ioutil.WriteFile(filename, []byte(processedMarkDown), 0644)
  38. }
  39. // cleanupForInclude parts of markdown that will make difficult to use it as include in the website:
  40. // - The title of the document (this allow more flexibility for include, e.g. include in tabs)
  41. // - The sections see also, that assumes file will be used as a main page
  42. func cleanupForInclude(md string) string {
  43. lines := strings.Split(md, "\n")
  44. cleanMd := ""
  45. for i, line := range lines {
  46. if i == 0 {
  47. continue
  48. }
  49. if line == "### SEE ALSO" {
  50. break
  51. }
  52. cleanMd += line
  53. if i < len(lines)-1 {
  54. cleanMd += "\n"
  55. }
  56. }
  57. return cleanMd
  58. }