-
Notifications
You must be signed in to change notification settings - Fork 940
/
Copy pathweave.go
220 lines (204 loc) · 5.01 KB
/
weave.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// The weave command is a simple preprocessor for markdown files.
// It builds a table of contents and processes %include directives.
//
// Example usage:
//
// $ go run internal/cmd/weave go-types.md > README.md
//
// The weave command copies lines of the input file to standard output, with two
// exceptions:
//
// If a line begins with "%toc", it is replaced with a table of contents
// consisting of links to the top two levels of headers ("#" and "##").
//
// If a line begins with "%include FILENAME TAG", it is replaced with the lines
// of the file between lines containing "!+TAG" and "!-TAG". TAG can be omitted,
// in which case the delimiters are simply "!+" and "!-".
//
// Before the included lines, a line of the form
//
// // go get PACKAGE
//
// is output, where PACKAGE is constructed from the module path, the
// base name of the current directory, and the directory of FILENAME.
// This caption can be suppressed by putting "-" as the final word of the %include line.
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
func main() {
log.SetFlags(0)
log.SetPrefix("weave: ")
if len(os.Args) != 2 {
log.Fatal("usage: weave input.md\n")
}
f, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer f.Close()
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
curDir := filepath.Base(wd)
fmt.Println("<!-- Autogenerated by weave; DO NOT EDIT -->")
// Pass 1: extract table of contents.
var toc []string
in := bufio.NewScanner(f)
for in.Scan() {
line := in.Text()
if line == "" || (line[0] != '#' && line[0] != '%') {
continue
}
line = strings.TrimSpace(line)
if line == "%toc" {
toc = nil
} else if strings.HasPrefix(line, "# ") || strings.HasPrefix(line, "## ") {
words := strings.Fields(line)
depth := len(words[0])
words = words[1:]
text := strings.Join(words, " ")
anchor := strings.Join(words, "-")
anchor = strings.ToLower(anchor)
anchor = strings.ReplaceAll(anchor, "**", "")
anchor = strings.ReplaceAll(anchor, "`", "")
anchor = strings.ReplaceAll(anchor, "_", "")
line = fmt.Sprintf("%s1. [%s](#%s)", strings.Repeat("\t", depth-1), text, anchor)
toc = append(toc, line)
}
}
if in.Err() != nil {
log.Fatal(in.Err())
}
// Pass 2.
if _, err := f.Seek(0, io.SeekStart); err != nil {
log.Fatalf("can't rewind input: %v", err)
}
in = bufio.NewScanner(f)
for in.Scan() {
line := in.Text()
switch {
case strings.HasPrefix(line, "%toc"): // ToC
for _, h := range toc {
fmt.Println(h)
}
case strings.HasPrefix(line, "%include"):
words := strings.Fields(line)
if len(words) < 2 {
log.Fatal(line)
}
filename := words[1]
// Show caption unless '-' follows.
if len(words) < 4 || words[3] != "-" {
fmt.Printf(" // go get golang.org/x/example/%s/%s\n\n",
curDir, filepath.Dir(filename))
}
section := ""
if len(words) > 2 {
section = words[2]
}
s, err := include(filename, section)
if err != nil {
log.Fatal(err)
}
fmt.Println("```")
fmt.Println(cleanListing(s)) // TODO(adonovan): escape /^```/ in s
fmt.Println("```")
default:
fmt.Println(line)
}
}
if in.Err() != nil {
log.Fatal(in.Err())
}
}
// include processes an included file, and returns the included text.
// Only lines between those matching !+tag and !-tag will be returned.
// This is true even if tag=="".
func include(file, tag string) (string, error) {
f, err := os.Open(file)
if err != nil {
return "", err
}
defer f.Close()
startre, err := regexp.Compile("!\\+" + tag + "$")
if err != nil {
return "", err
}
endre, err := regexp.Compile("!\\-" + tag + "$")
if err != nil {
return "", err
}
var text bytes.Buffer
in := bufio.NewScanner(f)
var on bool
for in.Scan() {
line := in.Text()
switch {
case startre.MatchString(line):
on = true
case endre.MatchString(line):
on = false
case on:
text.WriteByte('\t')
text.WriteString(line)
text.WriteByte('\n')
}
}
if in.Err() != nil {
return "", in.Err()
}
if text.Len() == 0 {
return "", fmt.Errorf("no lines of %s matched tag %q", file, tag)
}
return text.String(), nil
}
// cleanListing removes entirely blank leading and trailing lines from
// text, and removes n leading tabs.
func cleanListing(text string) string {
lines := strings.Split(text, "\n")
// remove minimum number of leading tabs from all non-blank lines
tabs := 999
for i, line := range lines {
if strings.TrimSpace(line) == "" {
lines[i] = ""
} else {
if n := leadingTabs(line); n < tabs {
tabs = n
}
}
}
for i, line := range lines {
if line != "" {
line := line[tabs:]
lines[i] = line // remove leading tabs
}
}
// remove leading blank lines
for len(lines) > 0 && lines[0] == "" {
lines = lines[1:]
}
// remove trailing blank lines
for len(lines) > 0 && lines[len(lines)-1] == "" {
lines = lines[:len(lines)-1]
}
return strings.Join(lines, "\n")
}
func leadingTabs(s string) int {
var i int
for i = 0; i < len(s); i++ {
if s[i] != '\t' {
break
}
}
return i
}