Skip to content

Commit

Permalink
code formating
Browse files Browse the repository at this point in the history
  • Loading branch information
sivanzcw committed Nov 14, 2019
1 parent 971cf03 commit 66cdfec
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 78 deletions.
2 changes: 1 addition & 1 deletion docs/book/src/cronjob-tutorial/testdata/emptycontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ objects, so these are added out of the box.
// CronJobReconciler reconciles a CronJob object
type CronJobReconciler struct {
client.Client
Log logr.Logger
Log logr.Logger
Scheme *runtime.Scheme
}

Expand Down
37 changes: 19 additions & 18 deletions docs/book/utils/litgo/literate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ package main

import (
"fmt"
"os"
"io/ioutil"
"path/filepath"
"go/scanner"
"go/token"
"io/ioutil"
"log"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"unicode"
"path"
"net/url"

"sigs.k8s.io/kubebuilder/docs/book/utils/plugin"
)
Expand All @@ -44,15 +44,16 @@ type Literate struct {
// BaseSourcePath specifies the base path to internet-reachable versions of the source code used
BaseSourcePath *url.URL
}

func (_ Literate) SupportsOutput(_ string) bool { return true }
func (l Literate) Process(input *plugin.Input) error {
bookSrcDir := filepath.Join(input.Context.Root, input.Context.Config.Book.Src)
return plugin.EachCommand(&input.Book, "literatego", func(chapter *plugin.BookChapter, relPath string) (string, error) {
chapterDir := filepath.Dir(chapter.Path)
pathInfo := filePathInfo{
chapterRelativePath: relPath,
chapterDir: chapterDir,
bookSrcDir: bookSrcDir,
chapterDir: chapterDir,
bookSrcDir: bookSrcDir,
}
path := pathInfo.FullPath()

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

outURL.Path = path.Join(outURL.Path, relPath)

return outURL.String()
}

// commentCodePair represents a block of code with some text before it, optionally
// marked as collapsed with the given "collapse summary".
type commentCodePair struct {
comment string
code string
comment string
code string
collapse string
}

Expand Down Expand Up @@ -141,7 +142,7 @@ func isBlockComment(tok token.Token, lit string) bool {
// commentText extracts the text from the given comment, slicing off
// some common amount of whitespace prefix.
func commentText(raw string, lineOffset int) string {
rawBody := raw[2:len(raw)-2] // chop of the delimiters
rawBody := raw[2 : len(raw)-2] // chop of the delimiters
lines := strings.Split(rawBody, "\n")
if len(lines) == 0 {
return ""
Expand Down Expand Up @@ -188,21 +189,21 @@ func extractPairs(contents []byte, path string) ([]commentCodePair, error) {
if collapse == "" && !isBlockComment(tok, lit) {
continue
}
codeEnd := file.Offset(pos)-1
if codeEnd - lastCodeBlockStart > 0 {
codeEnd := file.Offset(pos) - 1
if codeEnd-lastCodeBlockStart > 0 {
lastPair.code = string(contents[lastCodeBlockStart:codeEnd])
}
pairs = append(pairs, lastPair)
if collapse == "" {
line := file.Line(pos)
lineStart := file.LineStart(line)
lastPair = commentCodePair{
comment: commentText(lit, file.Offset(pos) - file.Offset(lineStart)),
comment: commentText(lit, file.Offset(pos)-file.Offset(lineStart)),
}
} else {
lastPair = commentCodePair{}
}
lastCodeBlockStart = file.Offset(pos)+len(lit)
lastCodeBlockStart = file.Offset(pos) + len(lit)
}
lastPair.code = string(contents[lastCodeBlockStart:])
pairs = append(pairs, lastPair)
Expand Down Expand Up @@ -236,7 +237,7 @@ func (l Literate) extractContents(contents []byte, pathInfo filePathInfo) (strin
prettyPath = prunedPath
}
out.WriteString(fmt.Sprintf(`<cite class="literate-source"><a href="%[1]s">%[2]s</a></cite>`, sourcePath, prettyPath))

for _, pair := range pairs {
if pair.collapse != "" {
// NB(directxman12): we add the hljs class to "cheat" and get the
Expand All @@ -256,7 +257,7 @@ func (l Literate) extractContents(contents []byte, pathInfo filePathInfo) (strin
out.WriteString(wrapWithNewlines(pair.code))
out.WriteString("```\n")
}
if pair.collapse != ""{
if pair.collapse != "" {
out.WriteString("\n</details>")
}
// TODO(directxman12): nice side-by-side sections
Expand Down Expand Up @@ -293,7 +294,7 @@ func main() {
}
cfg := Literate{
PrettyPathPrunePrefix: "testdata",
BaseSourcePath: baseURL,
BaseSourcePath: baseURL,
}
if err := plugin.Run(cfg, os.Stdin, os.Stdout, os.Args[1:]...); err != nil {
log.Fatal(err.Error())
Expand Down
23 changes: 11 additions & 12 deletions docs/book/utils/markerdocs/doctypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ type DetailedHelp struct {
}

type Argument struct {
Type string `json:"type"`
Optional bool `json:"optional"`
ItemType *Argument `json:"itemType"`
Type string `json:"type"`
Optional bool `json:"optional"`
ItemType *Argument `json:"itemType"`
}

type FieldHelp struct {
// definition
Name string `json:"name"`
Name string `json:"name"`
Argument `json:",inline"`

// help
Expand All @@ -42,23 +42,22 @@ type FieldHelp struct {
type MarkerDoc struct {
// definition

Name string `json:"name"`
Name string `json:"name"`
Target string `json:"target"`

// help

DetailedHelp `json:",inline"`
Category string `json:"category"`
DeprecatedInFavorOf *string `json:"deprecatedInFavorOf"`
Fields []FieldHelp `json:"fields"`
DetailedHelp `json:",inline"`
Category string `json:"category"`
DeprecatedInFavorOf *string `json:"deprecatedInFavorOf"`
Fields []FieldHelp `json:"fields"`
}

type CategoryDoc struct {
Category string `json:"category"`
Markers []MarkerDoc `json:"markers"`
Category string `json:"category"`
Markers []MarkerDoc `json:"markers"`
}

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

31 changes: 18 additions & 13 deletions docs/book/utils/markerdocs/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ package main

import (
"fmt"
"strings"
"html"
"io"

"strings"
)

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

// Text is a chunk of text in an HTML doc.
type Text string

func (t Text) WriteHTML(w io.Writer) error {
_, err := io.WriteString(w, html.EscapeString(string(t)))
return err
}

// Tag is some tag with contents and attributes in an HTML doc.
type Tag struct {
Name string
Attrs Attrs
Name string
Attrs Attrs
Children []toHTML
}

func (t Tag) WriteHTML(w io.Writer) error {
attrsOut := ""
if t.Attrs != nil {
Expand All @@ -74,6 +75,7 @@ func (t Tag) WriteHTML(w io.Writer) error {

// Fragment is some series of tags, text, etc in an HTML doc.
type Fragment []toHTML

func (f Fragment) WriteHTML(w io.Writer) error {
for _, item := range f {
if err := item.WriteHTML(w); err != nil {
Expand All @@ -91,10 +93,12 @@ type Attrs interface {

// classes sets the class attribute to these class names.
type classes []string

func (c classes) ToAttrs() string { return fmt.Sprintf("class=%q", strings.Join(c, " ")) }

// optionalClasses sets the the class attribute to these class names, if their values are true.
type optionalClasses map[string]bool

func (c optionalClasses) ToAttrs() string {
actualClasses := make([]string, 0, len(c))
for class, active := range c {
Expand All @@ -107,6 +111,7 @@ func (c optionalClasses) ToAttrs() string {

// attrs joins together one or more Attrs.
type attrs []Attrs

func (a attrs) ToAttrs() string {
parts := make([]string, len(a))
for i, attr := range a {
Expand All @@ -117,9 +122,10 @@ func (a attrs) ToAttrs() string {

// dataAttr represents some `data-*` attribute.
type dataAttr struct {
Name string
Name string
Value string
}

func (d dataAttr) ToAttrs() string {
return fmt.Sprintf("data-%s=%q", d.Name, d.Value)
}
Expand All @@ -129,20 +135,19 @@ func (d dataAttr) ToAttrs() string {
func makeTag(name string) func(Attrs, ...toHTML) Tag {
return func(attrs Attrs, children ...toHTML) Tag {
return Tag{
Name: name,
Attrs: attrs,
Name: name,
Attrs: attrs,
Children: children,
}
}
}

var (
dd = makeTag("dd")
dt = makeTag("dt")
dl = makeTag("dl")
dd = makeTag("dd")
dt = makeTag("dt")
dl = makeTag("dl")
details = makeTag("details")
summary = makeTag("summary")
span = makeTag("span")
div = makeTag("div")
span = makeTag("span")
div = makeTag("div")
)

22 changes: 11 additions & 11 deletions docs/book/utils/markerdocs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ limitations under the License.
package main

import (
"encoding/json"
"fmt"
"os"
"log"
"strings"
"os"
"os/exec"
"encoding/json"
"strings"

"sigs.k8s.io/kubebuilder/docs/book/utils/plugin"
)
Expand All @@ -44,7 +44,7 @@ func maybeDetails(help *DetailedHelp) toHTML {
if help.Summary == "" && help.Details == "" {
return Fragment{}
}

return Fragment{
details(nil,
summary(optionalClasses{"no-details": help.Details == ""},
Expand All @@ -59,7 +59,7 @@ func markerTemplate(marker *MarkerDoc) toHTML {
// the marker name
term := dt(classes{"literal", "name"},
Text(marker.Name))

// the args summary (displayed in summary mode)
var fields []toHTML
for _, field := range marker.Fields {
Expand All @@ -73,7 +73,7 @@ func markerTemplate(marker *MarkerDoc) toHTML {
argsDef := dd(classes{"args"},
dl(classes{"args", "summary"},
fields...))

// the argument details (displayed in details mode)
var args Fragment
for _, field := range marker.Fields {
Expand All @@ -91,14 +91,14 @@ func markerTemplate(marker *MarkerDoc) toHTML {
maybeDetails(&marker.DetailedHelp),
dl(classes{"args"},
args))

// the overall wrapping marker (common classes go here to make it easier to select
// on certain things w/o duplication)
markerAttrs := attrs{
optionalClasses{
"marker": true,
"marker": true,
"deprecated": marker.DeprecatedInFavorOf != nil,
"anonymous": marker.Anonymous(),
"anonymous": marker.Anonymous(),
},
dataAttr{Name: "target", Value: marker.Target},
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func (p MarkerDocs) Process(input *plugin.Input) error {

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

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

// ... and finally make sure we didn't miss any
if len(usedCategories) != len(markersByCategory) {
unusedCategories := make([]string, 0, len(markersByCategory) - len(usedCategories))
unusedCategories := make([]string, 0, len(markersByCategory)-len(usedCategories))
for cat := range markersByCategory {
if _, ok := usedCategories[cat]; !ok {
unusedCategories = append(unusedCategories, cat)
Expand Down
Loading

0 comments on commit 66cdfec

Please sign in to comment.