@@ -18,16 +18,16 @@ package main
18
18
19
19
import (
20
20
"fmt"
21
- "os"
22
- "io/ioutil"
23
- "path/filepath"
24
21
"go/scanner"
25
22
"go/token"
23
+ "io/ioutil"
26
24
"log"
25
+ "net/url"
26
+ "os"
27
+ "path"
28
+ "path/filepath"
27
29
"strings"
28
30
"unicode"
29
- "path"
30
- "net/url"
31
31
32
32
"sigs.k8s.io/kubebuilder/docs/book/utils/plugin"
33
33
)
@@ -44,15 +44,16 @@ type Literate struct {
44
44
// BaseSourcePath specifies the base path to internet-reachable versions of the source code used
45
45
BaseSourcePath * url.URL
46
46
}
47
+
47
48
func (_ Literate ) SupportsOutput (_ string ) bool { return true }
48
49
func (l Literate ) Process (input * plugin.Input ) error {
49
50
bookSrcDir := filepath .Join (input .Context .Root , input .Context .Config .Book .Src )
50
51
return plugin .EachCommand (& input .Book , "literatego" , func (chapter * plugin.BookChapter , relPath string ) (string , error ) {
51
52
chapterDir := filepath .Dir (chapter .Path )
52
53
pathInfo := filePathInfo {
53
54
chapterRelativePath : relPath ,
54
- chapterDir : chapterDir ,
55
- bookSrcDir : bookSrcDir ,
55
+ chapterDir : chapterDir ,
56
+ bookSrcDir : bookSrcDir ,
56
57
}
57
58
path := pathInfo .FullPath ()
58
59
@@ -90,15 +91,15 @@ func (f filePathInfo) ViewablePath(baseBookSrcURL url.URL) string {
90
91
outURL := baseBookSrcURL
91
92
92
93
outURL .Path = path .Join (outURL .Path , relPath )
93
-
94
+
94
95
return outURL .String ()
95
96
}
96
97
97
98
// commentCodePair represents a block of code with some text before it, optionally
98
99
// marked as collapsed with the given "collapse summary".
99
100
type commentCodePair struct {
100
- comment string
101
- code string
101
+ comment string
102
+ code string
102
103
collapse string
103
104
}
104
105
@@ -141,7 +142,7 @@ func isBlockComment(tok token.Token, lit string) bool {
141
142
// commentText extracts the text from the given comment, slicing off
142
143
// some common amount of whitespace prefix.
143
144
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
145
146
lines := strings .Split (rawBody , "\n " )
146
147
if len (lines ) == 0 {
147
148
return ""
@@ -188,21 +189,21 @@ func extractPairs(contents []byte, path string) ([]commentCodePair, error) {
188
189
if collapse == "" && ! isBlockComment (tok , lit ) {
189
190
continue
190
191
}
191
- codeEnd := file .Offset (pos )- 1
192
- if codeEnd - lastCodeBlockStart > 0 {
192
+ codeEnd := file .Offset (pos ) - 1
193
+ if codeEnd - lastCodeBlockStart > 0 {
193
194
lastPair .code = string (contents [lastCodeBlockStart :codeEnd ])
194
195
}
195
196
pairs = append (pairs , lastPair )
196
197
if collapse == "" {
197
198
line := file .Line (pos )
198
199
lineStart := file .LineStart (line )
199
200
lastPair = commentCodePair {
200
- comment : commentText (lit , file .Offset (pos ) - file .Offset (lineStart )),
201
+ comment : commentText (lit , file .Offset (pos )- file .Offset (lineStart )),
201
202
}
202
203
} else {
203
204
lastPair = commentCodePair {}
204
205
}
205
- lastCodeBlockStart = file .Offset (pos )+ len (lit )
206
+ lastCodeBlockStart = file .Offset (pos ) + len (lit )
206
207
}
207
208
lastPair .code = string (contents [lastCodeBlockStart :])
208
209
pairs = append (pairs , lastPair )
@@ -236,7 +237,7 @@ func (l Literate) extractContents(contents []byte, pathInfo filePathInfo) (strin
236
237
prettyPath = prunedPath
237
238
}
238
239
out .WriteString (fmt .Sprintf (`<cite class="literate-source"><a href="%[1]s">%[2]s</a></cite>` , sourcePath , prettyPath ))
239
-
240
+
240
241
for _ , pair := range pairs {
241
242
if pair .collapse != "" {
242
243
// 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
256
257
out .WriteString (wrapWithNewlines (pair .code ))
257
258
out .WriteString ("```\n " )
258
259
}
259
- if pair .collapse != "" {
260
+ if pair .collapse != "" {
260
261
out .WriteString ("\n </details>" )
261
262
}
262
263
// TODO(directxman12): nice side-by-side sections
@@ -293,7 +294,7 @@ func main() {
293
294
}
294
295
cfg := Literate {
295
296
PrettyPathPrunePrefix : "testdata" ,
296
- BaseSourcePath : baseURL ,
297
+ BaseSourcePath : baseURL ,
297
298
}
298
299
if err := plugin .Run (cfg , os .Stdin , os .Stdout , os .Args [1 :]... ); err != nil {
299
300
log .Fatal (err .Error ())
0 commit comments