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
+
1
17
package main
2
18
3
19
import (
@@ -23,48 +39,16 @@ import (
23
39
type Literate struct {}
24
40
func (_ Literate ) SupportsOutput (_ string ) bool { return true }
25
41
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 )
61
44
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 )
64
49
}
65
50
66
- chapter .Content = strings .Join (res , "" )
67
- return nil
51
+ return extractContents (contents , path )
68
52
})
69
53
}
70
54
0 commit comments