package parserimport ()// isInclude parses {{...}}[...], that contains a path between the {{, the [...] syntax contains// an address to select which lines to include. It is treated as an opaque string and just given// to readInclude.func ( *Parser) ( []byte) ( string, []byte, int) { := skipCharN(, 0, ' ', 3) // start with up to 3 spacesiflen([:]) < 3 {return"", nil, 0 }if [] != '{' || [+1] != '{' {return"", nil, 0 } := + 2// find the end delimiter = skipUntilChar(, , '}')if +1 >= len() {return"", nil, 0 } := ++if [] != '}' {return"", nil, 0 } = string([:])if +1 < len() && [+1] == '[' { // potential address specification := + 2 = skipUntilChar(, , ']')if >= len() {return"", nil, 0 } = [:]return , , + 1 }return , , + 1}func ( *Parser) (, string, []byte) []byte {if .Opts.ReadIncludeFn != nil {return .Opts.ReadIncludeFn(, , ) }returnnil}// isCodeInclude parses <{{...}} which is similar to isInclude the returned bytes are, however wrapped in a code block.func ( *Parser) ( []byte) ( string, []byte, int) { := skipCharN(, 0, ' ', 3) // start with up to 3 spacesiflen([:]) < 3 {return"", nil, 0 }if [] != '<' {return"", nil, 0 } := , , = .isInclude([+1:])if == 0 {return"", nil, 0 }return , , + + 1}// readCodeInclude acts like include except the returned bytes are wrapped in a fenced code block.func ( *Parser) (, string, []byte) []byte { := .readInclude(, , )if == nil {returnnil } := path.Ext() := &bytes.Buffer{} .Write([]byte("```"))if != "" { // starts with a dot .WriteString(" " + [1:] + "\n") } else { .WriteByte('\n') } .Write() .WriteString("```\n")return .Bytes()}// incStack hold the current stack of chained includes. Each value is the containing// path of the file being parsed.type incStack struct { stack []string}func newIncStack() *incStack {return &incStack{stack: []string{}}}// Push updates i with new.func ( *incStack) ( string) {ifpath.IsAbs() { .stack = append(.stack, path.Dir())return } := ""iflen(.stack) > 0 { = .stack[len(.stack)-1] } .stack = append(.stack, path.Dir(filepath.Join(, )))}// Pop pops the last value.func ( *incStack) () {iflen(.stack) == 0 {return } .stack = .stack[:len(.stack)-1]}func ( *incStack) () string {iflen(.stack) == 0 {return"" }return .stack[len(.stack)-1]}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.