Skip to content

Commit 74ea6c7

Browse files
committed
move litgo into own directory & refactor
This moves litgo into its own directory in order to prepare for having multiple doc plugins. It also refactors out the `{{#command}}` search logic so that it can be used across plugins.
1 parent 156acf6 commit 74ea6c7

File tree

5 files changed

+115
-40
lines changed

5 files changed

+115
-40
lines changed

docs/book/litgo.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -ex
44

55
(
66
pushd ./utils
7-
go build -o ../../../bin/literate-go ./literate.go
7+
go build -o ../../../bin/literate-go ./litgo
88
popd
99
) &>/dev/null
1010

docs/book/utils/literate.go docs/book/utils/litgo/literate.go

+23-39
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
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+
117
package main
218

319
import (
@@ -23,48 +39,16 @@ import (
2339
type Literate struct {}
2440
func (_ Literate) SupportsOutput(_ string) bool { return true }
2541
func (_ Literate) Process(input *plugin.Input) error {
26-
return plugin.EachItemInBook(&input.Book, func(chapter *plugin.BookChapter) error {
27-
if chapter.Content == "" {
28-
return nil
29-
}
30-
31-
// figure out all the trigger expressins
32-
partsRaw := strings.Split(chapter.Content, "{{#literatego ")
33-
// the first section won't start with `{{#literatego ` as per how split works
34-
if len(partsRaw) < 2 {
35-
return nil
36-
}
37-
38-
var res []string
39-
res = append(res, partsRaw[0])
40-
for _, part := range partsRaw[1:] {
41-
endDelim := strings.Index(part, "}}")
42-
if endDelim < 0 {
43-
return fmt.Errorf("missing end delimiter in chapter %q", chapter.Name)
44-
}
45-
// we need to join the path with the context root and the book's
46-
// source directory, since we assume paths are relative to the
47-
// given chapter file, like `{{#include}}`
48-
relPath := part[:endDelim]
49-
path := filepath.Join(input.Context.Root, input.Context.Config.Book.Src, filepath.Dir(chapter.Path), relPath)
50-
51-
// TODO(directxman12): don't escape root?
52-
contents, err := ioutil.ReadFile(path)
53-
if err != nil {
54-
return fmt.Errorf("unable to import %q: %v", path, err)
55-
}
56-
57-
newContents, err := extractContents(contents, path)
58-
if err != nil {
59-
return fmt.Errorf("unable to process %q: %v", path, err)
60-
}
42+
return plugin.EachCommand(&input.Book, "literatego", func(chapter *plugin.BookChapter, relPath string) (string, error) {
43+
path := filepath.Join(input.Context.Root, input.Context.Config.Book.Src, filepath.Dir(chapter.Path), relPath)
6144

62-
res = append(res, string(newContents))
63-
res = append(res, part[endDelim+2:])
45+
// TODO(directxman12): don't escape root?
46+
contents, err := ioutil.ReadFile(path)
47+
if err != nil {
48+
return "", fmt.Errorf("unable to import %q: %v", path, err)
6449
}
6550

66-
chapter.Content = strings.Join(res, "")
67-
return nil
51+
return extractContents(contents, path)
6852
})
6953
}
7054

docs/book/utils/plugin/input.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
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+
117
package plugin
218

319
import (

docs/book/utils/plugin/plugin.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
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+
117
package plugin
218

319
import (

docs/book/utils/plugin/utils.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
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 plugin
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
)
23+
24+
// EachCommand looks for mustache-like declarations of the form `{{#cmd args}}`
25+
// in each book chapter, and calls the callback for each one, substituting it
26+
// for the result.
27+
func EachCommand(book *Book, cmd string, callback func(chapter *BookChapter, args string) (string, error)) error {
28+
cmdStart := fmt.Sprintf("{{#%s ", cmd)
29+
return EachItemInBook(book, func(chapter *BookChapter) error {
30+
if chapter.Content == "" {
31+
return nil
32+
}
33+
34+
// figure out all the trigger expressins
35+
partsRaw := strings.Split(chapter.Content, cmdStart)
36+
// the first section won't start with `{{#<cmd> ` as per how split works
37+
if len(partsRaw) < 2 {
38+
return nil
39+
}
40+
41+
var res []string
42+
res = append(res, partsRaw[0])
43+
for _, part := range partsRaw[1:] {
44+
endDelim := strings.Index(part, "}}")
45+
if endDelim < 0 {
46+
return fmt.Errorf("missing end delimiter in chapter %q", chapter.Name)
47+
}
48+
newContents, err := callback(chapter, part[:endDelim])
49+
if err != nil {
50+
return err
51+
}
52+
res = append(res, string(newContents))
53+
res = append(res, part[endDelim+2:])
54+
}
55+
56+
chapter.Content = strings.Join(res, "")
57+
return nil
58+
})
59+
}

0 commit comments

Comments
 (0)