-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
176 lines (145 loc) · 3.22 KB
/
main.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
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
var prefix = "freshReadmeSnippet: "
type base int
const (
NORMAL base = iota
COMMENT
FENCE
INSIDE_FENCE
END
)
func check(e error) {
if e != nil {
panic(e)
}
}
func panicIf(condition bool, fileName string, line int, message string, a ...interface{}) {
if condition {
err := fmt.Sprintf(message, a...)
panic(fmt.Sprintf("%s at %s:%d", err, fileName, line))
}
}
func includeFile(out *os.File, fileName string) {
in, err := ioutil.ReadFile(fileName)
check(err)
_, err = out.Write(in)
check(err)
_, err = out.WriteString("\n")
check(err)
}
func fromFile(out *os.File, header string, fileName string) {
in, err := os.Open(fileName)
check(err)
defer in.Close()
var state = NORMAL
var i = 0
var skip = 0
var indent = ' '
scanner := bufio.NewScanner(in)
for scanner.Scan() {
i++
var line = scanner.Text()
var ruline = []rune(line)
if state == COMMENT {
state = FENCE
for skip < len(ruline) &&
(ruline[skip] == ' ' || ruline[skip] == '\t') {
skip++
}
indent = ruline[0]
}
if strings.Contains(line, header) {
if state == NORMAL {
state = COMMENT
} else if state == FENCE {
state = END
} else {
panicIf(true, fileName, i, "Snippet `%s` appears second time", header)
}
}
if state == FENCE {
if skip <= len(line) {
for c := 0; c < skip; c++ {
panicIf(ruline[c] != indent, fileName, i, "Character `%c` doesn't not match indent `%c` x %d ", ruline[c], indent, skip)
}
_, err := out.WriteString(string(ruline[skip:len(line)]) + "\n")
check(err)
} else {
_, err := out.WriteString("\n")
check(err)
}
}
}
panicIf(state != END, fileName, i, "Unable to find snippet `%s`", header)
}
func refresh(fileName string) {
dir := filepath.Dir(fileName)
refresh, _ := regexp.Compile("^<!--.*\\[freshReadmeSource\\]\\(([^#]+)#*(.*?)\\)")
in, err := os.Open(fileName)
check(err)
defer in.Close()
out, err := os.Create(fileName + ".tmp")
check(err)
defer out.Close()
state := NORMAL
header := ""
source := ""
i := 0
scanner := bufio.NewScanner(in)
for scanner.Scan() {
line := scanner.Text()
i++
if state == FENCE {
state = INSIDE_FENCE
if header == "" {
includeFile(out, filepath.Join(dir, source))
} else {
fromFile(out, prefix+header, filepath.Join(dir, source))
}
header = ""
source = ""
}
if strings.HasPrefix(line, "```") {
if state == COMMENT {
state = FENCE
} else if state == INSIDE_FENCE {
state = NORMAL
} else if state != NORMAL {
panicIf(true, fileName, i, "Unexpected ```")
}
}
matches := refresh.FindStringSubmatch(line)
if len(matches) > 0 && state != INSIDE_FENCE {
panicIf(state != NORMAL, fileName, i, "Unable to process include in include")
state = COMMENT
header = matches[2]
source = matches[1]
}
if state != INSIDE_FENCE {
_, err := out.WriteString(line + "\n")
check(err)
}
}
check(scanner.Err())
panicIf(state != NORMAL, fileName, i, "Unexpected end of file")
in.Close()
out.Close()
err = os.Rename(fileName+".tmp", fileName)
check(err)
}
func main() {
if len(os.Args) == 1 {
refresh("README.md")
} else {
refresh(os.Args[1])
}
}