Skip to content

Commit e8420e4

Browse files
committed
weave: fix %include parsing, add highlighting
Recognize that %include file - means "no tag, no caption" instead of "the tag is -, add caption." Write "go" after backticks, which GitHub will render with syntax highlighting. Change-Id: I9e0b3618b5fba8834d7f06b8fc7adf0781574a2d Reviewed-on: https://go-review.googlesource.com/c/example/+/673875 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent f8bc3a0 commit e8420e4

File tree

3 files changed

+66
-52
lines changed

3 files changed

+66
-52
lines changed

gotypes/README.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ run `go get golang.org/x/example/gotypes/...`.
143143

144144
// go get golang.org/x/example/gotypes/pkginfo
145145

146-
```
146+
```go
147147
package main
148148

149149
import (
@@ -247,7 +247,7 @@ Finally, the program prints the attributes of the package, shown below.
247247
(The hexadecimal number may vary from one run to the next.)
248248

249249

250-
```
250+
```go
251251
$ go build golang.org/x/example/gotypes/pkginfo
252252
$ ./pkginfo
253253
Package "cmd/hello"
@@ -505,7 +505,7 @@ identifier in the input program, and the object it refers to.
505505

506506
// go get golang.org/x/example/gotypes/defsuses
507507

508-
```
508+
```go
509509
func PrintDefsUses(fset *token.FileSet, files ...*ast.File) error {
510510
conf := types.Config{Importer: importer.Default()}
511511
info := &types.Info{
@@ -535,7 +535,7 @@ Let's use the _hello, world_ program again as the input:
535535

536536
// go get golang.org/x/example/gotypes/hello
537537

538-
```
538+
```go
539539
package main
540540

541541
import "fmt"
@@ -549,7 +549,7 @@ func main() {
549549
This is what it prints:
550550

551551

552-
```
552+
```go
553553
$ go build golang.org/x/example/gotypes/defsuses
554554
$ ./defsuses
555555
hello.go:1:9: "main" defines <nil>
@@ -796,7 +796,7 @@ preserve comments in the input.
796796

797797
// go get golang.org/x/example/gotypes/lookup
798798

799-
```
799+
```go
800800
func main() {
801801
fset := token.NewFileSet()
802802
f, err := parser.ParseFile(fset, "hello.go", hello, parser.ParseComments)
@@ -839,7 +839,7 @@ The second comment looks up `"fmt"` in the `main` function's block,
839839
and so on.
840840

841841

842-
```
842+
```go
843843
const hello = `
844844
package main
845845
@@ -863,7 +863,7 @@ func main() {
863863
Here's the output:
864864

865865

866-
```
866+
```go
867867
$ go build golang.org/x/example/gotypes/lookup
868868
$ ./lookup
869869
At hello.go:6:1, "append" = builtin append
@@ -1566,7 +1566,7 @@ type-checked file and prints its type, value, and mode:
15661566

15671567
// go get golang.org/x/example/gotypes/typeandvalue
15681568

1569-
```
1569+
```go
15701570
// f is a parsed, type-checked *ast.File.
15711571
ast.Inspect(f, func(n ast.Node) bool {
15721572
if expr, ok := n.(ast.Expr); ok {
@@ -1596,7 +1596,7 @@ It makes use of these two helper functions, which are not shown:
15961596
Given this input:
15971597

15981598

1599-
```
1599+
```go
16001600
const input = `
16011601
package main
16021602
@@ -1613,7 +1613,7 @@ func main() {
16131613
the program prints:
16141614

16151615

1616-
```
1616+
```go
16171617
$ go build golang.org/x/example/gotypes/typeandvalue
16181618
$ ./typeandvalue
16191619
make(map[string]int) mode: value
@@ -1672,7 +1672,7 @@ comparing a method `x.f` against nil is a common mistake.
16721672

16731673
// go get golang.org/x/example/gotypes/nilfunc
16741674

1675-
```
1675+
```go
16761676
// CheckNilFuncComparison reports unintended comparisons
16771677
// of functions against nil, e.g., "if x.Method == nil {".
16781678
func CheckNilFuncComparison(info *types.Info, n ast.Node) {
@@ -1718,7 +1718,7 @@ func CheckNilFuncComparison(info *types.Info, n ast.Node) {
17181718
Given this input,
17191719

17201720

1721-
```
1721+
```go
17221722
const input = `package main
17231723
17241724
import "bytes"
@@ -1736,7 +1736,7 @@ func main() {
17361736
the program reports these errors:
17371737

17381738

1739-
```
1739+
```go
17401740
$ go build golang.org/x/example/gotypes/nilfunc
17411741
$ ./nilfunc
17421742
input.go:7:5: comparison of function Bytes == nil is always false
@@ -1969,7 +1969,7 @@ interface.
19691969
Here's an example:
19701970

19711971

1972-
```
1972+
```go
19731973
$ ./skeleton io ReadWriteCloser buffer
19741974
// *buffer implements io.ReadWriteCloser.
19751975
type buffer struct{}
@@ -1993,7 +1993,7 @@ calls `PrintSkeleton` with the remaining two arguments:
19931993

19941994
// go get golang.org/x/example/gotypes/skeleton
19951995

1996-
```
1996+
```go
19971997
func PrintSkeleton(pkg *types.Package, ifacename, concname string) error {
19981998
obj := pkg.Scope().Lookup(ifacename)
19991999
if obj == nil {
@@ -2051,7 +2051,7 @@ Passing `(*types.Package).Name` causes only the package name
20512051
Here's another example that illustrates it:
20522052

20532053

2054-
```
2054+
```go
20552055
$ ./skeleton net/http Handler myHandler
20562056
// *myHandler implements net/http.Handler.
20572057
type myHandler struct{}
@@ -2067,7 +2067,7 @@ in `pkg`, and reports the types that satisfy each interface type.
20672067

20682068
// go get golang.org/x/example/gotypes/implements
20692069

2070-
```
2070+
```go
20712071
// Find all named types at package level.
20722072
var allNamed []*types.Named
20732073
for _, name := range pkg.Scope().Names() {
@@ -2099,7 +2099,7 @@ Given this input,
20992099

21002100
// go get golang.org/x/example/gotypes/implements
21012101

2102-
```
2102+
```go
21032103
const input = `package main
21042104
21052105
type A struct{}
@@ -2118,7 +2118,7 @@ type J interface { g() }
21182118
the program prints:
21192119

21202120

2121-
```
2121+
```go
21222122
$ go build golang.org/x/example/gotypes/implements
21232123
$ ./implements
21242124
*hello.A satisfies hello.I
@@ -2276,7 +2276,7 @@ programs.
22762276

22772277
// go get golang.org/x/example/gotypes/hugeparam
22782278

2279-
```
2279+
```go
22802280
var bytesFlag = flag.Int("bytes", 48, "maximum parameter size in bytes")
22812281

22822282
func PrintHugeParams(fset *token.FileSet, info *types.Info, sizes types.Sizes, files []*ast.File) {
@@ -2324,7 +2324,7 @@ It reports a number of places where the 7-word
23242324
is copied.
23252325

23262326

2327-
```
2327+
```go
23282328
% ./hugeparam encoding/xml
23292329
/go/src/encoding/xml/marshal.go:167:50: "start" parameter: encoding/xml.StartElement = 56 bytes
23302330
/go/src/encoding/xml/marshal.go:734:97: "" result: encoding/xml.StartElement = 56 bytes
@@ -2408,7 +2408,7 @@ the command line.
24082408
Here's an example:
24092409

24102410

2411-
```
2411+
```go
24122412
$ ./doc net/http File
24132413
type net/http.File interface{Readdir(count int) ([]os.FileInfo, error); Seek(offset int64, whence int) (int64, error); Stat() (os.FileInfo, error); io.Closer; io.Reader}
24142414
$GOROOT/src/io/io.go:92:2: method (net/http.File) Close() error
@@ -2435,7 +2435,7 @@ plus exported type information for its dependencies.
24352435
24362436
// go get golang.org/x/example/gotypes/doc
24372437
2438-
```
2438+
```go
24392439
pkgpath, name := os.Args[1], os.Args[2]
24402440
24412441
// Load complete type information for the specified packages,
@@ -2465,7 +2465,7 @@ The rest of the program prints the output:
24652465
24662466
// go get golang.org/x/example/gotypes/doc
24672467
2468-
```
2468+
```go
24692469
// Print the object and its methods (incl. location of definition).
24702470
fmt.Println(obj)
24712471
for _, sel := range typeutil.IntuitiveMethodSet(obj.Type(), nil) {

internal/cmd/weave/weave.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ package main
2727
import (
2828
"bufio"
2929
"bytes"
30+
"flag"
3031
"fmt"
3132
"io"
3233
"log"
@@ -37,13 +38,14 @@ import (
3738
)
3839

3940
func main() {
41+
flag.Parse()
4042
log.SetFlags(0)
4143
log.SetPrefix("weave: ")
42-
if len(os.Args) != 2 {
44+
if flag.NArg() != 1 {
4345
log.Fatal("usage: weave input.md\n")
4446
}
4547

46-
f, err := os.Open(os.Args[1])
48+
f, err := os.Open(flag.Arg(0))
4749
if err != nil {
4850
log.Fatal(err)
4951
}
@@ -100,26 +102,38 @@ func main() {
100102
}
101103
case strings.HasPrefix(line, "%include"):
102104
words := strings.Fields(line)
103-
if len(words) < 2 {
104-
log.Fatal(line)
105+
var section string
106+
caption := true
107+
switch len(words) {
108+
case 2: // %include filename
109+
// Nothing to do.
110+
case 3: // %include filename section OR %include filename -
111+
if words[2] == "-" {
112+
caption = false
113+
} else {
114+
section = words[2]
115+
}
116+
case 4: // %include filename section -
117+
section = words[2]
118+
if words[3] != "-" {
119+
log.Fatalf("last word is not '-': %s", line)
120+
}
121+
caption = false
122+
default:
123+
log.Fatalf("wrong # words (want 2-4): %s", line)
105124
}
106125
filename := words[1]
107126

108-
// Show caption unless '-' follows.
109-
if len(words) < 4 || words[3] != "-" {
127+
if caption {
110128
fmt.Printf(" // go get golang.org/x/example/%s/%s\n\n",
111129
curDir, filepath.Dir(filename))
112130
}
113131

114-
section := ""
115-
if len(words) > 2 {
116-
section = words[2]
117-
}
118132
s, err := include(filename, section)
119133
if err != nil {
120134
log.Fatal(err)
121135
}
122-
fmt.Println("```")
136+
fmt.Println("```go")
123137
fmt.Println(cleanListing(s)) // TODO(adonovan): escape /^```/ in s
124138
fmt.Println("```")
125139
default:

0 commit comments

Comments
 (0)