example_syncer.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright 2015 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. "fmt"
  16. "io/ioutil"
  17. "regexp"
  18. "strings"
  19. )
  20. const exampleToken = "EXAMPLE"
  21. const exampleLineStart = "<!-- BEGIN MUNGE: EXAMPLE"
  22. var exampleMungeTagRE = regexp.MustCompile(beginMungeTag(fmt.Sprintf("%s %s", exampleToken, `(([^ ])*[.]([^.]*))`)))
  23. // syncExamples updates all examples in markdown file.
  24. //
  25. // Finds the magic macro block tags, find the link to the example
  26. // specified in the tags, and replaces anything between those with
  27. // the content of the example, thereby syncing it.
  28. //
  29. // For example,
  30. // <!-- BEGIN MUNGE: EXAMPLE ../../examples/guestbook/frontend-service.yaml -->
  31. //
  32. // ```yaml
  33. // foo:
  34. // bar:
  35. // ```
  36. //
  37. // [Download example](../../examples/guestbook/frontend-service.yaml?raw=true)
  38. // <!-- END MUNGE: EXAMPLE -->
  39. func syncExamples(filePath string, mlines mungeLines) (mungeLines, error) {
  40. var err error
  41. type exampleTag struct {
  42. token string
  43. linkText string
  44. fileType string
  45. }
  46. exampleTags := []exampleTag{}
  47. // collect all example Tags
  48. for _, mline := range mlines {
  49. if mline.preformatted || !mline.beginTag {
  50. continue
  51. }
  52. line := mline.data
  53. if !strings.HasPrefix(line, exampleLineStart) {
  54. continue
  55. }
  56. match := exampleMungeTagRE.FindStringSubmatch(line)
  57. if len(match) < 4 {
  58. err = fmt.Errorf("Found unparsable EXAMPLE munge line %v", line)
  59. return mlines, err
  60. }
  61. tag := exampleTag{
  62. token: exampleToken + " " + match[1],
  63. linkText: match[1],
  64. fileType: match[3],
  65. }
  66. exampleTags = append(exampleTags, tag)
  67. }
  68. // update all example Tags
  69. for _, tag := range exampleTags {
  70. ft := ""
  71. if tag.fileType == "json" {
  72. ft = "json"
  73. }
  74. if tag.fileType == "yaml" {
  75. ft = "yaml"
  76. }
  77. example, err := exampleContent(filePath, tag.linkText, ft)
  78. if err != nil {
  79. return mlines, err
  80. }
  81. mlines, err = updateMacroBlock(mlines, tag.token, example)
  82. if err != nil {
  83. return mlines, err
  84. }
  85. }
  86. return mlines, nil
  87. }
  88. // exampleContent retrieves the content of the file at linkPath
  89. func exampleContent(filePath, linkPath, fileType string) (mungeLines, error) {
  90. repoRel, err := makeRepoRelative(linkPath, filePath)
  91. if err != nil {
  92. return nil, err
  93. }
  94. fileRel, err := makeFileRelative(linkPath, filePath)
  95. if err != nil {
  96. return nil, err
  97. }
  98. dat, err := ioutil.ReadFile(repoRel)
  99. if err != nil {
  100. return nil, err
  101. }
  102. // remove leading and trailing spaces and newlines
  103. trimmedFileContent := strings.TrimSpace(string(dat))
  104. content := fmt.Sprintf("\n```%s\n%s\n```\n\n[Download example](%s?raw=true)", fileType, trimmedFileContent, fileRel)
  105. out := getMungeLines(content)
  106. return out, nil
  107. }