|
| 1 | +/* |
| 2 | +Copyright 2015 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "io/ioutil" |
| 23 | + "path" |
| 24 | + "regexp" |
| 25 | + "strings" |
| 26 | +) |
| 27 | + |
| 28 | +const exampleMungeTag = "EXAMPLE" |
| 29 | + |
| 30 | +// syncExamples updates all examples in markdown file. |
| 31 | +// |
| 32 | +// Finds the magic macro block tags, find the link to the example |
| 33 | +// specified in the tags, and replaces anything between those with |
| 34 | +// the content of the example, thereby syncing it. |
| 35 | +// |
| 36 | +// For example, |
| 37 | +// <!-- BEGIN MUNGE: EXAMPLE ../../examples/guestbook/frontend-controller.yaml --> |
| 38 | +// |
| 39 | +// ```yaml |
| 40 | +// foo: |
| 41 | +// bar: |
| 42 | +// ``` |
| 43 | +// |
| 44 | +// [Download example](../../examples/guestbook/frontend-controller.yaml) |
| 45 | +// <!-- END MUNGE: EXAMPLE --> |
| 46 | +func syncExamples(filePath string, markdown []byte) ([]byte, error) { |
| 47 | + // find the example syncer begin tag |
| 48 | + header := beginMungeTag(fmt.Sprintf("%s %s", exampleMungeTag, `(([^ ])*.(yaml|json))`)) |
| 49 | + exampleLinkRE := regexp.MustCompile(header) |
| 50 | + lines := splitLines(markdown) |
| 51 | + updatedMarkdown, err := updateExampleMacroBlock(filePath, lines, exampleLinkRE, endMungeTag(exampleMungeTag)) |
| 52 | + if err != nil { |
| 53 | + return updatedMarkdown, err |
| 54 | + } |
| 55 | + return updatedMarkdown, nil |
| 56 | +} |
| 57 | + |
| 58 | +// exampleContent retrieves the content of the file at linkPath |
| 59 | +func exampleContent(filePath, linkPath, fileType string) (content string, err error) { |
| 60 | + realRoot := path.Join(*rootDir, *repoRoot) + "/" |
| 61 | + path := path.Join(realRoot, path.Dir(filePath), linkPath) |
| 62 | + dat, err := ioutil.ReadFile(path) |
| 63 | + if err != nil { |
| 64 | + return content, err |
| 65 | + } |
| 66 | + content = fmt.Sprintf("\n```%s\n%s\n```\n\n[Download example](%s)", fileType, string(dat), linkPath) |
| 67 | + return |
| 68 | +} |
| 69 | + |
| 70 | +// updateExampleMacroBlock sync the yaml/json example between begin tag and end tag |
| 71 | +func updateExampleMacroBlock(filePath string, lines []string, beginMarkExp *regexp.Regexp, endMark string) ([]byte, error) { |
| 72 | + var buffer bytes.Buffer |
| 73 | + betweenBeginAndEnd := false |
| 74 | + for _, line := range lines { |
| 75 | + trimmedLine := strings.Trim(line, " \n") |
| 76 | + if beginMarkExp.Match([]byte(trimmedLine)) { |
| 77 | + if betweenBeginAndEnd { |
| 78 | + return nil, fmt.Errorf("found second begin mark while updating macro blocks") |
| 79 | + } |
| 80 | + betweenBeginAndEnd = true |
| 81 | + buffer.WriteString(line) |
| 82 | + buffer.WriteString("\n") |
| 83 | + match := beginMarkExp.FindStringSubmatch(line) |
| 84 | + if len(match) < 4 { |
| 85 | + return nil, fmt.Errorf("failed to parse the link in example header") |
| 86 | + } |
| 87 | + // match[0] is the entire expression; [1] is the link text and [3] is the file type (yaml or json). |
| 88 | + linkText := match[1] |
| 89 | + fileType := match[3] |
| 90 | + example, err := exampleContent(filePath, linkText, fileType) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + buffer.WriteString(example) |
| 95 | + } else if trimmedLine == endMark { |
| 96 | + if !betweenBeginAndEnd { |
| 97 | + return nil, fmt.Errorf("found end mark without being mark while updating macro blocks") |
| 98 | + } |
| 99 | + // Extra newline avoids github markdown bug where comment ends up on same line as last bullet. |
| 100 | + buffer.WriteString("\n") |
| 101 | + buffer.WriteString(line) |
| 102 | + buffer.WriteString("\n") |
| 103 | + betweenBeginAndEnd = false |
| 104 | + } else { |
| 105 | + if !betweenBeginAndEnd { |
| 106 | + buffer.WriteString(line) |
| 107 | + buffer.WriteString("\n") |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + if betweenBeginAndEnd { |
| 112 | + return nil, fmt.Errorf("never found closing end mark while updating macro blocks") |
| 113 | + } |
| 114 | + return buffer.Bytes(), nil |
| 115 | +} |
0 commit comments