Skip to content

Commit 5b8f09f

Browse files
authored
Merge pull request #1200 from sivanzcw/dev
Format book-related coe
2 parents 4539e30 + 66cdfec commit 5b8f09f

File tree

12 files changed

+83
-78
lines changed

12 files changed

+83
-78
lines changed

docs/book/src/cronjob-tutorial/testdata/emptycontroller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ objects, so these are added out of the box.
4141
// CronJobReconciler reconciles a CronJob object
4242
type CronJobReconciler struct {
4343
client.Client
44-
Log logr.Logger
44+
Log logr.Logger
4545
Scheme *runtime.Scheme
4646
}
4747

docs/book/utils/litgo/literate.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ package main
1818

1919
import (
2020
"fmt"
21-
"os"
22-
"io/ioutil"
23-
"path/filepath"
2421
"go/scanner"
2522
"go/token"
23+
"io/ioutil"
2624
"log"
25+
"net/url"
26+
"os"
27+
"path"
28+
"path/filepath"
2729
"strings"
2830
"unicode"
29-
"path"
30-
"net/url"
3131

3232
"sigs.k8s.io/kubebuilder/docs/book/utils/plugin"
3333
)
@@ -44,15 +44,16 @@ type Literate struct {
4444
// BaseSourcePath specifies the base path to internet-reachable versions of the source code used
4545
BaseSourcePath *url.URL
4646
}
47+
4748
func (_ Literate) SupportsOutput(_ string) bool { return true }
4849
func (l Literate) Process(input *plugin.Input) error {
4950
bookSrcDir := filepath.Join(input.Context.Root, input.Context.Config.Book.Src)
5051
return plugin.EachCommand(&input.Book, "literatego", func(chapter *plugin.BookChapter, relPath string) (string, error) {
5152
chapterDir := filepath.Dir(chapter.Path)
5253
pathInfo := filePathInfo{
5354
chapterRelativePath: relPath,
54-
chapterDir: chapterDir,
55-
bookSrcDir: bookSrcDir,
55+
chapterDir: chapterDir,
56+
bookSrcDir: bookSrcDir,
5657
}
5758
path := pathInfo.FullPath()
5859

@@ -90,15 +91,15 @@ func (f filePathInfo) ViewablePath(baseBookSrcURL url.URL) string {
9091
outURL := baseBookSrcURL
9192

9293
outURL.Path = path.Join(outURL.Path, relPath)
93-
94+
9495
return outURL.String()
9596
}
9697

9798
// commentCodePair represents a block of code with some text before it, optionally
9899
// marked as collapsed with the given "collapse summary".
99100
type commentCodePair struct {
100-
comment string
101-
code string
101+
comment string
102+
code string
102103
collapse string
103104
}
104105

@@ -141,7 +142,7 @@ func isBlockComment(tok token.Token, lit string) bool {
141142
// commentText extracts the text from the given comment, slicing off
142143
// some common amount of whitespace prefix.
143144
func commentText(raw string, lineOffset int) string {
144-
rawBody := raw[2:len(raw)-2] // chop of the delimiters
145+
rawBody := raw[2 : len(raw)-2] // chop of the delimiters
145146
lines := strings.Split(rawBody, "\n")
146147
if len(lines) == 0 {
147148
return ""
@@ -188,21 +189,21 @@ func extractPairs(contents []byte, path string) ([]commentCodePair, error) {
188189
if collapse == "" && !isBlockComment(tok, lit) {
189190
continue
190191
}
191-
codeEnd := file.Offset(pos)-1
192-
if codeEnd - lastCodeBlockStart > 0 {
192+
codeEnd := file.Offset(pos) - 1
193+
if codeEnd-lastCodeBlockStart > 0 {
193194
lastPair.code = string(contents[lastCodeBlockStart:codeEnd])
194195
}
195196
pairs = append(pairs, lastPair)
196197
if collapse == "" {
197198
line := file.Line(pos)
198199
lineStart := file.LineStart(line)
199200
lastPair = commentCodePair{
200-
comment: commentText(lit, file.Offset(pos) - file.Offset(lineStart)),
201+
comment: commentText(lit, file.Offset(pos)-file.Offset(lineStart)),
201202
}
202203
} else {
203204
lastPair = commentCodePair{}
204205
}
205-
lastCodeBlockStart = file.Offset(pos)+len(lit)
206+
lastCodeBlockStart = file.Offset(pos) + len(lit)
206207
}
207208
lastPair.code = string(contents[lastCodeBlockStart:])
208209
pairs = append(pairs, lastPair)
@@ -236,7 +237,7 @@ func (l Literate) extractContents(contents []byte, pathInfo filePathInfo) (strin
236237
prettyPath = prunedPath
237238
}
238239
out.WriteString(fmt.Sprintf(`<cite class="literate-source"><a href="%[1]s">%[2]s</a></cite>`, sourcePath, prettyPath))
239-
240+
240241
for _, pair := range pairs {
241242
if pair.collapse != "" {
242243
// NB(directxman12): we add the hljs class to "cheat" and get the
@@ -256,7 +257,7 @@ func (l Literate) extractContents(contents []byte, pathInfo filePathInfo) (strin
256257
out.WriteString(wrapWithNewlines(pair.code))
257258
out.WriteString("```\n")
258259
}
259-
if pair.collapse != ""{
260+
if pair.collapse != "" {
260261
out.WriteString("\n</details>")
261262
}
262263
// TODO(directxman12): nice side-by-side sections
@@ -293,7 +294,7 @@ func main() {
293294
}
294295
cfg := Literate{
295296
PrettyPathPrunePrefix: "testdata",
296-
BaseSourcePath: baseURL,
297+
BaseSourcePath: baseURL,
297298
}
298299
if err := plugin.Run(cfg, os.Stdin, os.Stdout, os.Args[1:]...); err != nil {
299300
log.Fatal(err.Error())

docs/book/utils/markerdocs/doctypes.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ type DetailedHelp struct {
2424
}
2525

2626
type Argument struct {
27-
Type string `json:"type"`
28-
Optional bool `json:"optional"`
29-
ItemType *Argument `json:"itemType"`
27+
Type string `json:"type"`
28+
Optional bool `json:"optional"`
29+
ItemType *Argument `json:"itemType"`
3030
}
3131

3232
type FieldHelp struct {
3333
// definition
34-
Name string `json:"name"`
34+
Name string `json:"name"`
3535
Argument `json:",inline"`
3636

3737
// help
@@ -42,23 +42,22 @@ type FieldHelp struct {
4242
type MarkerDoc struct {
4343
// definition
4444

45-
Name string `json:"name"`
45+
Name string `json:"name"`
4646
Target string `json:"target"`
4747

4848
// help
4949

50-
DetailedHelp `json:",inline"`
51-
Category string `json:"category"`
52-
DeprecatedInFavorOf *string `json:"deprecatedInFavorOf"`
53-
Fields []FieldHelp `json:"fields"`
50+
DetailedHelp `json:",inline"`
51+
Category string `json:"category"`
52+
DeprecatedInFavorOf *string `json:"deprecatedInFavorOf"`
53+
Fields []FieldHelp `json:"fields"`
5454
}
5555

5656
type CategoryDoc struct {
57-
Category string `json:"category"`
58-
Markers []MarkerDoc `json:"markers"`
57+
Category string `json:"category"`
58+
Markers []MarkerDoc `json:"markers"`
5959
}
6060

6161
func (m MarkerDoc) Anonymous() bool {
6262
return len(m.Fields) == 1 && m.Fields[0].Name == ""
6363
}
64-

docs/book/utils/markerdocs/html.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ package main
1818

1919
import (
2020
"fmt"
21-
"strings"
2221
"html"
2322
"io"
24-
23+
"strings"
2524
)
2625

2726
// NB(directxman12): we use this instead of templates to avoid
@@ -39,17 +38,19 @@ type toHTML interface {
3938

4039
// Text is a chunk of text in an HTML doc.
4140
type Text string
41+
4242
func (t Text) WriteHTML(w io.Writer) error {
4343
_, err := io.WriteString(w, html.EscapeString(string(t)))
4444
return err
4545
}
4646

4747
// Tag is some tag with contents and attributes in an HTML doc.
4848
type Tag struct {
49-
Name string
50-
Attrs Attrs
49+
Name string
50+
Attrs Attrs
5151
Children []toHTML
5252
}
53+
5354
func (t Tag) WriteHTML(w io.Writer) error {
5455
attrsOut := ""
5556
if t.Attrs != nil {
@@ -74,6 +75,7 @@ func (t Tag) WriteHTML(w io.Writer) error {
7475

7576
// Fragment is some series of tags, text, etc in an HTML doc.
7677
type Fragment []toHTML
78+
7779
func (f Fragment) WriteHTML(w io.Writer) error {
7880
for _, item := range f {
7981
if err := item.WriteHTML(w); err != nil {
@@ -91,10 +93,12 @@ type Attrs interface {
9193

9294
// classes sets the class attribute to these class names.
9395
type classes []string
96+
9497
func (c classes) ToAttrs() string { return fmt.Sprintf("class=%q", strings.Join(c, " ")) }
9598

9699
// optionalClasses sets the the class attribute to these class names, if their values are true.
97100
type optionalClasses map[string]bool
101+
98102
func (c optionalClasses) ToAttrs() string {
99103
actualClasses := make([]string, 0, len(c))
100104
for class, active := range c {
@@ -107,6 +111,7 @@ func (c optionalClasses) ToAttrs() string {
107111

108112
// attrs joins together one or more Attrs.
109113
type attrs []Attrs
114+
110115
func (a attrs) ToAttrs() string {
111116
parts := make([]string, len(a))
112117
for i, attr := range a {
@@ -117,9 +122,10 @@ func (a attrs) ToAttrs() string {
117122

118123
// dataAttr represents some `data-*` attribute.
119124
type dataAttr struct {
120-
Name string
125+
Name string
121126
Value string
122127
}
128+
123129
func (d dataAttr) ToAttrs() string {
124130
return fmt.Sprintf("data-%s=%q", d.Name, d.Value)
125131
}
@@ -129,20 +135,19 @@ func (d dataAttr) ToAttrs() string {
129135
func makeTag(name string) func(Attrs, ...toHTML) Tag {
130136
return func(attrs Attrs, children ...toHTML) Tag {
131137
return Tag{
132-
Name: name,
133-
Attrs: attrs,
138+
Name: name,
139+
Attrs: attrs,
134140
Children: children,
135141
}
136142
}
137143
}
138144

139145
var (
140-
dd = makeTag("dd")
141-
dt = makeTag("dt")
142-
dl = makeTag("dl")
146+
dd = makeTag("dd")
147+
dt = makeTag("dt")
148+
dl = makeTag("dl")
143149
details = makeTag("details")
144150
summary = makeTag("summary")
145-
span = makeTag("span")
146-
div = makeTag("div")
151+
span = makeTag("span")
152+
div = makeTag("div")
147153
)
148-

docs/book/utils/markerdocs/main.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"encoding/json"
2021
"fmt"
21-
"os"
2222
"log"
23-
"strings"
23+
"os"
2424
"os/exec"
25-
"encoding/json"
25+
"strings"
2626

2727
"sigs.k8s.io/kubebuilder/docs/book/utils/plugin"
2828
)
@@ -44,7 +44,7 @@ func maybeDetails(help *DetailedHelp) toHTML {
4444
if help.Summary == "" && help.Details == "" {
4545
return Fragment{}
4646
}
47-
47+
4848
return Fragment{
4949
details(nil,
5050
summary(optionalClasses{"no-details": help.Details == ""},
@@ -59,7 +59,7 @@ func markerTemplate(marker *MarkerDoc) toHTML {
5959
// the marker name
6060
term := dt(classes{"literal", "name"},
6161
Text(marker.Name))
62-
62+
6363
// the args summary (displayed in summary mode)
6464
var fields []toHTML
6565
for _, field := range marker.Fields {
@@ -73,7 +73,7 @@ func markerTemplate(marker *MarkerDoc) toHTML {
7373
argsDef := dd(classes{"args"},
7474
dl(classes{"args", "summary"},
7575
fields...))
76-
76+
7777
// the argument details (displayed in details mode)
7878
var args Fragment
7979
for _, field := range marker.Fields {
@@ -91,14 +91,14 @@ func markerTemplate(marker *MarkerDoc) toHTML {
9191
maybeDetails(&marker.DetailedHelp),
9292
dl(classes{"args"},
9393
args))
94-
94+
9595
// the overall wrapping marker (common classes go here to make it easier to select
9696
// on certain things w/o duplication)
9797
markerAttrs := attrs{
9898
optionalClasses{
99-
"marker": true,
99+
"marker": true,
100100
"deprecated": marker.DeprecatedInFavorOf != nil,
101-
"anonymous": marker.Anonymous(),
101+
"anonymous": marker.Anonymous(),
102102
},
103103
dataAttr{Name: "target", Value: marker.Target},
104104
}
@@ -155,7 +155,7 @@ func (p MarkerDocs) Process(input *plugin.Input) error {
155155

156156
// HTML5 says that any characters are valid in ID except for space,
157157
// but may not be empty (which we prevent by skipping un-named categories):
158-
// https://www.w3.org/TR/html52/dom.html#element-attrdef-global-id
158+
// https://www.w3.org/TR/html52/dom.html#element-attrdef-global-id
159159
categoryAlias := strings.ReplaceAll(category, " ", "-")
160160

161161
content := new(strings.Builder)
@@ -182,7 +182,7 @@ func (p MarkerDocs) Process(input *plugin.Input) error {
182182

183183
// ... and finally make sure we didn't miss any
184184
if len(usedCategories) != len(markersByCategory) {
185-
unusedCategories := make([]string, 0, len(markersByCategory) - len(usedCategories))
185+
unusedCategories := make([]string, 0, len(markersByCategory)-len(usedCategories))
186186
for cat := range markersByCategory {
187187
if _, ok := usedCategories[cat]; !ok {
188188
unusedCategories = append(unusedCategories, cat)

0 commit comments

Comments
 (0)