-
Notifications
You must be signed in to change notification settings - Fork 134
/
fixlinks.go
188 lines (164 loc) · 5.34 KB
/
fixlinks.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
// Copyright 2015 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ingore
//
// 给特殊模式标定的单词增加链接.
//
// Example:
// fixlinks
// fixlinks dir "\.go$"
//
// Help:
// fixlinks -h
//
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"unicode/utf8"
)
const MaxFileSize = 8 << 20 // 8MB
const usage = `
Usage: fixlinks dir [nameFilter]
fixlinks -h
Example:
fixlinks
fixlinks dir "\.go$"
Report bugs to <chaishushan{AT}gmail.com>.
`
func main() {
if len(os.Args) < 2 || os.Args[1] == "-h" {
fmt.Fprintln(os.Stderr, usage[1:len(usage)-1])
os.Exit(0)
}
dir, nameFilter := os.Args[1], ".*"
if len(os.Args) > 2 {
nameFilter = os.Args[2]
}
total := 0
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal("filepath.Walk: ", err)
return err
}
if info.IsDir() {
return nil
}
relpath, err := filepath.Rel(dir, path)
if err != nil {
log.Fatal("filepath.Rel: ", err)
return err
}
mathed, err := regexp.MatchString(nameFilter, relpath)
if err != nil {
log.Fatal("regexp.MatchString: ", err)
}
if mathed {
if changed := convertFile(path); changed {
fmt.Printf("%s\n", relpath)
total++
}
}
return nil
})
fmt.Printf("total %d\n", total)
}
func convertFile(path string) (changed bool) {
abspath, err := filepath.Abs(path)
if err != nil {
log.Fatal("convertFile: filepath.Abs:", err)
}
fi, err := os.Lstat(abspath)
if err != nil {
log.Fatal("convertFile: os.Lstat:", err)
}
if fi.Size() > MaxFileSize {
return false
}
oldData, err := ioutil.ReadFile(abspath)
if err != nil {
log.Fatal("convertFile: ioutil.ReadFile:", err)
}
if !utf8.Valid(oldData) {
return false
}
newData := append([]byte{}, oldData...)
for re, v := range _RegexpLinksTable {
newData = re.ReplaceAll(newData, []byte(v))
}
if string(newData) == string(oldData) {
return false
}
err = ioutil.WriteFile(abspath, newData, 0666)
if err != nil {
log.Fatal("convertFile: ioutil.WriteFile:", err)
}
return true
}
var _RegexpLinksTable = func() map[*regexp.Regexp]string {
const (
reHttp = `(https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|])?`
reWikiTail = `(\([-A-Za-z0-9+~_]+\))?`
)
m := make(map[*regexp.Regexp]string)
for k, v := range _LinkTable {
reKey := regexp.MustCompile(regexp.QuoteMeta(`[`+k+`]`) + `\(` + reHttp + reWikiTail + `\)`)
m[reKey] = fmt.Sprintf("[%s](%s)", k, v)
}
return m
}()
var _LinkTable = map[string]string{
// 人名
"Alan Donovan": "https://github.com/adonovan",
"Brian Kernighan": "http://www.cs.princeton.edu/~bwk/",
"Alan A. A. Donovan": "https://github.com/adonovan",
"Brian W. Kernighan": "http://www.cs.princeton.edu/~bwk/",
"Dennis M. Ritchie": "http://genius.cat-v.org/dennis-ritchie/",
"Robert Griesemer": "http://research.google.com/pubs/author96.html",
"Rob Pike": "http://genius.cat-v.org/rob-pike/",
"Ken Thompson": "http://genius.cat-v.org/ken-thompson/",
"Russ Cox": "http://research.swtch.com/",
"Niklaus Wirth": "https://en.wikipedia.org/wiki/Niklaus_Wirth",
"Tony Hoare": "https://en.wikipedia.org/wiki/Tony_Hoare",
"Fred Brooks": "http://www.cs.unc.edu/~brooks/",
// 图书
"The C Programming Language": "http://s3-us-west-2.amazonaws.com/belllabs-microsite-dritchie/cbook/index.html",
"The Practice of Programming": "https://en.wikipedia.org/wiki/The_Practice_of_Programming",
// Go语言
"Go": "https://golang.org/",
"Google’s Go": "https://golang.org/",
"oracle": "https://godoc.org/golang.org/x/tools/oracle",
"godoc -analysis": "https://godoc.org/golang.org/x/tools/cmd/godoc",
"gorename": "https://godoc.org/golang.org/x/tools/cmd/gorename",
// 其他语言
"Alef": "http://doc.cat-v.org/plan_9/2nd_edition/papers/alef/",
"APL": "https://en.wikipedia.org/wiki/APL_(programming_language)",
"Limbo": "http://doc.cat-v.org/inferno/4th_edition/limbo_language/",
"Modula-2": "https://en.wikipedia.org/wiki/Modula-2",
"Newsqueak": "http://doc.cat-v.org/bell_labs/squeak/",
"Oberon": "https://en.wikipedia.org/wiki/Oberon_(programming_language)",
"Oberon-2": "https://en.wikipedia.org/wiki/Oberon-2_(programming_language)",
"Pascal": "https://en.wikipedia.org/wiki/Pascal_(programming_language)",
"Scheme": "https://en.wikipedia.org/wiki/Scheme_(programming_language)",
"Squeak": "http://doc.cat-v.org/bell_labs/squeak/",
// 系统
"Unix": "http://doc.cat-v.org/unix/",
"UNIX": "http://doc.cat-v.org/unix/",
"Linux": "http://www.linux.org/",
"FreeBSD": "https://www.freebsd.org/",
"OpenBSD": "http://www.openbsd.org/",
"Mac OSX": "http://www.apple.com/cn/osx/",
"Mac OS X": "http://www.apple.com/cn/osx/",
"Plan9": "http://plan9.bell-labs.com/plan9/",
"Microsoft Windows": "https://www.microsoft.com/zh-cn/windows/",
// 其他
"Bell Labs": "http://www.cs.bell-labs.com/",
"communicating sequential processes": "https://en.wikipedia.org/wiki/Communicating_sequential_processes",
"CSP": "https://en.wikipedia.org/wiki/Communicating_sequential_processes",
"K&R": "https://en.wikipedia.org/wiki/K%26R",
}